Completed
Pull Request — master (#1)
by Jindun
04:17
created

GitLabCIFile   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 47
dl 0
loc 135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 4
A addDeploy() 0 10 2
A findOrCreate() 0 8 2
A exist() 0 3 1
A addBuild() 0 10 2
A addStages() 0 19 2
A __construct() 0 3 1
A addCleanUp() 0 10 2
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\GitLabCI;
4
5
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException;
6
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractBuildJob;
7
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractCleanupJob;
8
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractDeployJob;
9
use TheAentMachine\Aenthill\Pheromone;
10
use TheAentMachine\Exception\MissingEnvironmentVariableException;
11
use TheAentMachine\YamlTools\YamlTools;
12
13
final class GitLabCIFile
14
{
15
    public const DEFAULT_FILENAME = '.gitlab-ci.yml';
16
17
    /** @var string */
18
    private $path;
19
20
    /** @var \SplFileInfo */
21
    private $file;
22
23
    /**
24
     * GitLabCIFile constructor.
25
     * @throws MissingEnvironmentVariableException
26
     */
27
    public function __construct()
28
    {
29
        $this->path = Pheromone::getContainerProjectDirectory() . '/' . self::DEFAULT_FILENAME;
30
    }
31
32
    public function exist(): bool
33
    {
34
        return \file_exists($this->path);
35
    }
36
37
    /**
38
     * @return GitLabCIFile
39
     * @throws GitLabCIFileException
40
     */
41
    public function findOrCreate(): self
42
    {
43
        if (!$this->exist()) {
44
            return $this->create()->addStages();
45
        }
46
47
        $this->file = new \SplFileInfo($this->path);
48
        return $this;
49
    }
50
51
    private function create(): self
52
    {
53
        if ($this->exist()) {
54
            return $this;
55
        }
56
57
        \file_put_contents($this->path, '');
58
59
        $fileOwner = \fileowner(\dirname($this->path));
60
        if (!is_bool($fileOwner)) {
0 ignored issues
show
introduced by
The condition is_bool($fileOwner) is always false.
Loading history...
61
            \chown($this->path, $fileOwner);
62
        }
63
64
        $fileGroup = \filegroup(\dirname($this->path));
65
        if (!is_bool($fileGroup)) {
0 ignored issues
show
introduced by
The condition is_bool($fileGroup) is always false.
Loading history...
66
            \chgrp($this->path, $fileGroup);
67
        }
68
69
        $this->file = new \SplFileInfo($this->path);
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return GitLabCIFile
76
     * @throws GitLabCIFileException
77
     */
78
    private function addStages(): self
79
    {
80
        if (!$this->exist()) {
81
            throw GitLabCIFileException::missingFile();
82
        }
83
84
        $stages = [
85
            'stages' => [
86
                'test',
87
                'build',
88
                'deploy',
89
                'cleanup',
90
            ],
91
        ];
92
93
        $yaml = YamlTools::dump($stages);
94
        \file_put_contents($this->path, $yaml);
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param AbstractBuildJob $job
101
     * @return GitLabCIFile
102
     * @throws GitLabCIFileException
103
     */
104
    public function addBuild(AbstractBuildJob $job): self
105
    {
106
        if (!$this->exist()) {
107
            throw GitLabCIFileException::missingFile();
108
        }
109
110
        $yaml = YamlTools::dump($job->dump());
111
        YamlTools::mergeContentIntoFile($yaml, $this->path);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param AbstractDeployJob $job
118
     * @return GitLabCIFile
119
     * @throws GitLabCIFileException
120
     */
121
    public function addDeploy(AbstractDeployJob $job): self
122
    {
123
        if (!$this->exist()) {
124
            throw GitLabCIFileException::missingFile();
125
        }
126
127
        $yaml = YamlTools::dump($job->dump());
128
        YamlTools::mergeContentIntoFile($yaml, $this->path);
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param AbstractCleanupJob $job
135
     * @return GitLabCIFile
136
     * @throws GitLabCIFileException
137
     */
138
    public function addCleanUp(AbstractCleanupJob $job): self
139
    {
140
        if (!$this->exist()) {
141
            throw GitLabCIFileException::missingFile();
142
        }
143
144
        $yaml = YamlTools::dump($job->dump());
145
        YamlTools::mergeContentIntoFile($yaml, $this->path);
146
147
        return $this;
148
    }
149
}
150