Completed
Push — master ( 63e481...1e8bb2 )
by Dominik
01:52
created

LazyPersistedModelCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 8.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 14
loc 157
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadModels() 0 11 2
A current() 0 6 1
A next() 0 6 1
A key() 0 6 1
A valid() 0 6 1
A rewind() 0 6 1
A add() 0 8 1
A remove() 14 14 3
A toPersist() 0 6 1
A toRemove() 0 6 1
A jsonSerialize() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Chubbyphp\Model\Collection;
4
5
use Chubbyphp\Model\ModelInterface;
6
7
class LazyPersistedModelCollection 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
     * @var ModelInterface[]|array
26
     */
27
    private $toRemoveModels;
28
29
    /**
30
     * ResolverCollection constructor.
31
     * @param \Closure $resolver
32
     */
33
    public function __construct(\Closure $resolver)
34
    {
35
        $this->resolver = $resolver;
36
    }
37
38
    private function loadModels()
39
    {
40
        if (null !== $this->models) {
41
            return;
42
        }
43
44
        $resolver = $this->resolver;
45
        $this->initialModels = (array) $resolver();
46
        $this->models = $this->initialModels;
47
        $this->toRemoveModels = [];
48
    }
49
50
    /**
51
     * @return ModelInterface
52
     */
53
    public function current()
54
    {
55
        $this->loadModels();
56
57
        return current($this->models);
58
    }
59
60
    /**
61
     * @return ModelInterface|false
62
     */
63
    public function next()
64
    {
65
        $this->loadModels();
66
67
        return next($this->models);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function key()
74
    {
75
        $this->loadModels();
76
77
        return key($this->models);
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function valid()
84
    {
85
        $this->loadModels();
86
87
        return (bool) current($this->models);
88
    }
89
90
    public function rewind()
91
    {
92
        $this->loadModels();
93
94
        reset($this->models);
95
    }
96
97
    /**
98
     * @param ModelInterface $model
99
     * @return ModelCollectionInterface
100
     */
101
    public function add(ModelInterface $model): ModelCollectionInterface
102
    {
103
        $this->loadModels();
104
105
        $this->models[$model->getId()] = $model;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param ModelInterface $model
112
     * @return ModelCollectionInterface
113
     */
114 View Code Duplication
    public function remove(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->loadModels();
117
118
        if (isset($this->models[$model->getId()])) {
119
            unset($this->models[$model->getId()]);
120
        }
121
122
        if (isset($this->initialModels[$model->getId()])) {
123
            $this->toRemoveModels[$model->getId()] = $model;
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return ModelInterface[]|array
131
     */
132
    public function toPersist(): array
133
    {
134
        $this->loadModels();
135
136
        return $this->models;
137
    }
138
139
    /**
140
     * @return ModelInterface[]|array
141
     */
142
    public function toRemove(): array
143
    {
144
        $this->loadModels();
145
146
        return $this->toRemoveModels;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function jsonSerialize(): array
153
    {
154
        $this->loadModels();
155
156
        $serialzedModels = [];
157
        foreach ($this->models as $model) {
158
            $serialzedModels[] = $model->jsonSerialize();
159
        }
160
161
        return $serialzedModels;
162
    }
163
}
164