Completed
Push — master ( 0339b7...e81256 )
by Bas
7s
created

FilesystemFactory::createService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 9
Bugs 0 Features 2
Metric Value
c 9
b 0
f 2
dl 10
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace BsbFlysystem\Filesystem\Factory;
4
5
use BsbFlysystem\Cache\ZendStorageCache;
6
use BsbFlysystem\Exception\RequirementsException;
7
use BsbFlysystem\Exception\UnexpectedValueException;
8
use Interop\Container\ContainerInterface;
9
use League\Flysystem\Cached\CachedAdapter;
10
use League\Flysystem\Cached\CacheInterface;
11
use League\Flysystem\EventableFilesystem\EventableFilesystem;
12
use League\Flysystem\Filesystem;
13
use Zend\Cache\Storage\StorageInterface;
14
use Zend\ServiceManager\FactoryInterface;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
17
class FilesystemFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $options;
23
24
    /**
25
     * FilesystemFactory constructor.
26
     *
27
     * @param array $options
28
     */
29 9
    public function __construct(array $options = [])
30
    {
31 9
        $this->setCreationOptions($options);
32 9
    }
33
34
    /**
35
     * Set creation options
36
     *
37
     * @param  array $options
38
     * @return void
39
     */
40 9
    public function setCreationOptions(array $options)
41
    {
42 9
        $this->options = $options;
43
44 9
        if (!isset($this->options['adapter_options'])) {
45 9
            $this->options['adapter_options'] = [];
46 9
        }
47 9
    }
48
49
    /**
50
     * @param ServiceLocatorInterface $serviceLocator
51
     * @return mixed
52
     */
53 View Code Duplication
    public function createService(ServiceLocatorInterface $serviceLocator)
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...
54
    {
55
        if (method_exists($serviceLocator, 'getServiceLocator')) {
56
            $serviceLocator = $serviceLocator->getServiceLocator();
57
        }
58
59
        $requestedName = func_get_arg(2);
60
        
61
        return $this($serviceLocator, $requestedName);
62
    }
63
64
    /**
65
     * @param ContainerInterface $container
66
     * @param string             $requestedName
67
     * @param array|null         $options
68
     * @return EventableFilesystem|Filesystem
69
     */
70 9
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
71
    {
72 9
        $config         = $container->get('config');
73 9
        $fsConfig       = $config['bsb_flysystem']['filesystems'][$requestedName];
74 9
        if (!isset($fsConfig['adapter'])) {
75 1
            throw new UnexpectedValueException(sprintf(
76 1
                "Missing 'adapter' key for the filesystem '%s' configuration",
77
                $requestedName
78 1
            ));
79
        }
80
81 8
        if (null !== $options) {
82 1
            $this->setCreationOptions($options);
83 1
        }
84
85
        $adapter = $container
86 8
            ->get('BsbFlysystemAdapterManager')
87 8
            ->get($fsConfig['adapter'], $this->options['adapter_options']);
88
89 8
        $options = isset($fsConfig['options']) && is_array($fsConfig['options']) ? $fsConfig['options'] : [];
90
91 8
        if (isset($fsConfig['cache']) && is_string($fsConfig['cache'])) {
92 3
            if (!class_exists(\League\Flysystem\Cached\CachedAdapter::class)) {
93
                throw new RequirementsException(['league/flysystem-cached-adapter'], 'CachedAdapter');
94
            }
95
96 3
            $cacheAdapter = $container->get($fsConfig['cache']);
97
98
            // wrap if StorageInterface, use filesystem name a key
99 3
            if ($cacheAdapter instanceof StorageInterface) {
100 2
                $cacheAdapter = new ZendStorageCache($cacheAdapter, $requestedName);
101 2
            }
102
103
            // ignore if not CacheInterface
104 3
            if ($cacheAdapter instanceof CacheInterface) {
105 3
                $adapter = new CachedAdapter($adapter, $cacheAdapter);
106 3
            }
107 3
        }
108
109 8
        if (isset($fsConfig['eventable']) && filter_var($fsConfig['eventable'], FILTER_VALIDATE_BOOLEAN)) {
110 1
            if (!class_exists(\League\Flysystem\EventableFilesystem\EventableFilesystem::class)) {
111
                throw new RequirementsException(['league/flysystem-eventable-filesystem'], 'EventableFilesystem');
112
            }
113
114 1
            $filesystem = new EventableFilesystem($adapter, $options);
115 1
        } else {
116 7
            $filesystem = new Filesystem($adapter, $options);
117
        }
118
119 8
        if (isset($fsConfig['plugins']) && is_array($fsConfig['plugins'])) {
120 1
            foreach ($fsConfig['plugins'] as $plugin) {
121 1
                $plugin = new $plugin();
122 1
                $filesystem->addPlugin($plugin);
123 1
            }
124 1
        }
125
126 8
        return $filesystem;
127
    }
128
}
129