Test Failed
Push — main ( fc8ba5...9083af )
by Bingo
05:27
created

BatchElementConfiguration::addDeploymentMappings()   B

Complexity

Conditions 9
Paths 84

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 24
rs 8.0555
cc 9
nc 84
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Batch;
4
5
class BatchElementConfiguration
6
{
7
    protected $collectedMappings = [];
8
9
    protected $ids = [];
10
    protected $mappings;
11
12
    /**
13
     * Add mappings of deployment ids to resource ids to the overall element
14
     * mapping list. All elements from <code>idList</code> that are not part of
15
     * the mappings will be added to the list of <code>null</code> deployment id
16
     * mappings.
17
     *
18
     * @param mappingsList
19
     *          the mappings to add
20
     * @param idList
21
     *          the list of ids to check for missing elements concerning the
22
     *          mappings to add
23
     */
24
    public function addDeploymentMappings(array $mappingsList, array $idList): void
25
    {
26
        if (!empty($this->ids)) {
27
            $this->ids = [];
28
            $this->mappings = null;
29
        }
30
        $missingIds = empty($idList) ? [] : $idList;
31
        foreach ($mappingsList as $pair) {
32
            $deploymentId = $pair[0];
33
            if (!array_key_exists($deploymentId, $this->collectedMappings)) {
34
                $this->collectedMappings[$deploymentId] = [];
35
            }
36
            $this->collectedMappings[$deploymentId][] = $pair[1];
37
            if (!empty($missingIds)) {
38
                if (($key = array_search($pair[1], $missingIds)) !== false) {
39
                    unset($missingIds[$key]);
40
                }
41
            }
42
        }
43
        if (!empty($missingIds)) {
44
            if (!array_key_exists(0, $this->collectedMappings)) {
45
                $this->collectedMappings[0] = [];
46
            }
47
            $this->collectedMappings[0] = array_merge($this->collectedMappings[0], $missingIds);
48
        }
49
    }
50
51
    /**
52
     * @return array the list of ids that are mapped to deployment ids, ordered by
53
     *         deployment id
54
     */
55
    public function getIds(): array
56
    {
57
        if (empty($this->ids)) {
58
            $this->createDeploymentMappings();
59
        }
60
        return $this->ids;
61
    }
62
63
    /**
64
     * @return DeploymentMappings the list of {@link DeploymentMapping}s
65
     */
66
    public function getMappings(): DeploymentMappings
67
    {
68
        if ($this->mappings == null) {
69
            $this->createDeploymentMappings();
70
        }
71
        return $this->mappings;
72
    }
73
74
    public function isEmpty(): bool
75
    {
76
        return empty($this->collectedMappings);
77
    }
78
79
    protected function createDeploymentMappings(): void
80
    {
81
        $this->ids = [];
82
        $this->mappings = new DeploymentMappings();
83
84
        foreach ($this->collectedMappings as $key => $mappingIds) {
85
            $this->ids = array_unique(array_merge($this->ids, $mappingIds));
86
            $this->mappings->add(new DeploymentMapping($key, count($mappingIds)));
87
        }
88
    }
89
}
90