Passed
Push — master ( 364662...9798b7 )
by Andrii
02:56
created

TargetCollection::getUniqueId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
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
    public function __construct(array $targets)
30
    {
31
        $this->targets = $targets;
32
        $ids = [];
33
        $types = [];
34
        foreach ($targets as $target) {
35
            $ids[] = $target->getId();
36
            $types[] = $target->getType();
37
        }
38
        $this->ids = array_unique(array_filter($ids));
39
        $this->types = array_unique(array_filter($types));
40
    }
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
    public function getId()
55
    {
56
        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
    public function getType()
76
    {
77
        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
    public function getTarget()
94
    {
95
        return reset($this->targets);
96
    }
97
98
    public function getTargets()
99
    {
100
        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
121
    public function matches(TargetInterface $other): bool
122
    {
123
        return $this->checkMatches($other);
124
    }
125
126
    public function checkMatches(TargetInterface $other): bool
127
    {
128
        foreach ($this->targets as $target) {
129
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
130
                return true;
131
            }
132
        }
133
134
        return false;
135
    }
136
137
    public static function takeIds(TargetInterface $other)
138
    {
139
        return $other instanceof static ? $other->ids : [$other->getId()];
140
    }
141
142
    public static function takeTypes(TargetInterface $other)
143
    {
144
        return $other instanceof static ? $other->types : [$other->getType()];
145
    }
146
147
    public function jsonSerialize()
148
    {
149
        return array_filter(get_object_vars($this));
150
    }
151
}
152