BranchesModel::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
4
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job\Model;
5
6
use TheAentMachine\Aent\Context\ContextInterface;
7
use TheAentMachine\AentGitLabCI\Exception\JobException;
8
use TheAentMachine\Aenthill\Aenthill;
9
10
final class BranchesModel implements ContextInterface
11
{
12
    /** @var string[] */
13
    private $branches;
14
15
    /** @var string[] */
16
    private $branchesToIgnore;
17
18
    /**
19
     * BranchesModel constructor.
20
     * @param string[] $branches
21
     * @param string[] $branchesToIgnore
22
     * @throws JobException
23
     */
24
    public function __construct(array $branches, array $branchesToIgnore = [])
25
    {
26
        if (empty($branches)) {
27
            throw JobException::branchIsNull();
28
        }
29
        $this->branches = $branches;
30
        $this->branchesToIgnore = $branchesToIgnore;
31
    }
32
33
    /**
34
     * @return void
35
     */
36
    public function toMetadata(): void
37
    {
38
        Aenthill::update([
39
            'BRANCHES' => \implode(';', $this->branches),
40
            'BRANCHES_TO_IGNORE' => \implode(';', $this->branchesToIgnore),
41
        ]);
42
    }
43
44
    /**
45
     * @return self
46
     * @throws JobException
47
     */
48
    public static function fromMetadata(): self
49
    {
50
        $branches = \explode(';', Aenthill::metadata('BRANCHES'));
51
        $branchesToIgnore = \explode(';', Aenthill::metadata('BRANCHES_TO_IGNORE'));
52
        return new self($branches, $branchesToIgnore);
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isSingleBranch(): bool
59
    {
60
        return (count($this->branches) === 1 && $this->branches[0] !== 'branches');
61
    }
62
63
    /**
64
     * @return string[]
65
     */
66
    public function getBranches(): array
67
    {
68
        return $this->branches;
69
    }
70
71
    /**
72
     * @return string[]
73
     */
74
    public function getBranchesToIgnore(): array
75
    {
76
        return $this->branchesToIgnore;
77
    }
78
}
79