Passed
Push — main ( 764702...024f60 )
by Daniel
04:19
created

AssetPathValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 12
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 31 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Setup\Validator;
6
7
use Uxmp\Core\Component\Setup\Validator\Exception\EnvironmentValidationException;
8
9
final class AssetPathValidator implements ValidatorInterface
10
{
11
    private const ASSET_SUB_FOLDERS = ['img/album', 'img/artist'];
12
13
    private const PERMISSIONS = 0766;
14
15
    public function validate(): void
16
    {
17
        $assetPath = $_ENV['ASSET_PATH'] ?? '';
18
        if ($assetPath === '') {
19
            throw new EnvironmentValidationException(
20
                'ASSET_PATH is not set in config'
21
            );
22
        }
23
24
        if (!is_writeable($assetPath)) {
25
            throw new EnvironmentValidationException(
26
                sprintf(
27
                    'ASSET_PATH `%s` is not a valid writeable directory',
28
                    $assetPath
29
                )
30
            );
31
        }
32
33
        foreach (static::ASSET_SUB_FOLDERS as $folder) {
34
            $path = sprintf('%s/%s', $assetPath, $folder);
35
36
            if (!is_dir($path)) {
37
                $result = mkdir(
38
                    $path,
39
                    static::PERMISSIONS,
40
                    true
41
                );
42
43
                if ($result === false) {
44
                    throw new EnvironmentValidationException(
45
                        sprintf('Creation of folder `%s` failed', $path)
46
                    );
47
                }
48
            }
49
        }
50
    }
51
}
52