Completed
Push — master ( 667ec9...454d67 )
by Dominik
01:49
created

ModelCollection::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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