Passed
Push — master ( 74b41a...e78beb )
by Dominik
07:41
created

ModelCollection::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Collection;
6
7
use Chubbyphp\Model\ModelInterface;
8
use Chubbyphp\Model\ModelSortTrait;
9
10
final class ModelCollection implements ModelCollectionInterface
11
{
12
    use ModelSortTrait;
13
14
    /**
15
     * @var string
16
     */
17
    private $modelClass;
18
19
    /**
20
     * @var string
21
     */
22
    private $foreignField;
23
24
    /**
25
     * @var string
26
     */
27
    private $foreignId;
28
29
    /**
30
     * @var array|null
31
     */
32
    private $orderBy;
33
34
    /**
35
     * @var ModelInterface[]|array
36
     */
37
    private $models;
38
39
    /**
40
     * @var \ReflectionProperty
41
     */
42
    private $propertyReflection;
43
44
    /**
45
     * @param string $modelClass
46
     * @param string $foreignField
47
     * @param string $foreignId
48
     * @param array|null $orderBy
49
     */
50 11 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
        string $modelClass,
52
        string $foreignField,
53
        string $foreignId,
54
        array $orderBy = null
55
    ) {
56 11
        $this->modelClass = $modelClass;
57 11
        $this->foreignField = $foreignField;
58 11
        $this->foreignId = $foreignId;
59 11
        $this->orderBy = $orderBy;
60
61 11
        $this->models = [];
62
63 11
        $this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField);
64 11
        $this->propertyReflection->setAccessible(true);
65 11
    }
66
67
    /**
68
     * @param ModelInterface $model
69
     *
70
     * @return ModelCollectionInterface
71
     */
72 1 View Code Duplication
    public function addModel(ModelInterface $model): ModelCollectionInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74 1
        $this->propertyReflection->setValue($model, $this->foreignId);
75
76 1
        $this->models[$model->getId()] = $model;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @param ModelInterface $model
83
     *
84
     * @return ModelCollectionInterface
85
     */
86 1 View Code Duplication
    public function removeModel(ModelInterface $model): ModelCollectionInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88 1
        if (isset($this->models[$model->getId()])) {
89 1
            $this->propertyReflection->setValue($model, null);
90
91 1
            unset($this->models[$model->getId()]);
92
        }
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * @param ModelInterface[]|array $models
99
     *
100
     * @return ModelCollectionInterface
101
     */
102 1 View Code Duplication
    public function setModels(array $models): ModelCollectionInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104 1
        foreach ($this->models as $model) {
105
            $this->removeModel($model);
106
        }
107 1
        foreach ($models as $model) {
108 1
            $this->addModel($model);
109
        }
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * @return ModelInterface[]|array
116
     */
117 2
    public function getModels(): array
118
    {
119 2
        return $this->sort($this->modelClass, array_values($this->models), $this->orderBy);
120
    }
121
122
    /**
123
     * @return ModelInterface[]|array
124
     */
125 1
    public function getInitialModels(): array
126
    {
127 1
        return [];
128
    }
129
130
    /**
131
     * @return \ArrayIterator
132
     */
133 1
    public function getIterator()
134
    {
135 1
        return new \ArrayIterator($this->getModels());
136
    }
137
138
    /**
139
     * @return int
140
     */
141 1
    public function count()
142
    {
143 1
        return count($this->getModels());
144
    }
145
146
    /**
147
     * @return array
148
     */
149 1 View Code Duplication
    public function jsonSerialize(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151 1
        $this->jsonSerializableOrException();
152
153 1
        $serializedModels = [];
154 1
        foreach ($this->getModels() as $model) {
155 1
            $serializedModels[] = $model->jsonSerialize();
156
        }
157
158 1
        return $serializedModels;
159
    }
160
161
    /**
162
     * @throws \LogicException
163
     */
164 View Code Duplication
    private function jsonSerializableOrException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166
        if (!in_array(\JsonSerializable::class, class_implements($this->modelClass), true)) {
167
            throw new \LogicException(
168
                sprintf('Model %s does not implement %s', $this->modelClass, \JsonSerializable::class)
169
            );
170
        }
171
    }
172
173
    /**
174
     * @return string
175
     */
176 1
    public function getForeignField(): string
177
    {
178 1
        return $this->foreignField;
179
    }
180
181
    /**
182
     * @return string
183
     */
184 1
    public function getForeignId(): string
185
    {
186 1
        return $this->foreignId;
187
    }
188
}
189