Completed
Push — master ( fef7a6...aff413 )
by Arne
02:38
created

StorageDriverFactory::getFactoryMap()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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