Passed
Push — master ( e7a7f9...d54a1d )
by Ronan
04:18
created

CreateWorkspaceAction::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.8006

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 43
ccs 15
cts 29
cp 0.5172
rs 9.472
cc 4
nc 4
nop 2
crap 5.8006
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 RuntimeException;
12
13
/**
14
 * Action to create the workspace for a given project
15
 *
16
 * - calculates the base paths for the project workspace
17
 * - ensures the base paths all exist
18
 *
19
 * @author Ronan Chilvers <[email protected]>
20
 */
21
class CreateWorkspaceAction extends AbstractAction
22
{
23
    /**
24
     * @see \App\Action\ActionInterface::run()
25
     */
26 1
    public function run(Config $configuration, Context $context)
27
    {
28 1
        $projectDir    = $context->getOrThrow('project_base_dir', 'Invalid or missing project base directory');
29 1
        $deploymentDir = $context->getOrThrow('deployment_base_dir', 'Invalid or missing deployment base directory');
30 1
        $deployment    = $context->getOrThrow('deployment', 'Invalid or missing deployment');
31 1
        $locations     = [$projectDir, $deploymentDir];
32 1
        $mode          = Settings::get('build.chmod.default_folder', Builder::MODE_DEFAULT);
33 1
        foreach ($locations as $location) {
34 1
            if (is_dir($location)) {
35
                $this->info(
36
                    $deployment,
37
                    [
38
                        'Location exists: ' . $location,
39
                    ]
40
                );
41
                Log::debug('Build directory exists', [
42
                    'location' => $location,
43
                ]);
44
                continue;
45
            }
46 1
            Log::debug('Creating build directory', [
47 1
                'location' => $location,
48 1
                'mode' => $mode,
49
            ]);
50 1
            if (!mkdir($location, $mode, true)) {
51
                $this->error(
52
                    $deployment,
53
                    [
54
                        "Failed to create location: {$location} (mode {$mode})",
55
                    ]
56
                );
57
                Log::error('Unable to create build directory', [
58
                    'location' => $location,
59
                    'mode' => $mode,
60
                ]);
61
                throw new RuntimeException(
62
                    'Unable to create build directory at ' . $location
63
                );
64
            }
65 1
            $this->info(
66 1
                $deployment,
67
                [
68 1
                    "Created location: {$location} (mode {$mode})",
69
                ]
70
            );
71
        }
72 1
    }
73
}
74