Passed
Pull Request — master (#9)
by Pavel
12:36
created

ResourceMethodCollectionLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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