AbstractTask::displayReleases()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 0
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 6
nop 0
crap 30
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[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
13
namespace Rocketeer\Tasks;
14
15
use DateTime;
16
use Rocketeer\Interfaces\IdentifierInterface;
17
use Rocketeer\Services\Connections\Shell\Bash;
18
use Rocketeer\Services\Events\Listeners\TaggableListenerInterface;
19
use Rocketeer\Services\Events\Listeners\TaggableListenerTrait;
20
use Rocketeer\Services\Roles\HasRolesInterface;
21
use Rocketeer\Services\Roles\HasRolesTrait;
22
use Rocketeer\Traits\Properties\ConfigurableTrait;
23
use Rocketeer\Traits\Properties\HasEventsTrait;
24
use Rocketeer\Traits\SluggableTrait;
25
use Rocketeer\Traits\StepsRunnerTrait;
26
27
/**
28
 * An abstract AbstractTask with common helpers, from which all Tasks derive.
29
 */
30
abstract class AbstractTask extends Bash implements HasRolesInterface, IdentifierInterface, TaggableListenerInterface
31
{
32
    use ConfigurableTrait;
33
    use HasEventsTrait;
34
    use HasRolesTrait;
35
    use StepsRunnerTrait;
36
    use SluggableTrait;
37
    use TaggableListenerTrait;
38
39
    /**
40
     * The name of the task.
41
     *
42
     * @var string
43
     */
44
    protected $name;
45
46
    /**
47
     * A description of what the task does.
48
     *
49
     * @var string
50
     */
51
    protected $description;
52
53
    /**
54
     * A set of options that guide the entity.
55
     *
56
     * @var array
57
     */
58
    protected $options = [];
59
60
    /**
61
     * @var array
62
     */
63
    protected $roles = [];
64
65
    /**
66
     * @var bool
67
     */
68
    protected $local = false;
69
70
    ////////////////////////////////////////////////////////////////////
71
    ////////////////////////////// REFLECTION //////////////////////////
72
    ////////////////////////////////////////////////////////////////////
73
74
    /**
75
     * Get a global identifier for this entity.
76
     *
77
     * @return string
78
     */
79 442
    public function getIdentifier()
80
    {
81 442
        return 'tasks.'.$this->getSlug();
82
    }
83
84
    /**
85
     * Get what the task does.
86
     *
87
     * @return string
88
     */
89 123
    public function getDescription()
90
    {
91 123
        return $this->description;
92
    }
93
94
    /**
95
     * Change the task's name.
96
     *
97
     * @param string $name
98
     */
99 442
    public function setName($name)
100
    {
101 442
        $this->name = is_string($name) ? ucfirst($name) : $this->name;
102 442
    }
103
104
    /**
105
     * @param string $description
106
     */
107 442
    public function setDescription($description)
108
    {
109 442
        $this->description = $description ?: $this->description;
110 442
    }
111
112
    ////////////////////////////////////////////////////////////////////
113
    ////////////////////////////// EXECUTION ///////////////////////////
114
    ////////////////////////////////////////////////////////////////////
115
116
    /**
117
     * Run the task.
118
     *
119
     * @return mixed
120
     */
121
    abstract public function execute();
122
123
    /**
124
     * Fire the command.
125
     *
126
     * @return bool
127
     */
128 68
    public function fire()
129
    {
130 68
        $this->displayStatus();
131
132 68
        return $this->runWithBeforeAfterEvents(function () {
133 68
            return $this->local ? $this->on('local', [$this, 'execute']) : $this->execute();
134 68
        });
135
    }
136
137
    ////////////////////////////////////////////////////////////////////
138
    /////////////////////////////// HELPERS ////////////////////////////
139
    ////////////////////////////////////////////////////////////////////
140
141
    /**
142
     * Display a list of releases and their status.
143
     *
144
     * @codeCoverageIgnore
145
     */
146
    protected function displayReleases()
147
    {
148
        if (!$this->command) {
149
            return;
150
        }
151
152
        $key = 0;
153
        $rows = [];
154
        $releases = $this->releasesManager->getValidationFile();
155
156
        // Append the rows
157
        foreach ($releases as $name => $state) {
158
            $icon = $state ? '✓' : '✘';
159
            $color = $state ? 'green' : 'red';
160
            $date = DateTime::createFromFormat('YmdHis', $name)->format('Y-m-d H:i:s');
161
            $date = sprintf('<fg=%s>%s</fg=%s>', $color, $date, $color);
162
163
            // Add color to row
164
            $rows[] = [$key, $name, $date, $icon];
165
            ++$key;
166
        }
167
168
        // Render table
169
        $this->command->comment('Here are the available releases :');
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method comment does not exist on object<Rocketeer\Console...mmands\AbstractCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
170
        $this->command->table(
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method table does not exist on object<Rocketeer\Console...mmands\AbstractCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
171
            ['#', 'Path', 'Deployed at', 'Status'],
172
            $rows
173
        );
174
175
        return $rows;
176
    }
177
178
    /**
179
     * Display what the command is and does.
180
     */
181 68
    protected function displayStatus()
182
    {
183 68
        $name = $this->getName();
184 68
        $description = $this->getDescription();
185 68
        $time = $this->timer->getTime($this);
186 68
        $event = $this->event ? $this->event->getName() : null;
187
188 68
        $this->explainer->display($name, $description, $event, $time);
189 68
    }
190
}
191