-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryFactory.php
More file actions
80 lines (61 loc) · 2.55 KB
/
RepositoryFactory.php
File metadata and controls
80 lines (61 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php /** @noinspection PhpIncompatibleReturnTypeInspection */
declare(strict_types=1);
namespace Pdsinterop\Solid\Auth\Factory;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\RepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use Pdsinterop\Solid\Auth\Enum\Repository;
class RepositoryFactory
{
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
/** @var array */
private $repositories;
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public function __construct(array $repositories = [])
{
$this->repositories = $repositories;
}
public function setRepository($type, $repository)
{
$this->repositories[$type] = $repository;
}
final public function createAccessTokenRepository() : AccessTokenRepositoryInterface
{
return $this->createOnce(Repository::ACCESS_TOKEN);
}
final public function createAuthCodeRepository() : AuthCodeRepositoryInterface
{
static $clientEntity;
if (array_key_exists(Repository::AUTH_CODE, $this->repositories) === false) {
$clientEntity = $this->createClientRepository()->createClientEntity();
}
return $this->createOnce(Repository::AUTH_CODE, [$clientEntity]);
}
final public function createClientRepository() : ClientRepositoryInterface
{
return $this->createOnce(Repository::CLIENT);
}
final public function createRefreshTokenRepository() : RefreshTokenRepositoryInterface
{
return $this->createOnce(Repository::REFRESH_TOKEN);
}
final public function createScopeRepository() : ScopeRepositoryInterface
{
return $this->createOnce(Repository::SCOPE);
}
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private function createOnce(string $className, array $properties = []): RepositoryInterface
{
if (array_key_exists($className, $this->repositories) === false) {
$this->repositories[$className] = $this->create($className, $properties);
}
return $this->repositories[$className];
}
private function create(string $className, array $properties = []) : RepositoryInterface
{
return new $className(...$properties);
}
}