ReleasesStrategy::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Deploy\Strategy;
13
14
use Mage\Runtime\Exception\RuntimeException;
15
use Mage\Runtime\Runtime;
16
17
/**
18
 * Strategy for Deployment with Releases, using Tar and SCP
19
 *
20
 * @author Andrés Montañez <[email protected]>
21
 */
22
class ReleasesStrategy implements StrategyInterface
23
{
24
    protected Runtime $runtime;
25
26 19
    public function getName(): string
27
    {
28 19
        return 'Releases';
29
    }
30
31 21
    public function setRuntime(Runtime $runtime): void
32
    {
33 21
        $this->runtime = $runtime;
34
    }
35
36 19
    public function getPreDeployTasks(): array
37
    {
38 19
        $this->checkStage(Runtime::PRE_DEPLOY);
39 18
        $tasks = $this->runtime->getTasks();
40
41
        if (
42 18
            ($this->runtime->getBranch() || $this->runtime->getTag()) &&
43 18
            !$this->runtime->inRollback() &&
44 18
            !in_array('git/change-branch', $tasks)
45
        ) {
46 17
            array_unshift($tasks, 'git/change-branch');
47
        }
48
49 18
        if (!$this->runtime->inRollback() && !in_array('deploy/tar/prepare', $tasks)) {
50 17
            array_push($tasks, 'deploy/tar/prepare');
51
        }
52
53 18
        return $tasks;
54
    }
55
56 19
    public function getOnDeployTasks(): array
57
    {
58 19
        $this->checkStage(Runtime::ON_DEPLOY);
59 18
        $tasks = $this->runtime->getTasks();
60
61 18
        if (!$this->runtime->inRollback() && !in_array('deploy/tar/copy', $tasks)) {
62 17
            array_unshift($tasks, 'deploy/tar/copy');
63
        }
64
65 18
        if (!$this->runtime->inRollback() && !in_array('deploy/release/prepare', $tasks)) {
66 17
            array_unshift($tasks, 'deploy/release/prepare');
67
        }
68
69 18
        return $tasks;
70
    }
71
72 15
    public function getOnReleaseTasks(): array
73
    {
74 15
        $this->checkStage(Runtime::ON_RELEASE);
75 14
        $tasks = $this->runtime->getTasks();
76
77 14
        if (!in_array('deploy/release', $tasks)) {
78 14
            array_unshift($tasks, 'deploy/release');
79
        }
80
81 14
        return $tasks;
82
    }
83
84 15
    public function getPostReleaseTasks(): array
85
    {
86 15
        $this->checkStage(Runtime::POST_RELEASE);
87 14
        $tasks = $this->runtime->getTasks();
88
89 14
        if (!in_array('deploy/release/cleanup', $tasks)) {
90 14
            array_unshift($tasks, 'deploy/release/cleanup');
91
        }
92
93 14
        return $tasks;
94
    }
95
96 13
    public function getPostDeployTasks(): array
97
    {
98 13
        $this->checkStage(Runtime::POST_DEPLOY);
99 12
        $tasks = $this->runtime->getTasks();
100
101
        if (
102 12
            ($this->runtime->getBranch() || $this->runtime->getTag()) &&
103 12
            !$this->runtime->inRollback() &&
104 12
            !in_array('git/change-branch', $tasks)
105
        ) {
106 11
            array_unshift($tasks, 'git/change-branch');
107
        }
108
109 12
        if (!$this->runtime->inRollback() && !in_array('deploy/tar/cleanup', $tasks)) {
110 11
            array_unshift($tasks, 'deploy/tar/cleanup');
111
        }
112
113 12
        return $tasks;
114
    }
115
116
    /**
117
     * Check the runtime stage is correct
118
     *
119
     * @throws RuntimeException
120
     */
121 19
    private function checkStage(string $stage): void
122
    {
123 19
        if ($this->runtime->getStage() !== $stage) {
124 1
            throw new RuntimeException(
125 1
                sprintf('Invalid stage, got "%s" but expected "%s"', $this->runtime->getStage(), $stage)
126
            );
127
        }
128
    }
129
}
130