Passed
Push — master ( c55922...0e93a7 )
by Ronan
04:05
created

AbstractAction::info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 3
crap 2.032
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 1
    public function getKey()
40
    {
41 1
        $reflection = new ReflectionClass($this);
42 1
        $name       = Str::snake(
43 1
            str_replace(
44 1
                'Action',
45 1
                '',
46 1
                $reflection->getShortName()
47
            )
48
        );
49
50 1
        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()]
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()
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, string $header, $detail = '')
132
    {
133 1
        if (is_array($detail)) {
134
            $detail = implode("\n", $detail);
135
        }
136 1
        return $this->eventFinder->event(
137 1
            EventFinder::INFO,
138
            $deployment,
139
            $header,
140
            $detail
141
        );
142
    }
143
144
    /**
145
     * Log an error event
146
     *
147
     * @param \App\Model\Deployment $deployment
148
     * @param string $header
149
     * @param mixed $detail
150
     * @return bool|\App\Model\Event
151
     * @author Ronan Chilvers <[email protected]>
152
     */
153 1
    protected function error(Deployment $deployment, string $header, $detail = '')
154
    {
155 1
        if (is_array($detail)) {
156
            $detail = implode("\n", $detail);
157
        }
158 1
        return $this->eventFinder->event(
159 1
            EventFinder::ERROR,
160
            $deployment,
161
            $header,
162
            $detail
163
        );
164
    }
165
}
166