Completed
Push — master ( 5d2be1...68f0c9 )
by Dominik
01:57
created

LazyModelCollection::getForeignField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * @param ResolverInterface $resolver
57
     * @param string $modelClass
58
     * @param string $foreignField
59
     * @param string $foreignId
60
     * @param array|null $orderBy
61
     */
62 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...
63
        ResolverInterface $resolver,
64
        string $modelClass,
65
        string $foreignField,
66
        string $foreignId,
67
        array $orderBy = null
68
    ) {
69
        $this->resolver = $resolver;
70
        $this->modelClass = $modelClass;
71
        $this->foreignField = $foreignField;
72
        $this->foreignId = $foreignId;
73
        $this->orderBy = $orderBy;
74
    }
75
76
    private function resolveModels()
77
    {
78
        if ($this->resolved) {
79
            return;
80
        }
81
82
        $this->resolved = true;
83
84
        $criteria = [$this->foreignField => $this->foreignId];
85
86
        $models = [];
87
        foreach ($this->resolver->findBy($this->modelClass, $criteria, $this->orderBy) as $model) {
88
            $models[$model->getId()] = $model;
89
        }
90
91
        $this->initialModels = $models;
92
        $this->models = $models;
93
    }
94
95
    /**
96
     * @param ModelInterface $model
97
     *
98
     * @return ModelCollectionInterface
99
     */
100
    public function addModel(ModelInterface $model): ModelCollectionInterface
101
    {
102
        $this->resolveModels();
103
104
        $this->models[$model->getId()] = $model;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param ModelInterface $model
111
     *
112
     * @return ModelCollectionInterface
113
     */
114 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...
115
    {
116
        $this->resolveModels();
117
118
        if (isset($this->models[$model->getId()])) {
119
            unset($this->models[$model->getId()]);
120
        }
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param ModelInterface[]|array $models
127
     *
128
     * @return ModelCollectionInterface
129
     */
130
    public function setModels(array $models): ModelCollectionInterface
131
    {
132
        $this->resolveModels();
133
134
        $this->models = [];
135
        foreach ($models as $model) {
136
            $this->addModel($model);
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return ModelInterface[]|array
144
     */
145
    public function getModels(): array
146
    {
147
        $this->resolveModels();
148
149
        return $this->sort($this->modelClass, array_values($this->models), $this->orderBy);
150
    }
151
152
    /**
153
     * @return ModelInterface[]|array
154
     */
155
    public function getInitialModels(): array
156
    {
157
        $this->resolveModels();
158
159
        return array_values($this->initialModels);
160
    }
161
162
    /**
163
     * @return \ArrayIterator
164
     */
165
    public function getIterator()
166
    {
167
        $this->resolveModels();
168
169
        return new \ArrayIterator($this->getModels());
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public function jsonSerialize(): array
176
    {
177
        $this->resolveModels();
178
179
        $serializedModels = [];
180
        foreach ($this->getModels() as $model) {
181
            $serializedModels[] = $model->jsonSerialize();
182
        }
183
184
        return $serializedModels;
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getForeignField(): string
191
    {
192
        return $this->foreignField;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getForeignId(): string
199
    {
200
        return $this->foreignId;
201
    }
202
}
203