Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.10% covered (warning)
56.10%
23 / 41
55.56% covered (warning)
55.56%
10 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
Duplication
56.10% covered (warning)
56.10%
23 / 41
55.56% covered (warning)
55.56%
10 / 18
88.69
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSourceCollectivity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSourceCollectivity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataIds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addDataId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeDataId
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getTargetCollectivities
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getDuplicatedObjects
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDuplicatedObjectOfCollectivityAndOriginId
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
 addDuplicatedObjet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeDuplicatedObject
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 __toString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Admin\Model;
6
7use App\Application\Traits\Model\HistoryTrait;
8use App\Domain\Admin\Dictionary\DuplicationTypeDictionary;
9use App\Domain\Reporting\Model\LoggableSubject;
10use App\Domain\User\Model\Collectivity;
11use Doctrine\ORM\PersistentCollection;
12use Ramsey\Uuid\Uuid;
13use Ramsey\Uuid\UuidInterface;
14
15class Duplication implements LoggableSubject
16{
17    use HistoryTrait;
18
19    /**
20     * @var UuidInterface
21     */
22    private $id;
23
24    /**
25     * @var string
26     */
27    private $type;
28
29    /**
30     * @var Collectivity
31     */
32    private $sourceCollectivity;
33
34    /**
35     * @var string[]
36     */
37    private $dataIds;
38
39    /**
40     * @var object[]
41     */
42    private $data;
43
44    /**
45     * @var Collectivity[]
46     */
47    private $targetCollectivities;
48
49    /**
50     * @var DuplicatedObject[]|PersistentCollection
51     */
52    private $duplicatedObjects;
53
54    /**
55     * DuplicationDTO constructor.
56     *
57     * @param Collectivity[] $targetCollectivities
58     *
59     * @throws \Exception
60     */
61    public function __construct(string $type, Collectivity $sourceCollectivity, array $targetCollectivities = [], array $duplicatedObjects = [])
62    {
63        if (!\in_array($type, DuplicationTypeDictionary::getDataKeys())) {
64            throw new \RuntimeException('Provided type is not an available one. Please check keys in ' . DuplicationTypeDictionary::class . ' class.');
65        }
66        $this->id                   = Uuid::uuid4();
67        $this->type                 = $type;
68        $this->sourceCollectivity   = $sourceCollectivity;
69        $this->dataIds              = [];
70        $this->data                 = [];
71        $this->targetCollectivities = $targetCollectivities;
72        $this->duplicatedObjects    = $duplicatedObjects;
73    }
74
75    public function getId(): UuidInterface
76    {
77        return $this->id;
78    }
79
80    public function getType(): string
81    {
82        return $this->type;
83    }
84
85    public function setType(string $type): void
86    {
87        $this->type = $type;
88    }
89
90    public function getSourceCollectivity(): Collectivity
91    {
92        return $this->sourceCollectivity;
93    }
94
95    public function setSourceCollectivity(Collectivity $sourceCollectivity): void
96    {
97        $this->sourceCollectivity = $sourceCollectivity;
98    }
99
100    /**
101     * @return string[]
102     */
103    public function getDataIds(): array
104    {
105        return $this->dataIds;
106    }
107
108    public function addDataId(string $dataId): void
109    {
110        $this->dataIds[] = $dataId;
111    }
112
113    public function removeDataId(string $dataId): void
114    {
115        $key = \array_search($dataId, $this->dataIds, true);
116
117        if (false === $key) {
118            return;
119        }
120
121        unset($this->dataIds[$key]);
122    }
123
124    public function getData(): array
125    {
126        return $this->data;
127    }
128
129    public function addData($data): void
130    {
131        $this->data[] = $data;
132    }
133
134    public function removeData($data): void
135    {
136        $key = \array_search($data, $this->data, true);
137
138        if (false === $key) {
139            return;
140        }
141
142        unset($this->data[$key]);
143    }
144
145    /**
146     * @return Collectivity[]
147     */
148    public function getTargetCollectivities(): iterable
149    {
150        return array_unique(array_map(function ($duplicatedObject) {
151            return $duplicatedObject->getCollectivity();
152        }, is_array($this->duplicatedObjects) ? $this->duplicatedObjects : $this->duplicatedObjects->getValues()), SORT_REGULAR);
153    }
154
155    public function getDuplicatedObjects()
156    {
157        return $this->duplicatedObjects;
158    }
159
160    public function getDuplicatedObjectOfCollectivityAndOriginId(Collectivity $collectivity, string $originId): ?DuplicatedObject
161    {
162        foreach ($this->getDuplicatedObjects() as $duplicatedObject) {
163            if ($duplicatedObject->getCollectivity() === $collectivity && $duplicatedObject->getOriginObjectId() === $originId) {
164                return $duplicatedObject;
165            }
166        }
167
168        return null;
169    }
170
171    public function addDuplicatedObjet(DuplicatedObject $duplicatedObject)
172    {
173        $this->duplicatedObjects[] = $duplicatedObject;
174    }
175
176    public function removeDuplicatedObject(DuplicatedObject $duplicatedObject)
177    {
178        $key = \array_search($duplicatedObject, $this->duplicatedObjects, true);
179
180        if (false === $key) {
181            return;
182        }
183
184        if (isset($this->duplicatedObjects[$key])) {
185            unset($this->duplicatedObjects[$key]);
186        }
187    }
188
189    public function __toString()
190    {
191        return 'Duplication ' . $this->sourceCollectivity->__toString();
192    }
193}