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

ModelCollection::remove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
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
    public function persist()
105
    {
106
        foreach ($this->models as $model) {
107
            $this->repository->persist($model);
108
        }
109
    }
110
111 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...
112
    {
113
        foreach ($this->initialModels as $initialModel) {
114
            if (!isset($this->models[$initialModel->getId()])) {
115
                $this->repository->remove($initialModel);
116
            }
117
        }
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function jsonSerialize(): array
124
    {
125
        $serializedModels = [];
126
        foreach ($this->models as $model) {
127
            $serializedModels[] = $model->jsonSerialize();
128
        }
129
130
        return $serializedModels;
131
    }
132
}
133