Completed
Pull Request — master (#5)
by Pavel
04:05
created

Router::__construct()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

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