Passed
Push — master ( d54a1d...1a6635 )
by Ronan
05:02
created

CreateWorkspaceAction::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.336

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 43
ccs 21
cts 29
cp 0.7241
rs 9.472
cc 4
nc 4
nop 2
crap 4.336
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 2
    public function run(Config $configuration, Context $context)
27
    {
28 2
        $projectDir    = $context->getOrThrow('project_base_dir', 'Invalid or missing project base directory');
29 2
        $deploymentDir = $context->getOrThrow('deployment_base_dir', 'Invalid or missing deployment base directory');
30 2
        $deployment    = $context->getOrThrow('deployment', 'Invalid or missing deployment');
31 2
        $locations     = [$projectDir, $deploymentDir];
32 2
        $mode          = Settings::get('build.chmod.default_folder', Builder::MODE_DEFAULT);
33 2
        foreach ($locations as $location) {
34 2
            if (is_dir($location)) {
35 1
                $this->info(
36 1
                    $deployment,
37
                    [
38 1
                        'Location exists: ' . $location,
39
                    ]
40
                );
41 1
                Log::debug('Build directory exists', [
42 1
                    'location' => $location,
43
                ]);
44 1
                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 2
    }
73
}
74