AbstractJob::dump()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 256
nop 0
dl 0
loc 33
rs 6.5222
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
4
5
use TheAentMachine\AentGitLabCI\Exception\JobException;
6
7
abstract class AbstractJob
8
{
9
    /** @var string */
10
    protected $jobName;
11
12
    /** @var string */
13
    protected $image;
14
15
    /** @var string */
16
    protected $stage;
17
18
    /** @var string[] */
19
    protected $services;
20
21
    /** @var array<string,string> */
22
    protected $variables = [];
23
24
    /** @var string[] */
25
    protected $beforeScript = [];
26
27
    /** @var string[] */
28
    protected $script = [];
29
30
    /** @var array<string,string> */
31
    protected $environment = [];
32
33
    /** @var string[] */
34
    protected $only = [];
35
36
    /** @var string[] */
37
    protected $except = [];
38
39
    /** @var bool */
40
    protected $manual = false;
41
42
    /** @return mixed[] */
43
    public function dump(): array
44
    {
45
        $obj = [
46
            $this->jobName => [
47
                'image' => $this->image,
48
                'stage' => $this->stage,
49
            ]
50
        ];
51
        if ($this->hasServices()) {
52
            $obj[$this->jobName]['services'] = $this->services;
53
        }
54
        if ($this->hasVariables()) {
55
            $obj[$this->jobName]['variables'] = $this->variables;
56
        }
57
        if ($this->hasBeforeScript()) {
58
            $obj[$this->jobName]['before_script'] = $this->beforeScript;
59
        }
60
        if ($this->hasScript()) {
61
            $obj[$this->jobName]['script'] = $this->script;
62
        }
63
        if ($this->hasEnvironment()) {
64
            $obj[$this->jobName]['environment'] = $this->environment;
65
        }
66
        if ($this->hasOnly()) {
67
            $obj[$this->jobName]['only'] = $this->only;
68
        }
69
        if ($this->hasExcept()) {
70
            $obj[$this->jobName]['except'] = $this->except;
71
        }
72
        if ($this->manual) {
73
            $obj[$this->jobName]['when'] = 'manual';
74
        }
75
        return $obj;
76
    }
77
78
    /**
79
     * @param string $identifier
80
     * @throws JobException
81
     */
82
    public function addOnly(string $identifier): void
83
    {
84
        if (\in_array($identifier, $this->only)) {
85
            return;
86
        }
87
        if (\in_array($identifier, $this->except)) {
88
            throw JobException::cannotAddOnly($identifier);
89
        }
90
        $this->only[] = $identifier;
91
    }
92
93
    /**
94
     * @param string $identifier
95
     * @throws JobException
96
     */
97
    public function addExcept(string $identifier): void
98
    {
99
        if (\in_array($identifier, $this->except)) {
100
            return;
101
        }
102
        if (\in_array($identifier, $this->only)) {
103
            throw JobException::cannotAddExcept($identifier);
104
        }
105
        $this->except[] = $identifier;
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    private function hasServices(): bool
112
    {
113
        return !empty($this->services);
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    private function hasVariables(): bool
120
    {
121
        return !empty($this->variables);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    private function hasBeforeScript(): bool
128
    {
129
        return !empty($this->beforeScript);
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    private function hasScript(): bool
136
    {
137
        return !empty($this->script);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    private function hasEnvironment(): bool
144
    {
145
        return !empty($this->environment);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    private function hasOnly(): bool
152
    {
153
        return !empty($this->only);
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    private function hasExcept(): bool
160
    {
161
        return !empty($this->except);
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getJobName(): string
168
    {
169
        return $this->jobName;
170
    }
171
}
172