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

ResourceMethodCollectionLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
ccs 16
cts 17
cp 0.9412
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A loadCollection() 0 20 4
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