ActivateAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 39
c 1
b 0
f 0
dl 0
loc 58
ccs 0
cts 19
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 53 4
1
<?php
2
3
namespace App\Action;
4
5
use App\Action\AbstractAction;
6
use App\Action\ActionInterface;
7
use App\Builder;
8
use App\Facades\Log;
9
use Ronanchilvers\Foundation\Config;
10
use Ronanchilvers\Utility\File;
11
use RuntimeException;
12
13
/**
14
 * Action to symlink the deployment in to the live location
15
 *
16
 * @author Ronan Chilvers <[email protected]>
17
 */
18
class ActivateAction extends AbstractAction implements ActionInterface
19
{
20
    /**
21
     * @see \App\Action\ActionInterface::run()
22
     */
23
    public function run(Config $configuration, Context $context)
24
    {
25
        $deployment    = $context->getOrThrow('deployment', 'Invalid or missing deployment');
26
        $projectDir    = $context->getOrThrow('project_base_dir', 'Missing or invalid project base directory');
27
        $deploymentDir = $context->getOrThrow('deployment_dir', 'Missing or invalid deployment directory');
28
        $linkFilename  = File::join($projectDir, 'current');
29
        Log::debug('Preparing to symlink deployment', [
30
            'deployment_dir' => $deploymentDir,
31
            'link_name' => $linkFilename,
32
        ]);
33
        if (file_exists($linkFilename)) {
34
            $this->info(
35
                $deployment,
36
                "Removing existing symlink: {$linkFilename}"
37
            );
38
            Log::debug('Removing existing symlink', [
39
                'deployment_dir' => $deploymentDir,
40
                'link_name' => $linkFilename,
41
            ]);
42
            if (!unlink($linkFilename)) {
43
                $this->error(
44
                    $deployment,
45
                    "Unable to remove existing symlink: {$linkFilename}"
46
                );
47
                Log::error('Unable to remove existing symlink', [
48
                    'deployment_dir' => $deploymentDir,
49
                    'link_name' => $linkFilename,
50
                ]);
51
                throw new RuntimeException('Unable to remove symlink prior to linking new deployment');
52
            }
53
        }
54
        Log::debug('Creating deployment symlink', [
55
            'deployment_dir' => $deploymentDir,
56
            'link_name' => $linkFilename,
57
        ]);
58
        if (!symlink($deploymentDir, $linkFilename)) {
59
            $this->error(
60
                $deployment,
61
                [
62
                    'Unable to create deployment symlink',
63
                    'Deployment Folder - ' . $deploymentDir,
64
                    'Link Name - ' . $linkFilename,
65
                ]
66
            );
67
            Log::debug('Unable to create symlink', [
68
                'deployment_dir' => $deploymentDir,
69
                'link_name' => $linkFilename,
70
            ]);
71
            throw new RuntimeException('Unable to activate deployment symlink');
72
        }
73
        $this->info(
74
            $deployment,
75
            "Deployment activated: {$linkFilename} -> {$deploymentDir}"
76
        );
77
    }
78
}
79