Completed
Push — master ( 1f3fa8...7d4b61 )
by Mike
03:08
created

ProviderChain::addProviders()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 7
nop 1
dl 16
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Data;
4
5
use Sugarcrm\UpgradeSpec\Data\Provider\ProviderInterface;
6
7
class ProviderChain
8
{
9
    /**
10
     * @var array
11
     */
12
    private $providers = [];
13
14
    /**
15
     * ProviderChain constructor.
16
     *
17
     * @param mixed $providers
18
     */
19
    public function __construct($providers)
20
    {
21
        $this->addProviders($providers);
22
    }
23
24
    /**
25
     * Adds providers to the chain
26
     *
27
     * @param mixed $providers
28
     */
29 View Code Duplication
    public function addProviders($providers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        if (!is_array($providers) && !$providers instanceof \Traversable) {
32
            throw new \InvalidArgumentException(sprintf('Argument is not traversable: %s', $providers));
33
        }
34
35
        $providers = is_array($providers) ? $providers : iterator_to_array($providers);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $providers. This often makes code more readable.
Loading history...
36
37
        foreach ($providers as $provider) {
38
            if (!is_a($provider, ProviderInterface::class)) {
39
                throw new \InvalidArgumentException('ProviderChain expects ProviderInterface[]');
40
            }
41
        }
42
43
        $this->providers = array_merge($this->providers, $providers);
44
    }
45
46
    /**
47
     * @param $name
48
     * @param $arguments
49
     *
50
     * @return mixed
51
     */
52
    public function __call($name, $arguments)
53
    {
54
        foreach ($this->providers as $provider) {
55
            if (method_exists($provider, $name)) {
56
                return call_user_func_array([$provider, $name], $arguments);
57
            }
58
        }
59
60
        throw new \RuntimeException(sprintf('There is no provider with method: %s', $name));
61
    }
62
}
63