| Total Complexity | 7 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | final class AssetPathValidator implements ValidatorInterface |
||
| 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 readonly 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 | ); |
||
| 64 |