SetupCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
eloc 43
dl 0
loc 83
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B createPath() 0 48 9
A execute() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle\Cli;
6
7
use Fabiang\AsseticBundle\Service;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
use function decoct;
13
use function is_dir;
14
use function is_readable;
15
use function is_writable;
16
use function mkdir;
17
18
class SetupCommand extends Command
19
{
20
    /**
21
     * The assetic service
22
     */
23
    private Service $assetic;
24
25
    public function __construct(Service $assetic)
26
    {
27
        parent::__construct('setup');
28
        $this->assetic = $assetic;
29
        $this->setDescription('Create cache and assets directories with valid permissions.');
30
    }
31
32
    /**
33
     * Executes the current command.
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37
        $config = $this->assetic->getConfiguration();
38
        $mode   = null !== ($mode   = $config->getUmask()) ? $mode : 0775;
39
40
        if (! $this->createPath($output, 'Cache', $config->getCachePath() ?? '', $mode)) {
41
            return 1;
42
        }
43
        if (! $this->createPath($output, 'Web', $config->getWebPath() ?? '', $mode)) {
44
            return 1;
45
        }
46
47
        return 0;
48
    }
49
50
    /**
51
     * Creates a path with the needed permissions
52
     */
53
    private function createPath(OutputInterface $output, string $which, string $path, int $mode): bool
54
    {
55
        $displayMode = decoct($mode);
56
        $pathExists  = is_dir($path);
57
        if (! $path) {
58
            $output->writeln('Creation of ' . $which . ' path skipped - no path provided');
59
60
            return true;
61
        }
62
        if (! $pathExists) {
63
            if (mkdir($path, $mode, true)) {
64
                $output->writeln($which . ' path created "' . $path . '" with mode "' . $displayMode . '"');
65
66
                return true;
67
            } else {
68
                $output->writeln('<error>' . $which . ' path "' . $path . '" could not be created.</error>');
69
70
                return false;
71
            }
72
        }
73
74
        $readable = is_readable($path);
75
        $writable = is_writable($path);
76
        if ($readable && $writable) {
77
            $output->writeln(
78
                'Creation of ' . $which . ' path "' . $path . '" skipped - path exists with correct permissions'
79
            );
80
81
            return true;
82
        } elseif (! $readable && ! $writable) {
83
            $output->writeln(
84
                '<error>Creation of ' . $which . ' path "'
85
                    . $path
86
                    . '" failed - path exists but is neither readable nor writable</error>'
87
            );
88
        } elseif (! $readable) {
89
            $output->writeln(
90
                '<error>Creation of ' . $which . ' path "'
91
                    . $path . '" failed - path exists but is not readable</error>'
92
            );
93
        } else {
94
            $output->writeln(
95
                '<error>Creation of ' . $which . ' path "'
96
                    . $path . '" failed - path exists but is not writable</error>'
97
            );
98
        }
99
100
        return false;
101
    }
102
}
103