Completed
Push — master ( 66f8df...27707a )
by Andrii
02:45
created

TargetCollection::matches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
    public function getFullName(): string
104
    {
105
        return $this->getTarget()->getFullName();
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getUniqueId()
112
    {
113
        return $this->getTarget()->getUniqueId();
114
    }
115
116
    public function equals(TargetInterface $other): bool
117
    {
118
        return $this->getUniqueId() === $other->getUniqueId();
119
    }
120 1
121
    /**
122 1
     * @param TargetInterface $other
123
     * @return bool
124
     */
125 1
    public function matches(TargetInterface $other): bool
126
    {
127 1
        return $this->checkMatches($other);
128 1
    }
129 1
130
    public function checkMatches(TargetInterface $other): bool
131
    {
132
        foreach ($this->targets as $target) {
133 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
134
                return true;
135
            }
136
        }
137
138
        return false;
139
    }
140
141
    public static function takeIds(TargetInterface $other)
142
    {
143
        return $other instanceof static ? $other->ids : [$other->getId()];
144
    }
145
146
    public static function takeTypes(TargetInterface $other)
147
    {
148
        return $other instanceof static ? $other->types : [$other->getType()];
149
    }
150
151
    public function jsonSerialize()
152
    {
153
        return array_filter(get_object_vars($this));
154
    }
155
}
156