Completed
Push — master ( c59a9c...50173f )
by Dominik
01:40
created

LazyModelCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 15
loc 105
rs 10
c 0
b 0
f 0

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
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($resolver());
45
        $this->initialModels = $this->models;
46
    }
47
48
    /**
49
     * @param ModelInterface $model
50
     * @return ModelCollectionInterface
51
     */
52
    public function addModel(ModelInterface $model): ModelCollectionInterface
53
    {
54
        $this->resolveModels();
55
56
        $this->models[$model->getId()] = $model;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param ModelInterface $model
63
     * @return ModelCollectionInterface
64
     */
65
    public function removeModel(ModelInterface $model): ModelCollectionInterface
66
    {
67
        $this->resolveModels();
68
69
        if (isset($this->models[$model->getId()])) {
70
            unset($this->models[$model->getId());
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')', expecting ']'
Loading history...
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param ModelInterface[]|array $models
78
     * @return ModelCollectionInterface
79
     */
80
    public function setModels(array $models): ModelCollectionInterface
81
    {
82
        $this->resolveModels();
83
84
        $this->models = [];
85
        foreach ($models as $model) {
86
            $this->addModel($model);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return ModelInterface[]|array
94
     */
95
    public function getModels(): array
96
    {
97
        $this->resolveModels();
98
99
        return $this->models;
100
    }
101
102
    /**
103
     * @return ModelInterface[]|array
104
     */
105
    public function getInitialModels(): array
106
    {
107
        $this->resolveModels();
108
109
        return $this->initialModels;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function jsonSerialize(): array
116
    {
117
        $this->resolveModels();
118
119
        $serializedModels = [];
120
        foreach ($this->models as $model) {
121
            $serializedModels[] = $model->jsonSerialize();
122
        }
123
124
        return $serializedModels;
125
    }
126
}
127