Skip to content

Commit

Permalink
feature #130 Decoupled models from managers issue (pistej)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 0.4-dev branch.

Discussion
----------

Decoupled models from managers issue

Issue #128

Commits
-------

547e571 Decoupled models from managers issue
  • Loading branch information
chalasr committed Apr 23, 2023
2 parents b1036a3 + 547e571 commit 272facb
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 70 deletions.
10 changes: 5 additions & 5 deletions src/Event/PreSaveClientEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace League\Bundle\OAuth2ServerBundle\Event;

use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
Expand All @@ -13,21 +13,21 @@
class PreSaveClientEvent extends Event
{
/**
* @var AbstractClient
* @var ClientInterface
*/
private $client;

public function __construct(AbstractClient $client)
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

public function getClient(): AbstractClient
public function getClient(): ClientInterface
{
return $this->client;
}

public function setClient(AbstractClient $client): void
public function setClient(ClientInterface $client): void
{
$this->client = $client;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Manager/AccessTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace League\Bundle\OAuth2ServerBundle\Manager;

use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface;

interface AccessTokenManagerInterface
{
public function find(string $identifier): ?AccessToken;
public function find(string $identifier): ?AccessTokenInterface;

public function save(AccessToken $accessToken): void;
public function save(AccessTokenInterface $accessToken): void;

public function clearExpired(): int;
}
6 changes: 3 additions & 3 deletions src/Manager/AuthorizationCodeManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace League\Bundle\OAuth2ServerBundle\Manager;

use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode;
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface;

interface AuthorizationCodeManagerInterface
{
public function find(string $identifier): ?AuthorizationCode;
public function find(string $identifier): ?AuthorizationCodeInterface;

public function save(AuthorizationCode $authCode): void;
public function save(AuthorizationCodeInterface $authCode): void;

public function clearExpired(): int;
}
10 changes: 5 additions & 5 deletions src/Manager/ClientManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace League\Bundle\OAuth2ServerBundle\Manager;

use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;

interface ClientManagerInterface
{
public function save(AbstractClient $client): void;
public function save(ClientInterface $client): void;

public function remove(AbstractClient $client): void;
public function remove(ClientInterface $client): void;

public function find(string $identifier): ?AbstractClient;
public function find(string $identifier): ?ClientInterface;

/**
* @return list<AbstractClient>
* @return list<ClientInterface>
*/
public function list(?ClientFilter $clientFilter): array;
}
5 changes: 3 additions & 2 deletions src/Manager/Doctrine/AccessTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\EntityManagerInterface;
use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface;

final class AccessTokenManager implements AccessTokenManagerInterface
{
Expand All @@ -24,7 +25,7 @@ public function __construct(EntityManagerInterface $entityManager, bool $persist
$this->persistAccessToken = $persistAccessToken;
}

public function find(string $identifier): ?AccessToken
public function find(string $identifier): ?AccessTokenInterface
{
if (!$this->persistAccessToken) {
return null;
Expand All @@ -33,7 +34,7 @@ public function find(string $identifier): ?AccessToken
return $this->entityManager->find(AccessToken::class, $identifier);
}

public function save(AccessToken $accessToken): void
public function save(AccessTokenInterface $accessToken): void
{
if (!$this->persistAccessToken) {
return;
Expand Down
5 changes: 3 additions & 2 deletions src/Manager/Doctrine/AuthorizationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\EntityManagerInterface;
use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode;
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface;

final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface
{
Expand All @@ -20,12 +21,12 @@ public function __construct(EntityManagerInterface $entityManager)
$this->entityManager = $entityManager;
}

public function find(string $identifier): ?AuthorizationCode
public function find(string $identifier): ?AuthorizationCodeInterface
{
return $this->entityManager->find(AuthorizationCode::class, $identifier);
}

public function save(AuthorizationCode $authCode): void
public function save(AuthorizationCodeInterface $authCode): void
{
$this->entityManager->persist($authCode);
$this->entityManager->flush();
Expand Down
7 changes: 4 additions & 3 deletions src/Manager/Doctrine/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
use League\Bundle\OAuth2ServerBundle\OAuth2Events;
use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
Expand Down Expand Up @@ -45,14 +46,14 @@ public function __construct(
$this->clientFqcn = $clientFqcn;
}

public function find(string $identifier): ?AbstractClient
public function find(string $identifier): ?ClientInterface
{
$repository = $this->entityManager->getRepository($this->clientFqcn);

return $repository->findOneBy(['identifier' => $identifier]);
}

public function save(AbstractClient $client): void
public function save(ClientInterface $client): void
{
$event = $this->dispatcher->dispatch(new PreSaveClientEvent($client), OAuth2Events::PRE_SAVE_CLIENT);
$client = $event->getClient();
Expand All @@ -61,7 +62,7 @@ public function save(AbstractClient $client): void
$this->entityManager->flush();
}

public function remove(AbstractClient $client): void
public function remove(ClientInterface $client): void
{
$this->entityManager->remove($client);
$this->entityManager->flush();
Expand Down
5 changes: 3 additions & 2 deletions src/Manager/Doctrine/RefreshTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\EntityManagerInterface;
use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\RefreshToken;
use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface;

final class RefreshTokenManager implements RefreshTokenManagerInterface
{
Expand All @@ -20,12 +21,12 @@ public function __construct(EntityManagerInterface $entityManager)
$this->entityManager = $entityManager;
}

public function find(string $identifier): ?RefreshToken
public function find(string $identifier): ?RefreshTokenInterface
{
return $this->entityManager->find(RefreshToken::class, $identifier);
}

public function save(RefreshToken $refreshToken): void
public function save(RefreshTokenInterface $refreshToken): void
{
$this->entityManager->persist($refreshToken);
$this->entityManager->flush();
Expand Down
10 changes: 5 additions & 5 deletions src/Manager/InMemory/AccessTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory;

use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface;

final class AccessTokenManager implements AccessTokenManagerInterface
{
/**
* @var array<string, AccessToken>
* @var array<string, AccessTokenInterface>
*/
private $accessTokens = [];

Expand All @@ -25,7 +25,7 @@ public function __construct(bool $persistAccessToken)
/**
* @psalm-mutation-free
*/
public function find(string $identifier): ?AccessToken
public function find(string $identifier): ?AccessTokenInterface
{
if (!$this->persistAccessToken) {
return null;
Expand All @@ -34,7 +34,7 @@ public function find(string $identifier): ?AccessToken
return $this->accessTokens[$identifier] ?? null;
}

public function save(AccessToken $accessToken): void
public function save(AccessTokenInterface $accessToken): void
{
if (!$this->persistAccessToken) {
return;
Expand All @@ -52,7 +52,7 @@ public function clearExpired(): int
$count = \count($this->accessTokens);

$now = new \DateTimeImmutable();
$this->accessTokens = array_filter($this->accessTokens, static function (AccessToken $accessToken) use ($now): bool {
$this->accessTokens = array_filter($this->accessTokens, static function (AccessTokenInterface $accessToken) use ($now): bool {
return $accessToken->getExpiry() >= $now;
});

Expand Down
10 changes: 5 additions & 5 deletions src/Manager/InMemory/AuthorizationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory;

use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode;
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface;

final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface
{
/**
* @var array<string, AuthorizationCode>
* @var array<string, AuthorizationCodeInterface>
*/
private $authorizationCodes = [];

/**
* @psalm-mutation-free
*/
public function find(string $identifier): ?AuthorizationCode
public function find(string $identifier): ?AuthorizationCodeInterface
{
return $this->authorizationCodes[$identifier] ?? null;
}

public function save(AuthorizationCode $authCode): void
public function save(AuthorizationCodeInterface $authCode): void
{
$this->authorizationCodes[$authCode->getIdentifier()] = $authCode;
}
Expand All @@ -32,7 +32,7 @@ public function clearExpired(): int
$count = \count($this->authorizationCodes);

$now = new \DateTimeImmutable();
$this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCode $authorizationCode) use ($now): bool {
$this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCodeInterface $authorizationCode) use ($now): bool {
return $authorizationCode->getExpiryDateTime() >= $now;
});

Expand Down
14 changes: 7 additions & 7 deletions src/Manager/InMemory/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\Bundle\OAuth2ServerBundle\Event\PreSaveClientEvent;
use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
use League\Bundle\OAuth2ServerBundle\OAuth2Events;
use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
Expand All @@ -17,7 +17,7 @@
final class ClientManager implements ClientManagerInterface
{
/**
* @var array<string, AbstractClient>
* @var array<string, ClientInterface>
*/
private $clients = [];

Expand All @@ -31,34 +31,34 @@ public function __construct(EventDispatcherInterface $dispatcher)
$this->dispatcher = $dispatcher;
}

public function find(string $identifier): ?AbstractClient
public function find(string $identifier): ?ClientInterface
{
return $this->clients[$identifier] ?? null;
}

public function save(AbstractClient $client): void
public function save(ClientInterface $client): void
{
$event = $this->dispatcher->dispatch(new PreSaveClientEvent($client), OAuth2Events::PRE_SAVE_CLIENT);
$client = $event->getClient();

$this->clients[$client->getIdentifier()] = $client;
}

public function remove(AbstractClient $client): void
public function remove(ClientInterface $client): void
{
unset($this->clients[$client->getIdentifier()]);
}

/**
* @return list<AbstractClient>
* @return list<ClientInterface>
*/
public function list(?ClientFilter $clientFilter): array
{
if (null === $clientFilter || !$clientFilter->hasFilters()) {
return array_values($this->clients);
}

return array_values(array_filter($this->clients, static function (AbstractClient $client) use ($clientFilter): bool {
return array_values(array_filter($this->clients, static function (ClientInterface $client) use ($clientFilter): bool {
if (!self::passesFilter($client->getGrants(), $clientFilter->getGrants())) {
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Manager/InMemory/RefreshTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory;

use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\RefreshToken;
use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface;

final class RefreshTokenManager implements RefreshTokenManagerInterface
{
/**
* @var array<string, RefreshToken>
* @var array<string, RefreshTokenInterface>
*/
private $refreshTokens = [];

/**
* @psalm-mutation-free
*/
public function find(string $identifier): ?RefreshToken
public function find(string $identifier): ?RefreshTokenInterface
{
return $this->refreshTokens[$identifier] ?? null;
}

public function save(RefreshToken $refreshToken): void
public function save(RefreshTokenInterface $refreshToken): void
{
$this->refreshTokens[$refreshToken->getIdentifier()] = $refreshToken;
}
Expand All @@ -32,7 +32,7 @@ public function clearExpired(): int
$count = \count($this->refreshTokens);

$now = new \DateTimeImmutable();
$this->refreshTokens = array_filter($this->refreshTokens, static function (RefreshToken $refreshToken) use ($now): bool {
$this->refreshTokens = array_filter($this->refreshTokens, static function (RefreshTokenInterface $refreshToken) use ($now): bool {
return $refreshToken->getExpiry() >= $now;
});

Expand Down
6 changes: 3 additions & 3 deletions src/Manager/RefreshTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace League\Bundle\OAuth2ServerBundle\Manager;

use League\Bundle\OAuth2ServerBundle\Model\RefreshToken;
use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface;

interface RefreshTokenManagerInterface
{
public function find(string $identifier): ?RefreshToken;
public function find(string $identifier): ?RefreshTokenInterface;

public function save(RefreshToken $refreshToken): void;
public function save(RefreshTokenInterface $refreshToken): void;

public function clearExpired(): int;
}
Loading

0 comments on commit 272facb

Please sign in to comment.