vendor/knpuniversity/oauth2-client-bundle/src/Client/ClientRegistry.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3. * OAuth2 Client Bundle
  4. * Copyright (c) KnpUniversity <http://knpuniversity.com/>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace KnpU\OAuth2ClientBundle\Client;
  10. use KnpU\OAuth2ClientBundle\DependencyInjection\InvalidOAuth2ClientException;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. class ClientRegistry
  13. {
  14. private ContainerInterface $container;
  15. private array $serviceMap;
  16. /**
  17. * ClientRegistry constructor.
  18. */
  19. public function __construct(ContainerInterface $container, array $serviceMap)
  20. {
  21. $this->container = $container;
  22. $this->serviceMap = $serviceMap;
  23. }
  24. /**
  25. * Easy accessor for client objects.
  26. *
  27. * @param string $key
  28. *
  29. * @return OAuth2ClientInterface
  30. *
  31. * @throws InvalidOAuth2ClientException
  32. */
  33. public function getClient($key)
  34. {
  35. if (isset($this->serviceMap[$key])) {
  36. $client = $this->container->get($this->serviceMap[$key]);
  37. if (!$client instanceof OAuth2ClientInterface) {
  38. throw new InvalidOAuth2ClientException(\sprintf('Somehow the "%s" client is not an instance of OAuth2ClientInterface.', $key));
  39. }
  40. return $client;
  41. }
  42. throw new InvalidOAuth2ClientException(\sprintf('There is no OAuth2 client called "%s". Available are: %s', $key, implode(', ', array_keys($this->serviceMap))));
  43. }
  44. /**
  45. * Returns all enabled client keys.
  46. *
  47. * @return array
  48. */
  49. public function getEnabledClientKeys()
  50. {
  51. return array_keys($this->serviceMap);
  52. }
  53. }