TargetCollection::getFullName()   A
last analyzed

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 2
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-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\target;
12
13
use hiqdev\php\billing\Exception\UnknownEntityException;
14
15
/**
16
 * @see TargetInterface
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class TargetCollection implements TargetInterface
21
{
22
    /**
23
     * @var TargetInterface[]
24
     */
25
    protected $targets;
26
27
    protected $ids;
28
29
    protected $types;
30
31
    protected array $states = [];
32
33
    public function __construct(array $targets)
34
    {
35
        $ids = [];
36
        $types = [];
37
        $states = [];
38
        foreach ($targets as $target) {
39
            if ($target instanceof TargetInterface) {
40
                $ids[] = $target->getId();
41
                $types[] = $target->getType();
42
                $states[] = $target->getState();
43
                $this->targets[] = $target;
44
            } else {
45
                throw new UnknownEntityException('The target is invalid');
46
            }
47
        }
48
        $this->ids = array_unique(array_filter($ids));
49
        $this->types = array_unique(array_filter($types));
50
        $this->states = array_unique(array_filter($states));
51
    }
52
53
    public function add(TargetInterface $target)
54
    {
55
        $this->targets[] = $target;
56
        $this->ids[] = $target->getId();
57
        $this->types[] = $target->getType();
58
        $this->states[] = $target->getState();
59
        $this->ids = array_unique(array_filter($this->ids));
60
        $this->types = array_unique(array_filter($this->types));
61
        $this->states = array_unique(array_filter($this->states));
62
    }
63
64
    /**
65
     * @return int
66
     */
67
    public function getId()
68
    {
69
        return $this->getTarget()?->getId();
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function hasId()
76
    {
77
        return $this->getId() !== null;
78
    }
79
80
    public function getIds()
81
    {
82
        return $this->ids;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getType()
89
    {
90
        return $this->getTarget()?->getType();
91
    }
92
93
    public function getState(): ?string
94
    {
95
        return $this->getTarget()?->getState();
96
    }
97
98
    public function getTypes()
99
    {
100
        return $this->types;
101
    }
102
103
    public function getStates(): array
104
    {
105
        return $this->states;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getName()
112
    {
113
        return $this->getTarget()?->getName();
114
    }
115
116
    public function getLabel(): ?string
117
    {
118
        return $this->getTarget()?->getLabel();
119
    }
120
121
    public function getTarget()
122
    {
123
        return reset($this->targets);
124
    }
125
126
    public function getTargets()
127
    {
128
        return $this->targets;
129
    }
130
131
    public function getFullName(): string
132
    {
133
        return $this->getTarget()?->getFullName();
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getUniqueId()
140
    {
141
        return $this->getTarget()?->getUniqueId();
142
    }
143
144
    public function equals(TargetInterface $other): bool
145
    {
146
        return $this->getUniqueId() === $other->getUniqueId();
147
    }
148
149
    public function matches(TargetInterface $other): bool
150
    {
151
        return $this->checkMatches($other);
152
    }
153
154
    public function checkMatches(TargetInterface $other): bool
155
    {
156
        foreach ($this->targets as $target) {
157
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
158
                return true;
159
            }
160
        }
161
162
        return false;
163
    }
164
165
    public static function takeIds(TargetInterface $other)
166
    {
167
        return $other instanceof static ? $other->ids : [$other->getId()];
168
    }
169
170
    public static function takeTypes(TargetInterface $other)
171
    {
172
        return $other instanceof static ? $other->types : [$other->getType()];
173
    }
174
175
    public static function takeStates(TargetInterface $other): array
176
    {
177
        return $other instanceof static ? $other->states : [$other->getState()];
178
    }
179
180
    public function jsonSerialize(): array
181
    {
182
        return array_filter(get_object_vars($this));
183
    }
184
}
185