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

StorageDriverFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 8
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresInstanceOf() 0 4 1
B getFactoryMap() 0 26 4
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