Completed
Push — master ( c5a3b4...fa0d52 )
by Arne
01:49
created

StorageDriverFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Archivr\StorageDriver;
4
5
use Archivr\AbstractFactory;
6
use Archivr\Exception\ConfigurationException;
7
use Archivr\Exception\Exception;
8
use Archivr\TildeExpansionTrait;
9
use Archivr\VaultConfiguration;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\Filesystem;
12
13
class StorageDriverFactory extends AbstractFactory
14
{
15
    use TildeExpansionTrait;
16
17
    public function __construct()
18
    {
19
        $this->factoryMap['dummy'] = function ()
20
        {
21
            return new DummyStorageDriver();
22
        };
23
24
        $this->factoryMap['path'] = function(VaultConfiguration $vaultConfiguration)
25
        {
26
            $path = $vaultConfiguration->getSetting('path');
27
            $path = $this->expandTildePath($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
41 View Code Duplication
    public function create(string $name, ...$params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $driver = parent::create($name, ...$params);
44
45
        if (!($driver instanceof StorageDriverInterface))
46
        {
47
            throw new Exception(sprintf('Factory closure for driver "%s" does not return an instance of %s!', $name, StorageDriverInterface::class));
48
        }
49
50
        return $driver;
51
    }
52
}
53