Completed
Push — master ( e32de9...277825 )
by Dominik
02:04
created

LazyModelCollection::getIterator()   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
final 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 resolveModels()
35
    {
36
        if (null === $this->resolver) {
37
            return;
38
        }
39
40
        $resolver = $this->resolver;
41
42
        $this->resolver = null;
43
44
        $this->setModels((array) $resolver());
45
        $this->initialModels = $this->models;
46
    }
47
48
    /**
49
     * @param ModelInterface $model
50
     *
51
     * @return ModelCollectionInterface
52
     */
53
    public function addModel(ModelInterface $model): ModelCollectionInterface
54
    {
55
        $this->resolveModels();
56
57
        $this->models[$model->getId()] = $model;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param ModelInterface $model
64
     *
65
     * @return ModelCollectionInterface
66
     */
67 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...
68
    {
69
        $this->resolveModels();
70
71
        if (isset($this->models[$model->getId()])) {
72
            unset($this->models[$model->getId()]);
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param ModelInterface[]|array $models
80
     *
81
     * @return ModelCollectionInterface
82
     */
83
    public function setModels(array $models): ModelCollectionInterface
84
    {
85
        $this->resolveModels();
86
87
        $this->models = [];
88
        foreach ($models as $model) {
89
            $this->addModel($model);
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return ModelInterface[]|array
97
     */
98
    public function getModels(): array
99
    {
100
        $this->resolveModels();
101
102
        return $this->models;
103
    }
104
105
    /**
106
     * @return ModelInterface[]|array
107
     */
108
    public function getInitialModels(): array
109
    {
110
        $this->resolveModels();
111
112
        return $this->initialModels;
113
    }
114
115
    /**
116
     * @return \ArrayIterator
117
     */
118
    public function getIterator()
119
    {
120
        $this->resolveModels();
121
122
        return new \ArrayIterator($this->models);
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function jsonSerialize(): array
129
    {
130
        $this->resolveModels();
131
132
        $serializedModels = [];
133
        foreach ($this->models as $model) {
134
            $serializedModels[] = $model->jsonSerialize();
135
        }
136
137
        return $serializedModels;
138
    }
139
}
140