Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoupled models from managers #260

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Manager/AccessTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Trikoder\Bundle\OAuth2Bundle\Manager;

use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessTokenInterface;

/**
* @method int clearRevoked() not defining this method is deprecated since version 3.2
*/
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 Manager/AuthorizationCodeManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Trikoder\Bundle\OAuth2Bundle\Manager;

use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCodeInterface;

/**
* @method int clearRevoked() not defining this method is deprecated since version 3.2
*/
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;
}
24 changes: 12 additions & 12 deletions Manager/ClientFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

namespace Trikoder\Bundle\OAuth2Bundle\Manager;

use Trikoder\Bundle\OAuth2Bundle\Model\Grant;
use Trikoder\Bundle\OAuth2Bundle\Model\RedirectUri;
use Trikoder\Bundle\OAuth2Bundle\Model\Scope;
use Trikoder\Bundle\OAuth2Bundle\Model\GrantInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\RedirectUriInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\ScopeInterface;

final class ClientFilter
{
/**
* @var Grant[]
* @var GrantInterface[]
*/
private $grants = [];

/**
* @var RedirectUri[]
* @var RedirectUriInterface[]
*/
private $redirectUris = [];

/**
* @var Scope[]
* @var ScopeInterface[]
*/
private $scopes = [];

Expand All @@ -30,17 +30,17 @@ public static function create(): self
return new static();
}

public function addGrantCriteria(Grant ...$grants): self
public function addGrantCriteria(GrantInterface ...$grants): self
{
return $this->addCriteria($this->grants, ...$grants);
}

public function addRedirectUriCriteria(RedirectUri ...$redirectUris): self
public function addRedirectUriCriteria(RedirectUriInterface ...$redirectUris): self
{
return $this->addCriteria($this->redirectUris, ...$redirectUris);
}

public function addScopeCriteria(Scope ...$scopes): self
public function addScopeCriteria(ScopeInterface ...$scopes): self
{
return $this->addCriteria($this->scopes, ...$scopes);
}
Expand All @@ -57,23 +57,23 @@ private function addCriteria(&$field, ...$values): self
}

/**
* @return Grant[]
* @return GrantInterface[]
*/
public function getGrants(): array
{
return $this->grants;
}

/**
* @return RedirectUri[]
* @return RedirectUriInterface[]
*/
public function getRedirectUris(): array
{
return $this->redirectUris;
}

/**
* @return Scope[]
* @return ScopeInterface[]
*/
public function getScopes(): array
{
Expand Down
8 changes: 4 additions & 4 deletions Manager/ClientManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace Trikoder\Bundle\OAuth2Bundle\Manager;

use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\ClientInterface;
use Trikoder\Bundle\OAuth2Bundle\Service\ClientFinderInterface;

interface ClientManagerInterface extends ClientFinderInterface
{
public function save(Client $client): void;
public function save(ClientInterface $client): void;

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

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

final class AccessTokenManager implements AccessTokenManagerInterface
{
Expand All @@ -24,15 +25,15 @@ public function __construct(EntityManagerInterface $entityManager)
/**
* {@inheritdoc}
*/
public function find(string $identifier): ?AccessToken
public function find(string $identifier): ?AccessTokenInterface
{
return $this->entityManager->find(AccessToken::class, $identifier);
}

/**
* {@inheritdoc}
*/
public function save(AccessToken $accessToken): void
public function save(AccessTokenInterface $accessToken): void
{
$this->entityManager->persist($accessToken);
$this->entityManager->flush();
Expand Down
5 changes: 3 additions & 2 deletions Manager/Doctrine/AuthorizationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\AuthorizationCodeManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCodeInterface;

final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface
{
Expand All @@ -24,15 +25,15 @@ public function __construct(EntityManagerInterface $entityManager)
/**
* {@inheritdoc}
*/
public function find(string $identifier): ?AuthorizationCode
public function find(string $identifier): ?AuthorizationCodeInterface
{
return $this->entityManager->find(AuthorizationCode::class, $identifier);
}

/**
* {@inheritdoc}
*/
public function save(AuthorizationCode $authorizationCode): void
public function save(AuthorizationCodeInterface $authorizationCode): void
{
$this->entityManager->persist($authorizationCode);
$this->entityManager->flush();
Expand Down
7 changes: 4 additions & 3 deletions Manager/Doctrine/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientFilter;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\ClientInterface;

final class ClientManager implements ClientManagerInterface
{
Expand All @@ -24,15 +25,15 @@ public function __construct(EntityManagerInterface $entityManager)
/**
* {@inheritdoc}
*/
public function find(string $identifier): ?Client
public function find(string $identifier): ?ClientInterface
{
return $this->entityManager->find(Client::class, $identifier);
}

/**
* {@inheritdoc}
*/
public function save(Client $client): void
public function save(ClientInterface $client): void
{
$this->entityManager->persist($client);
$this->entityManager->flush();
Expand All @@ -41,7 +42,7 @@ public function save(Client $client): void
/**
* {@inheritdoc}
*/
public function remove(Client $client): void
public function remove(ClientInterface $client): void
{
$this->entityManager->remove($client);
$this->entityManager->flush();
Expand Down
5 changes: 3 additions & 2 deletions Manager/Doctrine/RefreshTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\RefreshTokenManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshTokenInterface;

final class RefreshTokenManager implements RefreshTokenManagerInterface
{
Expand All @@ -24,15 +25,15 @@ public function __construct(EntityManagerInterface $entityManager)
/**
* {@inheritdoc}
*/
public function find(string $identifier): ?RefreshToken
public function find(string $identifier): ?RefreshTokenInterface
{
return $this->entityManager->find(RefreshToken::class, $identifier);
}

/**
* {@inheritdoc}
*/
public function save(RefreshToken $refreshToken): void
public function save(RefreshTokenInterface $refreshToken): void
{
$this->entityManager->persist($refreshToken);
$this->entityManager->flush();
Expand Down
12 changes: 6 additions & 6 deletions Manager/InMemory/AccessTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

use DateTimeImmutable;
use Trikoder\Bundle\OAuth2Bundle\Manager\AccessTokenManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessTokenInterface;

final class AccessTokenManager implements AccessTokenManagerInterface
{
/**
* @var AccessToken[]
* @var AccessTokenInterface[]
*/
private $accessTokens = [];

/**
* {@inheritdoc}
*/
public function find(string $identifier): ?AccessToken
public function find(string $identifier): ?AccessTokenInterface
{
return $this->accessTokens[$identifier] ?? null;
}

/**
* {@inheritdoc}
*/
public function save(AccessToken $accessToken): void
public function save(AccessTokenInterface $accessToken): void
{
$this->accessTokens[$accessToken->getIdentifier()] = $accessToken;
}
Expand All @@ -36,7 +36,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 All @@ -47,7 +47,7 @@ public function clearRevoked(): int
{
$count = \count($this->accessTokens);

$this->accessTokens = array_filter($this->accessTokens, static function (AccessToken $accessToken): bool {
$this->accessTokens = array_filter($this->accessTokens, static function (AccessTokenInterface $accessToken): bool {
return !$accessToken->isRevoked();
});

Expand Down
9 changes: 5 additions & 4 deletions Manager/InMemory/AuthorizationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use Trikoder\Bundle\OAuth2Bundle\Manager\AuthorizationCodeManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCodeInterface;

final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface
{
Expand All @@ -15,12 +16,12 @@ final class AuthorizationCodeManager implements AuthorizationCodeManagerInterfac
*/
private $authorizationCodes = [];

public function find(string $identifier): ?AuthorizationCode
public function find(string $identifier): ?AuthorizationCodeInterface
{
return $this->authorizationCodes[$identifier] ?? null;
}

public function save(AuthorizationCode $authorizationCode): void
public function save(AuthorizationCodeInterface $authorizationCode): void
{
$this->authorizationCodes[$authorizationCode->getIdentifier()] = $authorizationCode;
}
Expand All @@ -30,7 +31,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 All @@ -41,7 +42,7 @@ public function clearRevoked(): int
{
$count = \count($this->authorizationCodes);

$this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCode $authorizationCode): bool {
$this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCodeInterface $authorizationCode): bool {
return !$authorizationCode->isRevoked();
});

Expand Down
12 changes: 6 additions & 6 deletions Manager/InMemory/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@

use Trikoder\Bundle\OAuth2Bundle\Manager\ClientFilter;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\ClientInterface;

final class ClientManager implements ClientManagerInterface
{
/**
* @var Client[]
* @var ClientInterface[]
*/
private $clients = [];

/**
* {@inheritdoc}
*/
public function find(string $identifier): ?Client
public function find(string $identifier): ?ClientInterface
{
return $this->clients[$identifier] ?? null;
}

/**
* {@inheritdoc}
*/
public function save(Client $client): void
public function save(ClientInterface $client): void
{
$this->clients[$client->getIdentifier()] = $client;
}

/**
* {@inheritdoc}
*/
public function remove(Client $client): void
public function remove(ClientInterface $client): void
{
unset($this->clients[$client->getIdentifier()]);
}
Expand All @@ -48,7 +48,7 @@ public function list(?ClientFilter $clientFilter): array
return $this->clients;
}

return array_filter($this->clients, static function (Client $client) use ($clientFilter): bool {
return array_filter($this->clients, static function (ClientInterface $client) use ($clientFilter): bool {
$grantsPassed = self::passesFilter($client->getGrants(), $clientFilter->getGrants());
$scopesPassed = self::passesFilter($client->getScopes(), $clientFilter->getScopes());
$redirectUrisPassed = self::passesFilter($client->getRedirectUris(), $clientFilter->getRedirectUris());
Expand Down
Loading