Completed
Push — master ( 679558...de1094 )
by Dominik
01:55
created

getToPersistModels()   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\Doctrine\DBAL\Repository;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
final class RelatedModelManipulationStack
10
{
11
    /**
12
     * @var ModelInterface[]|array
13
     */
14
    private $toPersistModels = [];
15
16
    /**
17
     * @var ModelInterface[]|array
18
     */
19
    private $toRemoveModels = [];
20
21
    /**
22
     * @param ModelInterface[]|array $models
23
     * @return RelatedModelManipulationStack
24
     */
25
    public function addToPersistModels(array $models): RelatedModelManipulationStack
26
    {
27
        foreach ($models as $model) {
28
            $this->addToPersistModel($model);
29
        }
30
31
        return $this;
32
    }
33
34
    /**
35
     * @param ModelInterface $model
36
     * @return RelatedModelManipulationStack
37
     */
38
    public function addToPersistModel(ModelInterface $model): RelatedModelManipulationStack
39
    {
40
        $this->toPersistModels[$model->getId()] = $model;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param ModelInterface[]|array $models
47
     * @return RelatedModelManipulationStack
48
     */
49
    public function addToRemoveModels(array $models): RelatedModelManipulationStack
50
    {
51
        foreach ($models as $model) {
52
            $this->addToRemoveModel($model);
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param ModelInterface $model
60
     * @return RelatedModelManipulationStack
61
     */
62
    public function addToRemoveModel(ModelInterface $model): RelatedModelManipulationStack
63
    {
64
        $this->toRemoveModels[$model->getId()] = $model;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return ModelInterface[]|array
71
     */
72
    public function getToPersistModels(): array
73
    {
74
        return $this->toPersistModels;
75
    }
76
77
    /**
78
     * @return ModelInterface[]|array
79
     */
80
    public function getToRemoveModels(): array
81
    {
82
        $toRemoveModels = $this->toRemoveModels;
83
        foreach ($toRemoveModels as $toRemoveModel) {
84
            if (isset($this->toPersistModels[$toRemoveModel->getId()])) {
85
                unset($toRemoveModels[$toRemoveModel->getId()]);
86
            }
87
        }
88
89
        return $toRemoveModels;
90
    }
91
}
92