Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

AssetPathValidator::validate()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 24
cp 0
rs 9.0111
cc 6
nc 6
nop 0
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Setup\Validator;
6
7
use Uxmp\Core\Component\Config\ConfigProviderInterface;
8
use Uxmp\Core\Component\Setup\Validator\Exception\EnvironmentValidationException;
9
10
final readonly class AssetPathValidator implements ValidatorInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 10 at column 6
Loading history...
11
{
12
    /**
13
     * @var string[]
14
     */
15
    private const ASSET_SUB_FOLDERS = ['img/album', 'img/artist'];
16
17
    /**
18
     * @var int
19
     */
20
    private const PERMISSIONS = 0o766;
21
22
    public function __construct(
23
        private ConfigProviderInterface $configProvider,
24
    ) {
25
    }
26
27
    public function validate(): void
28
    {
29
        $assetPath = $this->configProvider->getAssetPath();
30
        if ($assetPath === '') {
31
            throw new EnvironmentValidationException(
32
                'ASSET_PATH is not set in config'
33
            );
34
        }
35
36
        if (!is_writable($assetPath)) {
37
            throw new EnvironmentValidationException(
38
                sprintf(
39
                    'ASSET_PATH `%s` is not a valid writeable directory',
40
                    $assetPath
41
                )
42
            );
43
        }
44
45
        foreach (self::ASSET_SUB_FOLDERS as $folder) {
46
            $path = sprintf('%s/%s', $assetPath, $folder);
47
48
            if (!is_dir($path)) {
49
                $result = mkdir(
50
                    $path,
51
                    self::PERMISSIONS,
52
                    true
53
                );
54
55
                if (!$result) {
56
                    throw new EnvironmentValidationException(
57
                        sprintf('Creation of folder `%s` failed', $path)
58
                    );
59
                }
60
            }
61
        }
62
    }
63
}
64