Passed
Branch master (117216)
by Pavel
08:06
created

AbstractTargetDefinition::getCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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