Completed
Push — master ( 4fe608...5ceeaf )
by Jindun
12s
created

BranchesModel::getBranch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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