Passed
Pull Request — master (#9)
by Pavel
11:27
created

ResourceMethodCollectionLoader::loadCollection()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 11
cts 12
cp 0.9167
rs 9.2
cc 4
eloc 10
nc 5
nop 0
crap 4.0092
1
<?php
2
3
namespace Bankiru\Api\Rpc\Routing;
4
5
final class ResourceMethodCollectionLoader implements MethodCollectionLoader
6
{
7
    /**
8
     * @var array
9
     */
10
    private $resources;
11
    /**
12
     * @var LoaderResolverInterface
13
     */
14
    private $resolver;
15
    /**
16
     * @var array
17
     */
18
    private $context;
19
20
    /**
21
     * ResourceMethodCollectionLoader constructor.
22
     *
23
     * @param LoaderResolverInterface $resolver
24
     * @param array                   $resources
25
     * @param array                   $context
26
     */
27 11
    public function __construct(LoaderResolverInterface $resolver, array $resources, array $context = [])
28
    {
29 11
        $this->resources = $resources;
30 11
        $this->resolver  = $resolver;
31 11
        $this->context   = $context;
32 11
    }
33
34
    /** {@inheritdoc} */
35 2
    public function loadCollection()
36
    {
37 2
        $collection = new MethodCollection();
38
39 2
        foreach ($this->resources as $resource) {
40 2
            $loader = $this->resolver->resolve($resource);
41
42 2
            if (false === $loader) {
43
                throw new \RuntimeException(sprintf('Could not resolve loader for resource "%s"', $resource));
44
            }
45
46 2
            $collection->addCollection($loader->load($resource));
47 2
        }
48
49 2
        foreach ($this->context as $item) {
50 2
            $collection->addContext($item);
51 2
        }
52
53 2
        return $collection;
54
    }
55
}
56