Completed
Push — master ( 647045...113f0b )
by Dominik
01:45
created

LazyModelCollection::toPersistence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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