Completed
Pull Request — master (#1)
by Jindun
06:14
created

BranchesModel::getBranchesToIgnore()   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 isMultipleBranches(): bool
58
    {
59
        $branchesCount = count($this->branches);
60
        return $branchesCount > 1 || ($branchesCount === 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