Completed
Push — master ( 454d67...c04101 )
by Dominik
01:52
created

LazyModelCollection   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 171
Duplicated Lines 14.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 25
loc 171
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A loadModels() 0 13 2
A modelsWithIdKey() 15 15 3
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 set() 0 6 1
A persist() 0 8 2
A remove() 10 10 3
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
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
    public function persist()
145
    {
146
        $this->loadModels();
147
148
        foreach ($this->models as $model) {
149
            $this->repository->persist($model);
150
        }
151
    }
152
153 View Code Duplication
    public function remove()
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...
154
    {
155
        $this->loadModels();
156
157
        foreach ($this->initialModels as $initialModel) {
158
            if (!isset($this->models[$initialModel->getId()])) {
159
                $this->repository->remove($initialModel);
160
            }
161
        }
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function jsonSerialize(): array
168
    {
169
        $this->loadModels();
170
171
        $serializedModels = [];
172
        foreach ($this->models as $model) {
173
            $serializedModels[] = $model->jsonSerialize();
174
        }
175
176
        return $serializedModels;
177
    }
178
}
179