Completed
Push — master ( 647045...113f0b )
by Dominik
01:45
created

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