Passed
Pull Request — master (#23)
by Ronan
04:13
created

AbstractAction::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace App\Action;
4
5
use App\Action\ActionInterface;
6
use App\Action\Context;
7
use App\Facades\Log;
8
use App\Model\Deployment;
9
use App\Model\Finder\EventFinder;
10
use ReflectionClass;
11
use Ronanchilvers\Foundation\Config;
12
use Ronanchilvers\Foundation\Traits\Optionable;
13
use Ronanchilvers\Utility\Str;
14
use RuntimeException;
15
use Symfony\Component\Process\Process;
16
17
/**
18
 * Action to symlink the deployment in to the live location
19
 *
20
 * @author Ronan Chilvers <[email protected]>
21
 */
22
abstract class AbstractAction implements ActionInterface
23
{
24
    use Optionable;
25
26
    /**
27
     * @var \App\Model\Finder\EventFinder
28
     */
29
    protected $eventFinder = null;
30
31
    /**
32
     * @var boolean
33
     */
34
    protected $hookable = true;
35
36
    /**
37
     * @see \App\Action\ActionInterface::getKey()
38
     */
39 3
    public function getKey()
40
    {
41 3
        $reflection = new ReflectionClass($this);
42 3
        $name       = Str::snake(
43 3
            str_replace(
44 3
                'Action',
45 3
                '',
46 3
                $reflection->getShortName()
47
            )
48
        );
49
50 3
        return $name;
51
    }
52
53
    /**
54
     * @author Ronan Chilvers <[email protected]>
55
     */
56 2
    public function setEventFinder(EventFinder $eventFinder)
57
    {
58 2
        $this->eventFinder = $eventFinder;
59 2
    }
60
61
    /**
62
     * @author Ronan Chilvers <[email protected]>
63
     */
64 1
    public function isHookable()
65
    {
66 1
        return $this->hookable;
67
    }
68
69
    /**
70
     * @author Ronan Chilvers <[email protected]>
71
     */
72
    public function runHooks($hook, Config $configuration, Context $context)
73
    {
74
        $deployment = $context->getOrThrow(
75
            'deployment',
76
            'Invalid or missing deployment in hook runner'
77
        );
78
        $deploymentDir = $context->getOrThrow(
79
            'deployment_dir',
80
            'Invalid or missing deployment directory'
81
        );
82
        $hook = strtolower($hook);
83
        if (!in_array($hook, ['before', 'after'])) {
84
            return;
85
        }
86
        $key   = $this->getKey() . '.' . $hook;
87
        $hooks = $configuration->get($key);
88
        if (!is_array($hooks) || empty($hooks)) {
89
            Log::debug('No hooks defined', [
90
                'key' => $key
91
            ]);
92
            return;
93
        }
94
        foreach ($hooks as $command) {
95
            $this->info(
96
                $deployment,
97
                sprintf('%s hook running - %s', $key, $command)
98
            );
99
            $process = new Process($command, $deploymentDir);
100
            $process->run();
101
            if (!$process->isSuccessful()) {
102
                $this->error(
103
                    $deployment,
104
                    sprintf('%s hook failed to run : %s', $key, $command),
105
                    [$process->getOutput, $process->getErrorOutput()]
0 ignored issues
show
Unused Code introduced by
The call to App\Action\AbstractAction::error() has too many arguments starting with array($process->getOutpu...cess->getErrorOutput()). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
                $this->/** @scrutinizer ignore-call */ 
106
                       error(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
106
                );
107
                throw new RuntimeException('Unable to run deployment hook');
108
            }
109
            $this->info(
110
                $deployment,
111
                sprintf('%s hook ran successfully', $key),
112
                $process->getOutput()
0 ignored issues
show
Unused Code introduced by
The call to App\Action\AbstractAction::info() has too many arguments starting with $process->getOutput(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            $this->/** @scrutinizer ignore-call */ 
113
                   info(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
113
            );
114
        }
115
    }
116
117
    /**
118
     * @see \App\Action\ActionInterface::run()
119
     */
120
    abstract public function run(Config $configuration, Context $context);
121
122
    /**
123
     * Log an info event
124
     *
125
     * @param \App\Model\Deployment $deployment
126
     * @param string $header
127
     * @param mixed $detail
128
     * @return bool|\App\Model\Event
129
     * @author Ronan Chilvers <[email protected]>
130
     */
131 1
    protected function info(Deployment $deployment, $detail = '')
132
    {
133 1
        return $this->event(
134 1
            EventFinder::INFO,
135 1
            $deployment,
136 1
            $detail
137
        );
138
    }
139
140
    /**
141
     * Log an error event
142
     *
143
     * @param \App\Model\Deployment $deployment
144
     * @param string $header
145
     * @param mixed $detail
146
     * @return bool|\App\Model\Event
147
     * @author Ronan Chilvers <[email protected]>
148
     */
149 1
    protected function error(Deployment $deployment, $detail = '')
150
    {
151 1
        return $this->event(
152 1
            EventFinder::ERROR,
153 1
            $deployment,
154 1
            $detail
155
        );
156
    }
157
158
    /**
159
     * Log an event
160
     *
161
     * @param string $type
162
     * @param \App\Model\Deployment $deployment
163
     * @param string $header
164
     * @param mixed $detail
165
     * @return bool|\App\Model\Event
166
     * @author Ronan Chilvers <[email protected]>
167
     */
168 2
    protected function event(string $type, Deployment $deployment, $detail = '')
169
    {
170 2
        if (is_array($detail)) {
171
            $detail = implode("\n", $detail);
172
        }
173 2
        $header = ucwords(str_replace('_', ' ', $this->getKey()));
174 2
        return $this->eventFinder->event(
175 2
            $type,
176 2
            $deployment,
177 2
            $header,
178 2
            $detail
179
        );
180
    }
181
}
182