Completed
Push — master ( a5a4c6...8debe9 )
by Dmitry
02:42
created

TargetCollection::checkMatches()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\target;
12
13
/**
14
 * @see TargetInterface
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class TargetCollection implements TargetInterface
19
{
20
    /**
21
     * @var TargetInterface[]
22
     */
23
    protected $targets;
24
25
    protected $ids;
26
27
    protected $types;
28
29 3
    public function __construct(array $targets)
30
    {
31 3
        $this->targets = $targets;
32 3
        $ids = [];
33 3
        $types = [];
34 3
        foreach ($targets as $target) {
35 3
            $ids[] = $target->getId();
36 3
            $types[] = $target->getType();
37
        }
38 3
        $this->ids = array_unique(array_filter($ids));
39 3
        $this->types = array_unique(array_filter($types));
40 3
    }
41
42
    public function add(TargetInterface $target)
43
    {
44
        $this->targets[] = $target;
45
        $this->ids[] = $target->getId();
46
        $this->types[] = $target->getType();
47
        $this->ids = array_unique(array_filter($this->ids));
48
        $this->types = array_unique(array_filter($this->types));
49
    }
50
51
    /**
52
     * @return int
53
     */
54 1
    public function getId()
55
    {
56 1
        return $this->getTarget()->getId();
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function hasId()
63
    {
64
        return $this->getId() !== null;
65
    }
66
67
    public function getIds()
68
    {
69
        return $this->ids;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getType()
76
    {
77 1
        return $this->getTarget()->getType();
78
    }
79
80
    public function getTypes()
81
    {
82
        return $this->types;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getName()
89
    {
90
        return $this->getTarget()->getName();
91
    }
92
93 1
    public function getTarget()
94
    {
95 1
        return reset($this->targets);
96
    }
97
98 1
    public function getTargets()
99
    {
100 1
        return $this->targets;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getUniqueId()
107
    {
108
        return implode(':', array_filter([$this->getType(), $this->getId()]));
109
    }
110
111
    public function equals(TargetInterface $other): bool
112
    {
113
        return $this->getUniqueId() === $other->getUniqueId();
114
    }
115
116
    /**
117
     * @param TargetInterface $other
118
     * @return bool
119
     */
120 1
    public function matches(TargetInterface $other): bool
121
    {
122 1
        return $this->checkMatches($other);
123
    }
124
125 1
    public function checkMatches(TargetInterface $other): bool
126
    {
127 1
        foreach ($this->targets as $target) {
128 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
129 1
                return true;
130
            }
131
        }
132
133 1
        return false;
134
    }
135
136
    public static function takeIds(TargetInterface $other)
137
    {
138
        return $other instanceof static ? $other->ids : [$other->getId()];
139
    }
140
141
    public static function takeTypes(TargetInterface $other)
142
    {
143
        return $other instanceof static ? $other->types : [$other->getType()];
144
    }
145
146
    public function jsonSerialize()
147
    {
148
        return get_object_vars($this);
149
    }
150
}
151