Passed
Push — master ( 893bce...1901a3 )
by Dominik
02:01
created

ModelCollection::getModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 13 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 13
        $this->modelClass = $modelClass;
57 13
        $this->foreignField = $foreignField;
58 13
        $this->foreignId = $foreignId;
59 13
        $this->orderBy = $orderBy;
60
61 13
        $this->models = [];
62
63 13
        $this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField);
64 13
        $this->propertyReflection->setAccessible(true);
65 13
    }
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 1
            $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 3
    public function getModels(): array
118
    {
119 3
        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 2 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 2
        $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 2 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 2
        $reflectionClass = new \ReflectionClass($this->modelClass);
167 2
        if (!$reflectionClass->implementsInterface(\JsonSerializable::class)) {
168 1
            throw new \LogicException(
169 1
                sprintf('Model %s does not implement %s', $this->modelClass, \JsonSerializable::class)
170
            );
171
        }
172 1
    }
173
174
    /**
175
     * @return string
176
     */
177 1
    public function getForeignField(): string
178
    {
179 1
        return $this->foreignField;
180
    }
181
182
    /**
183
     * @return string
184
     */
185 1
    public function getForeignId(): string
186
    {
187 1
        return $this->foreignId;
188
    }
189
}
190