Completed
Push — master ( 28d89f...74b41a )
by Dominik
02:30
created

LazyModelCollection::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
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\ResolverInterface;
9
use Chubbyphp\Model\ModelSortTrait;
10
11
final class LazyModelCollection implements ModelCollectionInterface
12
{
13
    use ModelSortTrait;
14
15
    /**
16
     * @var ResolverInterface
17
     */
18
    private $resolver;
19
20
    /**
21
     * @var string
22
     */
23
    private $modelClass;
24
25
    /**
26
     * @var string
27
     */
28
    private $foreignField;
29
30
    /**
31
     * @var string
32
     */
33
    private $foreignId;
34
35
    /**
36
     * @var array|null
37
     */
38
    private $orderBy;
39
40
    /**
41
     * @var bool
42
     */
43
    private $resolved = false;
44
45
    /**
46
     * @var ModelInterface[]|array
47
     */
48
    private $initialModels;
49
50
    /**
51
     * @var ModelInterface[]|array
52
     */
53
    private $models;
54
55
    /**
56
     * @var \ReflectionProperty
57
     */
58
    private $propertyReflection;
59
60
    /**
61
     * @param ResolverInterface $resolver
62
     * @param string $modelClass
63
     * @param string $foreignField
64
     * @param string $foreignId
65
     * @param array|null $orderBy
66
     */
67 10 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...
68
        ResolverInterface $resolver,
69
        string $modelClass,
70
        string $foreignField,
71
        string $foreignId,
72
        array $orderBy = null
73
    ) {
74 10
        $this->resolver = $resolver;
75 10
        $this->modelClass = $modelClass;
76 10
        $this->foreignField = $foreignField;
77 10
        $this->foreignId = $foreignId;
78 10
        $this->orderBy = $orderBy;
79
80 10
        $this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField);
81 10
        $this->propertyReflection->setAccessible(true);
82 10
    }
83
84 8
    private function resolveModels()
85
    {
86 8
        if ($this->resolved) {
87 8
            return;
88
        }
89
90 8
        $this->resolved = true;
91
92 8
        $criteria = [$this->foreignField => $this->foreignId];
93
94 8
        $models = [];
95 8
        foreach ($this->resolver->findBy($this->modelClass, $criteria, $this->orderBy) as $model) {
96 4
            $models[$model->getId()] = $model;
97
        }
98
99 8
        $this->initialModels = $models;
100 8
        $this->models = $models;
101 8
    }
102
103
    /**
104
     * @param ModelInterface $model
105
     *
106
     * @return ModelCollectionInterface
107
     */
108 1
    public function addModel(ModelInterface $model): ModelCollectionInterface
109
    {
110 1
        $this->resolveModels();
111
112 1
        $this->propertyReflection->setValue($model, $this->foreignId);
113
114 1
        $this->models[$model->getId()] = $model;
115
116 1
        return $this;
117
    }
118
119
    /**
120
     * @param ModelInterface $model
121
     *
122
     * @return ModelCollectionInterface
123
     */
124 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...
125
    {
126 1
        $this->resolveModels();
127
128 1
        if (isset($this->models[$model->getId()])) {
129 1
            $this->propertyReflection->setValue($model, null);
130
131 1
            unset($this->models[$model->getId()]);
132
        }
133
134 1
        return $this;
135
    }
136
137
    /**
138
     * @param ModelInterface[]|array $models
139
     *
140
     * @return ModelCollectionInterface
141
     */
142 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...
143
    {
144 1
        $this->resolveModels();
145
146 1
        foreach ($this->models as $model) {
147
            $this->removeModel($model);
148
        }
149 1
        foreach ($models as $model) {
150 1
            $this->addModel($model);
151
        }
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * @return ModelInterface[]|array
158
     */
159 2
    public function getModels(): array
160
    {
161 2
        $this->resolveModels();
162
163 2
        return $this->sort($this->modelClass, array_values($this->models), $this->orderBy);
164
    }
165
166
    /**
167
     * @return ModelInterface[]|array
168
     */
169 1
    public function getInitialModels(): array
170
    {
171 1
        $this->resolveModels();
172
173 1
        return array_values($this->initialModels);
174
    }
175
176
    /**
177
     * @return \ArrayIterator
178
     */
179 1
    public function getIterator()
180
    {
181 1
        $this->resolveModels();
182
183 1
        return new \ArrayIterator($this->getModels());
184
    }
185
186
    /**
187
     * @return int
188
     */
189 1
    public function count()
190
    {
191 1
        $this->resolveModels();
192
193 1
        return count($this->getModels());
194
    }
195
196
    /**
197
     * @return string
198
     */
199 1
    public function getForeignField(): string
200
    {
201 1
        return $this->foreignField;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 1
    public function getForeignId(): string
208
    {
209 1
        return $this->foreignId;
210
    }
211
}
212