vendor/league/oauth2-server-bundle/src/Manager/Doctrine/ClientManager.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\Bundle\OAuth2ServerBundle\Manager\Doctrine;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use League\Bundle\OAuth2ServerBundle\Event\PreSaveClientEvent;
  6. use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter;
  7. use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
  8. use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
  9. use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
  10. use League\Bundle\OAuth2ServerBundle\OAuth2Events;
  11. use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
  12. use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
  13. use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. final class ClientManager implements ClientManagerInterface
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $entityManager;
  21.     /**
  22.      * @var class-string<AbstractClient>
  23.      */
  24.     private $clientFqcn;
  25.     /**
  26.      * @var EventDispatcherInterface
  27.      */
  28.     private $dispatcher;
  29.     /**
  30.      * @param class-string<AbstractClient> $clientFqcn
  31.      */
  32.     public function __construct(
  33.         EntityManagerInterface $entityManager,
  34.         EventDispatcherInterface $dispatcher,
  35.         string $clientFqcn
  36.     ) {
  37.         $this->entityManager $entityManager;
  38.         $this->dispatcher $dispatcher;
  39.         $this->clientFqcn $clientFqcn;
  40.     }
  41.     public function find(string $identifier): ?ClientInterface
  42.     {
  43.         $repository $this->entityManager->getRepository($this->clientFqcn);
  44.         return $repository->findOneBy(['identifier' => $identifier]);
  45.     }
  46.     public function save(ClientInterface $client): void
  47.     {
  48.         $event $this->dispatcher->dispatch(new PreSaveClientEvent($client), OAuth2Events::PRE_SAVE_CLIENT);
  49.         $client $event->getClient();
  50.         $this->entityManager->persist($client);
  51.         $this->entityManager->flush();
  52.     }
  53.     public function remove(ClientInterface $client): void
  54.     {
  55.         $this->entityManager->remove($client);
  56.         $this->entityManager->flush();
  57.     }
  58.     /**
  59.      * @return list<AbstractClient>
  60.      */
  61.     public function list(?ClientFilter $clientFilter): array
  62.     {
  63.         $repository $this->entityManager->getRepository($this->clientFqcn);
  64.         $criteria self::filterToCriteria($clientFilter);
  65.         /** @var list<AbstractClient> */
  66.         return $repository->findBy($criteria);
  67.     }
  68.     /**
  69.      * @return array{grants?: list<Grant>, redirect_uris?: list<RedirectUri>, scopes?: list<Scope>}
  70.      */
  71.     private static function filterToCriteria(?ClientFilter $clientFilter): array
  72.     {
  73.         if (null === $clientFilter || false === $clientFilter->hasFilters()) {
  74.             return [];
  75.         }
  76.         $criteria = [];
  77.         $grants $clientFilter->getGrants();
  78.         if ($grants) {
  79.             $criteria['grants'] = $grants;
  80.         }
  81.         $redirectUris $clientFilter->getRedirectUris();
  82.         if ($redirectUris) {
  83.             $criteria['redirect_uris'] = $redirectUris;
  84.         }
  85.         $scopes $clientFilter->getScopes();
  86.         if ($scopes) {
  87.             $criteria['scopes'] = $scopes;
  88.         }
  89.         return $criteria;
  90.     }
  91. }