Completed
Push — master ( c5de01...c9c19d )
by
unknown
07:34 queued 05:10
created

Resolver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Coduo\TuTu\Config;
4
5
use Coduo\TuTu\Config\Loader\Loader;
6
use Coduo\TuTu\Request\MatchingPolicy;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class Resolver
10
{
11
    /**
12
     * @var Element[]|array
13
     */
14
    protected $configs;
15
16
    /**
17
     * @var \Coduo\TuTu\Request\MatchingPolicy
18
     */
19
    private $matchingPolicy;
20
21
    /**
22
     * @param Loader $loader
23
     * @param MatchingPolicy $matchingPolicy
24
     */
25
    public function __construct(Loader $loader, MatchingPolicy $matchingPolicy)
26
    {
27
        $this->configs = [];
28
        foreach ($loader->getResponsesArray() as $responseArrayConfig) {
29
            $this->configs[] = Element::fromArray($responseArrayConfig);
30
        }
31
32
        $this->matchingPolicy = $matchingPolicy;
33
    }
34
35
    /**
36
     * @param Request $request
37
     * @return Element|null
38
     */
39
    public function resolveConfigElement(Request $request)
40
    {
41
        foreach ($this->configs as $config) {
42
            if ($this->matchingPolicy->match($request, $config)) {
43
                return $config;
44
            }
45
        }
46
47
        return null;
48
    }
49
}
50