Passed
Pull Request — master (#806)
by Maxim
19:17
created

RuntimeDirectory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 6
eloc 25
c 0
b 0
f 0
dl 0
loc 50
ccs 17
cts 18
cp 0.9444
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A ensure() 0 42 5
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 11
    public function __construct(
17
        private readonly FilesInterface $files,
18
        private readonly DirectoriesInterface $dirs
19
    ) {
20
    }
21
22 11
    public function ensure(OutputInterface $output): void
23
    {
24 11
        $output->write('Verifying runtime directory... ');
25
26 11
        $runtimeDirectory = $this->dirs->get('runtime');
27
28 11
        if (!$this->files->exists($runtimeDirectory)) {
29 10
            $this->files->ensureDirectory($runtimeDirectory);
30 10
            $output->writeln('<comment>created</comment>');
31 10
            return;
32
        }
33 1
        $output->writeln('<info>exists</info>');
34
35
36 1
        foreach ($this->files->getFiles($runtimeDirectory) as $filename) {
37
            try {
38 1
                $this->files->setPermissions($filename, FilesInterface::RUNTIME);
39 1
                $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 1
            if ($output->isVerbose()) {
54 1
                $output->writeln(
55 1
                    \sprintf(
56
                        '<fg=green>[updated]</fg=green> `%s`',
57 1
                        $this->files->relativePath($filename, $runtimeDirectory)
58
                    )
59
                );
60
            }
61
        }
62
63 1
        $output->writeln('Runtime directory permissions were updated.');
64
    }
65
}
66