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

GitLabCIFile::addCleanUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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