Completed
Push — master ( 788661...cf45e8 )
by Rémi
08:25
created

ProjectConfig::setProjectDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php namespace G2R\Config;
2
3
use G2R\Exception\Exception;
4
5
class ProjectConfig extends AbstractConfig
6
{
7 12
    public function __construct($path)
8
    {
9 12
        parent::__construct($path);
10
11 12
        $this->setProjectDefaults();
12 12
    }
13
14 12
    protected function getDefaults()
15
    {
16
        return [
17 12
            'base_url' => 'http://gitlab.local',
18 6
        ];
19
    }
20
21 12
    protected function getRequirements()
22
    {
23
        return [
24 12
            'base_url',
25 6
            'projects',
26 6
        ];
27
    }
28
29 12
    protected function getProjectDefaults()
30
    {
31
        return [
32 12
            'ref'       => 'master',
33 6
            'runOnFail' => false,
34 6
        ];
35
    }
36
37 12
    protected function setProjectDefaults()
38
    {
39 12
        $projects = $this['projects'];
40
41
        array_walk($projects, function (&$project) {
42 12
            $project['project'] = array_merge(
43 12
                $this->getProjectDefaults(),
44 12
                $project['project']
45 6
            );
46 12
        });
47
48 12
        $this['projects'] = $projects;
49 12
    }
50
51
    /**
52
     * Try to find a project and return it
53
     *
54
     * @return array $project
55
     */
56 10
    protected function findProject($name, $ref)
57
    {
58 10
        $projects = array_filter(
59 10
            $this->get('projects'),
60 10
            function ($config) use ($name, $ref) {
61
                return
62 10
                    $config['project']['name'] == $name &&
63 10
                    $config['project']['ref'] == $ref;
64 5
            }
65 5
        );
66
67 10
        if (count($projects) > 1) {
68 2
            throw new Exception("Duplicate project {$name}[{$ref}]");
69
        }
70
71 8
        if (count($projects) === 0) {
72 2
            throw new Exception("Project {$name}[{$ref}] not found");
73
        }
74
75 6
        return array_shift($projects)['project'];
76
    }
77
78
    /**
79
     * Return a project identified by its short url
80
     *
81
     * @param        $name
82
     * @param string $ref
83
     *
84
     * @return array $project
85
     * @throws Exception
86
     */
87 10
    public function getProject($name, $ref = 'master')
88
    {
89 10
        $project = $this->findProject($name, $ref);
90
91 6
        if (!isset($project['jobId'])) {
92 2
            throw new Exception(
93 2
                "The jobId is missing for the {$name}[{$ref}] project"
94 1
            );
95
        }
96
97 4
        return $project;
98
    }
99
}
100