|
| 1 | +<?php /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Pdsinterop\Solid\Auth\Factory; |
| 6 | + |
| 7 | +use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
| 8 | +use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
| 9 | +use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
| 10 | +use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
| 11 | +use League\OAuth2\Server\Repositories\RepositoryInterface; |
| 12 | +use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
| 13 | +use Pdsinterop\Solid\Auth\Enum\Repository; |
| 14 | + |
| 15 | +class RepositoryFactory |
| 16 | +{ |
| 17 | + ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 18 | + |
| 19 | + /** @var array */ |
| 20 | + private $repositories; |
| 21 | + |
| 22 | + //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 23 | + |
| 24 | + public function __construct(array $repositories = []) |
| 25 | + { |
| 26 | + $this->repositories = $repositories; |
| 27 | + } |
| 28 | + |
| 29 | + final public function createAccessTokenRepository() : AccessTokenRepositoryInterface |
| 30 | + { |
| 31 | + return $this->createOnce(Repository::ACCESS_TOKEN); |
| 32 | + } |
| 33 | + |
| 34 | + final public function createAuthCodeRepository() : AuthCodeRepositoryInterface |
| 35 | + { |
| 36 | + static $clientEntity; |
| 37 | + |
| 38 | + if ($this->repositories[Repository::AUTH_CODE] === null) { |
| 39 | + $clientEntity = $this->createClientRepository()->createClientEntity(); |
| 40 | + } |
| 41 | + |
| 42 | + return $this->createOnce(Repository::AUTH_CODE, [$clientEntity]); |
| 43 | + } |
| 44 | + |
| 45 | + final public function createClientRepository() : ClientRepositoryInterface |
| 46 | + { |
| 47 | + return $this->createOnce(Repository::CLIENT); |
| 48 | + } |
| 49 | + |
| 50 | + final public function createRefreshTokenRepository() : RefreshTokenRepositoryInterface |
| 51 | + { |
| 52 | + return $this->createOnce(Repository::REFRESH_TOKEN); |
| 53 | + } |
| 54 | + |
| 55 | + final public function createScopeRepository() : ScopeRepositoryInterface |
| 56 | + { |
| 57 | + return $this->createOnce(Repository::SCOPE); |
| 58 | + } |
| 59 | + |
| 60 | + ////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 61 | + |
| 62 | + private function createOnce(string $className, array $properties = []): RepositoryInterface |
| 63 | + { |
| 64 | + if ($this->repositories[$className] === null) { |
| 65 | + $this->repositories[$className] = $this->create($className, $properties); |
| 66 | + } |
| 67 | + |
| 68 | + return $this->repositories[$className]; |
| 69 | + } |
| 70 | + |
| 71 | + private function create(string $className, array $properties = []) : RepositoryInterface |
| 72 | + { |
| 73 | + return new $className(...$properties); |
| 74 | + } |
| 75 | +} |
0 commit comments