Completed
Push — master ( b3f5fe...46cf75 )
by Pavel
02:54
created

TargetDefinition::getCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Bankiru\Seo\Entity;
4
5
use Bankiru\Seo\ConditionInterface;
6
use Bankiru\Seo\DestinationInterface;
7
use Bankiru\Seo\SourceInterface;
8
use Bankiru\Seo\TargetDefinitionInterface;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Common\Collections\Collection;
11
12
class TargetDefinition implements TargetDefinitionInterface
13
{
14
    /**
15
     * @var Collection|ConditionInterface[]
16
     */
17
    protected $conditions;
18
    /** @var  string */
19
    protected $route;
20
21
    /**
22
     * TargetDefinition constructor.
23
     *
24
     * @param string $route
25
     */
26 7
    public function __construct($route)
27
    {
28 7
        $this->route      = $route;
29 7
        $this->conditions = new ArrayCollection();
30 7
    }
31
32
    /** {@inheritdoc} */
33 7
    public function match(DestinationInterface $destination)
34
    {
35 7
        if ($destination->getRoute() !== $this->getRoute()) {
36
            return null;
37
        }
38
39 7
        $result = 0;
40 7
        foreach ($this->conditions as $code => $condition) {
41 4
            $payload = $destination->resolve($code);
42 4
            $score   = $condition->match($payload);
43 4
            if ($score === null) {
44 3
                return null;
45
            }
46 2
            $result += $score;
47 5
        }
48
49 4
        return $result;
50
    }
51
52
    /**
53
     * Get conditions indexed by code
54
     *
55
     * @return ConditionInterface[]
56
     */
57
    public function getConditions()
58
    {
59
        return $this->conditions->toArray();
60
    }
61
62
    /** {@inheritdoc} */
63
    public function setCondition($code, ConditionInterface $condition)
64
    {
65
        if ($condition instanceof PermissiveCondition) {
66
            return;
67
        }
68
69
        $this->conditions->set($code, $condition);
70
    }
71
72
    /** {@inheritdoc} */
73 1
    public function getCondition($code)
74
    {
75 1
        if ($this->conditions->containsKey($code)) {
76
            return $this->conditions->get($code);
77
        }
78
79 1
        return new PermissiveCondition();
80
    }
81
82
    /**
83
     * @param SourceInterface[] $sources
84
     *
85
     * @return int
86
     */
87 1
    public function count(array $sources)
88
    {
89 1
        $count         = 1;
90 1
        $uniqueSources = [];
91 1
        foreach ($sources as $code => $source) {
92 1
            $source->withCondition($this->getCondition($code));
93 1
            $uniqueSources[spl_object_hash($source)] = $source;
94 1
        }
95
96 1
        foreach ($uniqueSources as $source) {
97 1
            $count *= $source->count();
98 1
        }
99
100 1
        return $count;
101
    }
102
103
    /** {@inheritdoc} */
104 11
    public function getRoute()
105
    {
106 11
        return $this->route;
107
    }
108
}
109