Completed
Push — master ( 667ec9...454d67 )
by Dominik
01:49
created

LazyModelCollection::modelsWithIdKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Chubbyphp\Model\Collection;
4
5
use Chubbyphp\Model\ModelInterface;
6
use Chubbyphp\Model\RepositoryInterface;
7
8
class LazyModelCollection implements ModelCollectionInterface
9
{
10
    /**
11
     * @var RepositoryInterface
12
     */
13
    private $repository;
14
15
    /**
16
     * @var \Closure;
17
     */
18
    private $resolver;
19
20
    /**
21
     * @var ModelInterface[]|array
22
     */
23
    private $initialModels;
24
25
    /**
26
     * @var ModelInterface[]|array
27
     */
28
    private $models;
29
30
    /**
31
     * LazyModelCollection constructor.
32
     *
33
     * @param RepositoryInterface $repository
34
     * @param array               $criteria
35
     * @param array|null          $orderBy
36
     * @param int|null            $limit
37
     * @param int|null            $offset
38
     */
39
    public function __construct(
40
        RepositoryInterface $repository,
41
        array $criteria,
42
        array $orderBy = null,
43
        int $limit = null,
44
        int $offset = null
45
    ) {
46
        $this->repository = $repository;
47
        $this->resolver = function () use ($repository, $criteria, $orderBy, $limit, $offset) {
48
            return $repository->findBy($criteria, $orderBy, $limit, $offset);
49
        };
50
    }
51
52
    private function loadModels()
53
    {
54
        if (null !== $this->initialModels) {
55
            return;
56
        }
57
58
        $resolver = $this->resolver;
59
60
        $models = $this->modelsWithIdKey((array) $resolver());
61
62
        $this->initialModels = $models;
63
        $this->models = $models;
64
    }
65
66
    /**
67
     * @param ModelInterface[]|array $models
68
     *
69
     * @return ModelInterface[]|array
70
     */
71 View Code Duplication
    private function modelsWithIdKey(array $models): 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...
72
    {
73
        $modelsWithIdKey = [];
74
        foreach ($models as $model) {
75
            if (!$model instanceof ModelInterface) {
76
                throw new \InvalidArgumentException(
77
                    sprintf('Model with index %d needs to implement: %s', ModelInterface::class)
78
                );
79
            }
80
81
            $modelsWithIdKey[$model->getId()] = $model;
82
        }
83
84
        return $modelsWithIdKey;
85
    }
86
87
    /**
88
     * @return ModelInterface
89
     */
90
    public function current()
91
    {
92
        $this->loadModels();
93
94
        return current($this->models);
95
    }
96
97
    /**
98
     * @return ModelInterface|false
99
     */
100
    public function next()
101
    {
102
        $this->loadModels();
103
104
        return next($this->models);
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function key()
111
    {
112
        $this->loadModels();
113
114
        return key($this->models);
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function valid()
121
    {
122
        $this->loadModels();
123
124
        return (bool) current($this->models);
125
    }
126
127
    public function rewind()
128
    {
129
        $this->loadModels();
130
131
        reset($this->models);
132
    }
133
134
    /**
135
     * @param ModelInterface[]|array $models
136
     */
137
    public function set(array $models)
138
    {
139
        $this->loadModels();
140
141
        $this->models = $this->modelsWithIdKey($models);
142
    }
143
144
    /**
145
     * @return ModelInterface[]
146
     */
147
    public function toPersist(): array
148
    {
149
        $this->loadModels();
150
151
        return $this->models;
152
    }
153
154
    /**
155
     * @return array
156
     */
157 View Code Duplication
    public function toRemove(): 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...
158
    {
159
        $this->loadModels();
160
161
        $toRemove = [];
162
        foreach ($this->initialModels as $initialModel) {
163
            if (!isset($this->models[$initialModel->getId()])) {
164
                $toRemove[$initialModel->getId()] = $initialModel;
165
            }
166
        }
167
168
        return $toRemove;
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public function jsonSerialize(): array
175
    {
176
        $this->loadModels();
177
178
        $serializedModels = [];
179
        foreach ($this->models as $model) {
180
            $serializedModels[] = $model->jsonSerialize();
181
        }
182
183
        return $serializedModels;
184
    }
185
}
186