WritablesAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 38
c 2
b 0
f 0
dl 0
loc 61
ccs 0
cts 20
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 56 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 App\Facades\Settings;
10
use Ronanchilvers\Foundation\Config;
11
use Ronanchilvers\Utility\File;
12
use RuntimeException;
13
14
/**
15
 * Action to manage the writable locations for a project
16
 *
17
 * @author Ronan Chilvers <[email protected]>
18
 */
19
class WritablesAction extends AbstractAction
20
{
21
    /**
22
     * @see \App\Action\ActionInterface::run()
23
     */
24
    public function run(Config $configuration, Context $context)
25
    {
26
        $deployment    = $context->getOrThrow('deployment', 'Invalid or missing deployment');
27
        $deploymentDir = $context->getOrThrow('deployment_dir', 'Invalid or missing deployment directory');
28
        $writableMode  = Settings::get('build.chmod.writable_folder', Builder::MODE_WRITABLE_DIR);
29
        $writables     = $configuration->get('writables.paths', []);
30
        if (0 == count($writables)) {
31
            $this->info(
32
                $deployment,
33
                'No writables found in the deployment configuration'
34
            );
35
            return;
36
        }
37
        foreach ($writables as $writable) {
38
            $dir = realpath(File::join($deploymentDir, $writable));
39
            // $this->info(
40
            //     $deployment,
41
            //     'Writable: ' . $writable
42
            // );
43
            Log::debug("Working on writable", [
44
                'writable' => $writable,
45
                'writable_dir' => $dir,
46
            ]);
47
            if (!is_dir($dir)) {
48
                $this->error(
49
                    $deployment,
50
                    [
51
                        'Writable path does not exist',
52
                        'Writable - ' . $writable,
53
                        'Writable Folder - ' . $dir,
54
                    ]
55
                );
56
                Log::error("Writable doesn't exist", [
57
                    'writable' => $writable,
58
                    'writable_dir' => $dir,
59
                ]);
60
                throw new RuntimeException("Writable doesn't exist - " . $writable);
61
            }
62
            if (!chmod($dir, $writableMode)) {
63
                $this->error(
64
                    $deployment,
65
                    [
66
                        'Unable to chmod writable',
67
                        'Writable - ' . $writable,
68
                        'Writable Folder - ' . $dir,
69
                    ]
70
                );
71
                Log::error('Unable to chmod writable', [
72
                    'writable' => $writable,
73
                    'writable_dir' => $dir,
74
                ]);
75
                throw new RuntimeException('Unable to chmod writable dir ' . $writable);
76
            }
77
            $this->info(
78
                $deployment,
79
                "Writable verified: {$writable} @ {$dir}"
80
            );
81
        }
82
    }
83
}
84