Completed
Push — master ( e1c269...513975 )
by Dominik
02:01
created

LazyModelCollection::toRemove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

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