ChainedResources   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 41
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiResources() 0 7 2
A __construct() 0 13 5
1
<?php
2
namespace  W2w\Lib\Apie\Core\Resources;
3
4
use W2w\Lib\Apie\Exceptions\BadConfigurationException;
5
6
/**
7
 * Merge multiple ApiResourcesInterface results in one.
8
 */
9
class ChainedResources implements ApiResourcesInterface
10
{
11
    /**
12
     * @var string[]
13
     */
14
    private $static = [];
15
16
    /**
17
     * @var ApiResourcesInterface[]
18
     */
19
    private $chainedResources = [];
20
21
    /**
22
     * @param (string|ApiResourcesInterface)[] $resources
23
     */
24
    public function __construct(array $resources)
25
    {
26
        foreach ($resources as $resource)
27
        {
28
            if (gettype($resource) === 'string' && class_exists($resource)) {
29
                $this->static[] = $resource;
30
                continue;
31
            }
32
            if ($resource instanceof ApiResourcesInterface) {
33
                $this->chainedResources[] = $resource;
34
                continue;
35
            }
36
            throw new BadConfigurationException('I expect to get a list of classes or ApiResourcesInterface instances.');
37
        }
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function getApiResources(): array
44
    {
45
        $res = $this->static;
46
        foreach ($this->chainedResources as $chainedResource) {
47
            $res = array_merge($res, $chainedResource->getApiResources());
48
        }
49
        return array_values(array_unique($res));
50
    }
51
}
52