Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

GroupParser::checkEnvironmentAreUnique()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace Karma\Configuration\Parser;
4
5
class GroupParser extends AbstractSectionParser
6
{
7
    private
8
        $groups;
9
    
10
    public function __construct()
11
    {
12
        $this->groups = array();
13
    }
14
    
15
    protected function parseLine($line)
16
    {
17
        if($this->isACommentLine($line))
18
        {
19
            return true;
20
        }
21
22
        $line = trim($line);
23
        
24
        if(preg_match('~(?P<groupName>[^=]+)\s*=\s*\[(?P<envList>[^\[\]]+)\]$~', $line, $matches))
25
        {
26
            return $this->processGroupDefinition($matches['groupName'], $matches['envList']);
27
        }
28
        
29
        $this->triggerError($line);
30
    }
31
    
32
    private function processGroupDefinition($groupName, $envList)
33
    {
34
        $groupName = trim($groupName);
35
        
36
        $this->checkGroupStillNotExists($groupName);
37
        
38
        $environments = array_map('trim', explode(',', $envList));
39
        $this->checkEnvironmentAreUnique($groupName, $environments);
40
        
41
        $this->groups[$groupName] = array();
42
        
43
        foreach($environments as $env)
44
        {
45
            if(empty($env))
46
            {
47
                $this->triggerError("empty environment in declaration of group $groupName");
48
            }
49
            
50
            $this->groups[$groupName][] = $env;
51
        }
52
    }
53
    
54
    private function checkGroupStillNotExists($groupName)
55
    {
56
        if(isset($this->groups[$groupName]))
57
        {
58
            $this->triggerError("group $groupName has already been declared");
59
        }
60
    }
61
    
62
    private function checkEnvironmentAreUnique($groupName, array $environments)
63
    {
64
        if($this->hasDuplicatedValues($environments))
65
        {
66
            $this->triggerError("duplicated environment in group $groupName");
67
        }
68
    }
69
    
70
    private function hasDuplicatedValues(array $values)
71
    {
72
        $duplicatedValues = array_filter(array_count_values($values), function ($counter) {
73
            return $counter !== 1;
74
        });
75
        
76
        return empty($duplicatedValues) === false;
77
    }
78
    
79
    public function getCollectedGroups()
80
    {
81
        return $this->groups;
82
    }
83
    
84
    public function postParse()
85
    {
86
        $this->checkEnvironmentsBelongToOnlyOneGroup();
87
        $this->checkGroupsAreNotPartsOfAnotherGroups();
88
    }
89
    
90
    private function checkEnvironmentsBelongToOnlyOneGroup()
91
    {
92
        $allEnvironments = $this->getAllEnvironmentsBelongingToGroups();
93
94
        if($this->hasDuplicatedValues($allEnvironments))
95
        {
96
            throw new \RuntimeException('Error : some environments are in various groups');
97
        }
98
    }
99
    
100
    private function getAllEnvironmentsBelongingToGroups()
101
    {
102
        $allEnvironments = array();
103
        
104
        foreach($this->groups as $group)
105
        {
106
            $allEnvironments = array_merge($allEnvironments, $group);
107
        }
108
        
109
        return $allEnvironments;
110
    }
111
    
112
    private function checkGroupsAreNotPartsOfAnotherGroups()
113
    {
114
        $allEnvironments = $this->getAllEnvironmentsBelongingToGroups();
115
116
        $errors = array_intersect($allEnvironments, array_keys($this->groups));
117
        
118
        if(! empty($errors))
119
        {
120
            throw new \RuntimeException(sprintf(
121
               'Error : a group can not be part of another group (%s)',
122
                implode(', ', $errors)
123
            ));
124
        }
125
    }
126
}