Passed
Push — master ( d1c5a6...bac8bf )
by Fabian
03:36 queued 12s
created

SetupCommand::createPath()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 44
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 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
class SetupCommand extends Command
13
{
14
15
    /**
16
     * The assetic service
17
     */
18
    private Service $assetic;
19
20
    /**
21
     * Constructor.
22
     */
23
    public function __construct(Service $assetic)
24
    {
25
        parent::__construct('setup');
26
        $this->assetic = $assetic;
27
        $this->setDescription('Create cache and assets directories with valid permissions.');
28
    }
29
30
    /**
31
     * Executes the current command.
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35
        $config = $this->assetic->getConfiguration();
36
        $mode   = (null !== ($mode   = $config->getUmask())) ? $mode : 0775;
37
38
        if (!$this->createPath($output, 'Cache', $config->getCachePath() ?? '', $mode)) {
39
            return 1;
40
        }
41
        if (!$this->createPath($output, 'Web', $config->getWebPath() ?? '', $mode)) {
42
            return 1;
43
        }
44
45
        return 0;
46
    }
47
48
    /**
49
     * Creates a path with the needed permissions
50
     */
51
    private function createPath(OutputInterface $output, string $which, string $path, int $mode): bool
52
    {
53
        $displayMode = decoct($mode);
54
        $pathExists  = is_dir($path);
55
        if (!$path) {
56
            $output->writeln('Creation of ' . $which . ' path skipped - no path provided');
57
58
            return true;
59
        }
60
        if (!$pathExists) {
61
            if (mkdir($path, $mode, true)) {
62
                $output->writeln($which . ' path created "' . $path . '" with mode "' . $displayMode . '"');
63
64
                return true;
65
            } else {
66
                $output->writeln('<error>' . $which . ' path "' . $path . '" could not be created.</error>');
67
68
                return false;
69
            }
70
        }
71
72
        $readable = is_readable($path);
73
        $writable = is_writable($path);
74
        if ($readable && $writable) {
75
            $output->writeln(
76
                'Creation of ' . $which . ' path "' . $path . '" skipped - path exists with correct permissions'
77
            );
78
79
            return true;
80
        } elseif (!$readable && !$writable) {
81
            $output->writeln(
82
                '<error>Creation of ' . $which . ' path "' . $path . '" failed - path exists but is neither readable nor writable</error>'
83
            );
84
        } elseif (!$readable) {
85
            $output->writeln(
86
                '<error>Creation of ' . $which . ' path "' . $path . '" failed - path exists but is not readable</error>'
87
            );
88
        } else {
89
            $output->writeln(
90
                '<error>Creation of ' . $which . ' path "' . $path . '" failed - path exists but is not writable</error>'
91
            );
92
        }
93
94
        return false;
95
    }
96
97
}
98