Passed
Pull Request — master (#1005)
by Maxim
10:03
created

RuntimeDirectory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Console\Sequence;
6
7
use Spiral\Boot\DirectoriesInterface;
8
use Spiral\Files\FilesInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Creates runtime directory or/and ensure proper permissions for it.
13
 */
14
final class RuntimeDirectory
15
{
16
    public function __construct(
17
        private readonly FilesInterface $files,
18
        private readonly DirectoriesInterface $dirs
19
    ) {
20
    }
21
22
    public function ensure(OutputInterface $output): void
23
    {
24
        $output->write('Verifying runtime directory... ');
25
26
        $runtimeDirectory = $this->dirs->get('runtime');
27
28
        if (!$this->files->exists($runtimeDirectory)) {
29
            $this->files->ensureDirectory($runtimeDirectory);
30
            $output->writeln('<comment>created</comment>');
31
            return;
32
        }
33
        $output->writeln('<info>exists</info>');
34
35
36
        foreach ($this->files->getFiles($runtimeDirectory) as $filename) {
37
            try {
38
                $this->files->setPermissions($filename, FilesInterface::RUNTIME);
39
                $this->files->setPermissions(\dirname($filename), FilesInterface::RUNTIME);
40
            } catch (\Throwable $e) {
41
                // @codeCoverageIgnoreStart
42
                $output->writeln(
43
                    \sprintf(
44
                        '<fg=red>[errored]</fg=red> `%s`: <fg=red>%s</fg=red>',
45
                        $this->files->relativePath($filename, $runtimeDirectory),
46
                        $e->getMessage()
47
                    )
48
                );
49
                continue;
50
                // @codeCoverageIgnoreEnd
51
            }
52
53
            if ($output->isVerbose()) {
54
                $output->writeln(
55
                    \sprintf(
56
                        '<fg=green>[updated]</fg=green> `%s`',
57
                        $this->files->relativePath($filename, $runtimeDirectory)
58
                    )
59
                );
60
            }
61
        }
62
63
        $output->writeln('Runtime directory permissions were updated.');
64
    }
65
}
66