Completed
Push — master ( 073df6...660a9d )
by Dominik
01:58
created

LazyModelCollection::modelsWithIdKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Collection;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
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 loadModels()
35
    {
36
        if (null !== $this->initialModels) {
37
            return;
38
        }
39
40
        $resolver = $this->resolver;
41
42
        $models = $this->modelsWithIdKey((array) $resolver());
43
44
        $this->initialModels = $models;
45
        $this->models = $models;
46
    }
47
48
    /**
49
     * @param ModelInterface[]|array $models
50
     *
51
     * @return ModelInterface[]|array
52
     */
53 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...
54
    {
55
        $modelsWithIdKey = [];
56
        foreach ($models as $model) {
57
            if (!$model instanceof ModelInterface) {
58
                throw new \InvalidArgumentException(
59
                    sprintf('Model with index %d needs to implement: %s', ModelInterface::class)
60
                );
61
            }
62
63
            $modelsWithIdKey[$model->getId()] = $model;
64
        }
65
66
        return $modelsWithIdKey;
67
    }
68
69
    /**
70
     * @param ModelInterface[]|array $models
71
     */
72
    public function set(array $models)
73
    {
74
        $this->loadModels();
75
76
        $this->models = $this->modelsWithIdKey($models);
77
    }
78
79
    /**
80
     * @return ModelInterface[]|array
81
     */
82
    public function get(): array
83
    {
84
        $this->loadModels();
85
86
        return $this->models;
87
    }
88
89
    /**
90
     * @return ModelInterface[]|array
91
     */
92
    public function getInitial(): array
93
    {
94
        $this->loadModels();
95
96
        return $this->initialModels;
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function jsonSerialize(): array
103
    {
104
        $this->loadModels();
105
106
        $serializedModels = [];
107
        foreach ($this->models as $model) {
108
            $serializedModels[] = $model->jsonSerialize();
109
        }
110
111
        return $serializedModels;
112
    }
113
}
114