Completed
Push — master ( d40022...a8a6e5 )
by Arne
01:50
created

StorageDriverFactory::getFactoryMap()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Archivr\StorageDriver;
4
5
use Archivr\AbstractFactory;
6
use Archivr\Exception\ConfigurationException;
7
use Archivr\Exception\ConflictException;
8
use Archivr\PathUtils;
9
use Archivr\VaultConfiguration;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\Filesystem;
12
13
final class StorageDriverFactory extends AbstractFactory
14
{
15
    protected static function requiresInstanceOf(): string
16
    {
17
        return StorageDriverInterface::class;
18
    }
19
20
    protected static function getFactoryMap(): array
21
    {
22
        $return = [];
23
24
        $return['local'] = function(VaultConfiguration $vaultConfiguration)
25
        {
26
            if (!($path = $vaultConfiguration->getSetting('path')))
27
            {
28
                throw new ConflictException("Missing vault config setting 'path' for vault '{$vaultConfiguration->getTitle()}'.'");
29
            }
30
31
            $path = PathUtils::getAbsolutePath($path);
32
33
            if (!is_dir($path) || !is_writable($path))
34
            {
35
                throw new ConfigurationException(sprintf('Path "%s" does not exist or is not writable.', $path));
36
            }
37
38
            $adapter = new Local($path);
39
            $filesystem = new Filesystem($adapter);
40
41
            return new FlysystemStorageDriver($filesystem);
42
        };
43
44
        return $return;
45
    }
46
}
47