1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Manager; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use WBW\Bundle\CoreBundle\Exception\AlreadyRegisteredProviderException; |
16
|
|
|
use WBW\Bundle\CoreBundle\Provider\ProviderInterface; |
17
|
|
|
use WBW\Bundle\CoreBundle\Provider\QuoteProviderInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Quote manager. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\CoreBundle\Tests\Manager |
24
|
|
|
*/ |
25
|
|
|
class QuoteManager extends AbstractManager { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Service name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const SERVICE_NAME = "webeweb.core.manager.quote"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function addProvider(ProviderInterface $provider) { |
38
|
|
|
if (true === $this->contains($provider)) { |
39
|
|
|
throw new AlreadyRegisteredProviderException($provider); |
40
|
|
|
} |
41
|
|
|
$provider->init(); |
|
|
|
|
42
|
|
|
return parent::addProvider($provider); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function contains(ProviderInterface $provider) { |
49
|
|
|
if (false === ($provider instanceof QuoteProviderInterface)) { |
50
|
|
|
throw new InvalidArgumentException("The provider must implements QuoteProviderInterface"); |
51
|
|
|
} |
52
|
|
|
foreach ($this->getProviders() as $current) { |
53
|
|
|
if ($provider->getDomain() !== $current->getDomain()) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a quote provider. |
63
|
|
|
* |
64
|
|
|
* @param string $domain The domain. |
65
|
|
|
* @return ProviderInterface|null Returns the quote provider. |
66
|
|
|
*/ |
67
|
|
|
public function getQuoteProvider($domain) { |
68
|
|
|
foreach ($this->getProviders() as $current) { |
69
|
|
|
if ($domain === $current->getDomain()) { |
|
|
|
|
70
|
|
|
return $current; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: