ClearPathsAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 29
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 17
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 42 5
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 remove specific paths from the deployment prior to symlinking
15
 *
16
 * @author Ronan Chilvers <[email protected]>
17
 */
18
class ClearPathsAction extends AbstractAction
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
        $deploymentDir = $context->getOrThrow('deployment_dir', 'Missing or invalid deployment directory');
27
        Log::debug('Preparing to clear paths from deployment', [
28
            'deployment'     => $deployment->toArray(),
29
            'deployment_dir' => $deploymentDir,
30
        ]);
31
        $clearPaths = $configuration->get('clear_paths.paths', []);
32
        if (0 == count($clearPaths)) {
33
            $this->info(
34
                $deployment,
35
                'No paths configured for clearing'
36
            );
37
            return;
38
        }
39
        foreach ($clearPaths as $path) {
40
            $fullPath = File::join($deploymentDir, $path);
41
            // is_readable works for both files and directories
42
            if (!is_readable($fullPath)) {
43
                $this->info(
44
                    $deployment,
45
                    [
46
                        'Path to clear was not found when clearing - ' . $path,
47
                        'Full path - ' . $fullPath,
48
                    ]
49
                );
50
                continue;
51
            }
52
            if (!File::rm($fullPath)) {
53
                $this->error(
54
                    $deployment,
55
                    [
56
                        'Path to clear couldn\'t be removed',
57
                        'Full path - ' . $fullPath,
58
                    ]
59
                );
60
                throw new RuntimeException('Unable to remove clear path - ' . $path);
61
            }
62
            $this->info(
63
                $deployment,
64
                "Cleared path: {$path} @ {$fullPath}"
65
            );
66
        }
67
    }
68
}
69