Completed
Push — master ( e1c269...513975 )
by Dominik
02:01
created

ModelCollection::toRemove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace Chubbyphp\Model\Collection;
4
5
use Chubbyphp\Model\ModelInterface;
6
7
class ModelCollection implements ModelCollectionInterface
8
{
9
    /**
10
     * @var ModelInterface[]|array
11
     */
12
    private $initialModels;
13
14
    /**
15
     * @var ModelInterface[]|array
16
     */
17
    private $models;
18
19
    /**
20
     * @param ModelInterface[]|array $models
21
     */
22
    public function __construct(array $models = [])
23
    {
24
        $models = $this->modelsWithIdKey($models);
25
26
        $this->initialModels = $models;
27
        $this->models = $models;
28
    }
29
30
    /**
31
     * @param ModelInterface[]|array $models
32
     *
33
     * @return ModelInterface[]|array
34
     */
35 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...
36
    {
37
        $modelsWithIdKey = [];
38
        foreach ($models as $model) {
39
            if (!$model instanceof ModelInterface) {
40
                throw new \InvalidArgumentException(
41
                    sprintf('Model with index %d needs to implement: %s', ModelInterface::class)
42
                );
43
            }
44
45
            $modelsWithIdKey[$model->getId()] = $model;
46
        }
47
48
        return $modelsWithIdKey;
49
    }
50
51
    /**
52
     * @return ModelInterface
53
     */
54
    public function current()
55
    {
56
        return current($this->models);
57
    }
58
59
    /**
60
     * @return ModelInterface|false
61
     */
62
    public function next()
63
    {
64
        return next($this->models);
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function key()
71
    {
72
        return key($this->models);
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function valid()
79
    {
80
        return (bool) current($this->models);
81
    }
82
83
    public function rewind()
84
    {
85
        reset($this->models);
86
    }
87
88
    /**
89
     * @param ModelInterface[]|array $models
90
     */
91
    public function set(array $models)
92
    {
93
        $this->models = $this->modelsWithIdKey($models);
94
    }
95
96
    /**
97
     * @return ModelInterface[]|array
98
     */
99
    public function toPersist(): array
100
    {
101
        return $this->models;
102
    }
103
104
    /**
105
     * @return ModelInterface[]|array
106
     */
107 View Code Duplication
    public function toRemove(): 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...
108
    {
109
        $toRemoveModels = [];
110
        foreach ($this->initialModels as $initialModel) {
111
            if (!isset($this->models[$initialModel->getId()])) {
112
                $toRemoveModels[$initialModel->getId()] = $initialModel;
113
            }
114
        }
115
116
        return $toRemoveModels;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function jsonSerialize(): array
123
    {
124
        $serializedModels = [];
125
        foreach ($this->models as $model) {
126
            $serializedModels[] = $model->jsonSerialize();
127
        }
128
129
        return $serializedModels;
130
    }
131
}
132