CreateWorkspaceAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 29
c 1
b 0
f 0
dl 0
loc 48
ccs 26
cts 26
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 43 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 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 3
    public function run(Config $configuration, Context $context)
27
    {
28 3
        $projectDir    = $context->getOrThrow('project_base_dir', 'Invalid or missing project base directory');
29 3
        $deploymentDir = $context->getOrThrow('deployment_base_dir', 'Invalid or missing deployment base directory');
30 3
        $deployment    = $context->getOrThrow('deployment', 'Invalid or missing deployment');
31 3
        $locations     = [$projectDir, $deploymentDir];
32 3
        $mode          = Settings::get('build.chmod.default_folder', Builder::MODE_DEFAULT);
33 3
        foreach ($locations as $location) {
34 3
            if (is_dir($location)) {
35 1
                $this->info(
36
                    $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 2
            Log::debug('Creating build directory', [
47 2
                'location' => $location,
48 2
                'mode' => $mode,
49
            ]);
50 2
            if (!mkdir($location, $mode, true)) {
51 1
                $this->error(
52
                    $deployment,
53
                    [
54 1
                        "Failed to create location: {$location}",
55
                    ]
56
                );
57 1
                Log::error('Unable to create build directory', [
58 1
                    'location' => $location,
59 1
                    'mode' => $mode,
60
                ]);
61 1
                throw new RuntimeException(
62 1
                    'Unable to create build directory at ' . $location
63
                );
64
            }
65 1
            $this->info(
66
                $deployment,
67
                [
68 1
                    "Created location: {$location}",
69
                ]
70
            );
71
        }
72 2
    }
73
}
74