Skip to content

Commit f70fefe

Browse files
authored
Add named InvalidOAuth2ClientException (#457)
1 parent cdc238c commit f70fefe

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/Client/ClientRegistry.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace KnpU\OAuth2ClientBundle\Client;
1212

13+
use KnpU\OAuth2ClientBundle\DependencyInjection\InvalidOAuth2ClientException;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
1415

1516
class ClientRegistry
@@ -33,19 +34,21 @@ public function __construct(ContainerInterface $container, array $serviceMap)
3334
* @param string $key
3435
*
3536
* @return OAuth2ClientInterface
37+
*
38+
* @throws InvalidOAuth2ClientException
3639
*/
3740
public function getClient($key)
3841
{
3942
if (isset($this->serviceMap[$key])) {
4043
$client = $this->container->get($this->serviceMap[$key]);
4144
if (!$client instanceof OAuth2ClientInterface) {
42-
throw new \InvalidArgumentException(\sprintf('Somehow the "%s" client is not an instance of OAuth2ClientInterface.', $key));
45+
throw new InvalidOAuth2ClientException(\sprintf('Somehow the "%s" client is not an instance of OAuth2ClientInterface.', $key));
4346
}
4447

4548
return $client;
4649
}
4750

48-
throw new \InvalidArgumentException(\sprintf('There is no OAuth2 client called "%s". Available are: %s', $key, implode(', ', array_keys($this->serviceMap))));
51+
throw new InvalidOAuth2ClientException(\sprintf('There is no OAuth2 client called "%s". Available are: %s', $key, implode(', ', array_keys($this->serviceMap))));
4952
}
5053

5154
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* OAuth2 Client Bundle
5+
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace KnpU\OAuth2ClientBundle\DependencyInjection;
12+
13+
use KnpU\OAuth2ClientBundle\Exception\OAuth2ClientException;
14+
15+
class InvalidOAuth2ClientException extends \InvalidArgumentException implements OAuth2ClientException
16+
{
17+
}

tests/Client/ClientRegistryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
1414
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface;
15+
use KnpU\OAuth2ClientBundle\DependencyInjection\InvalidOAuth2ClientException;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718

@@ -51,7 +52,7 @@ public function testShouldThrowExceptionIfClientDoesNotExist()
5152

5253
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);
5354

54-
$this->expectException(\InvalidArgumentException::class);
55+
$this->expectException(InvalidOAuth2ClientException::class);
5556

5657
$testClientRegistry->getClient("unknownClient");
5758
}
@@ -69,7 +70,7 @@ public function testShouldThrowExceptionIfClientExistsButNotOAuth2Client()
6970

7071
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);
7172

72-
$this->expectException(\InvalidArgumentException::class);
73+
$this->expectException(InvalidOAuth2ClientException::class);
7374

7475
$testClientRegistry->getClient("invalid");
7576
}

0 commit comments

Comments
 (0)