Completed
Push — master ( ca2abf...933c4d )
by Westin
05:20
created

FlySystemManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem;
5
6
use League\Flysystem\Cached\CachedAdapter;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\FilesystemInterface;
9
use League\Flysystem\MountManager;
10
use Psr\Container\ContainerInterface;
11
use WShafer\PSR11FlySystem\Config\FileSystemConfig;
12
use WShafer\PSR11FlySystem\Config\MainConfig;
13
use WShafer\PSR11FlySystem\Exception\UnknownFileSystemException;
14
use WShafer\PSR11FlySystem\Exception\UnknownPluginException;
15
16
class FlySystemManager implements ContainerInterface
17
{
18
    /** @var MainConfig  */
19
    protected $config;
20
21
    /** @var ContainerInterface */
22
    protected $adaptorManager;
23
24
    /** @var ContainerInterface */
25
    protected $cacheManager;
26
27
    /** @var Filesystem[]|MountManager[] */
28
    protected $systems = [];
29
30
    /** @var ContainerInterface */
31
    protected $container;
32
33
    /**
34
     * Manager constructor.
35
     * @param MainConfig         $config
36
     * @param ContainerInterface $adaptorManager
37
     * @param ContainerInterface $cacheManager
38
     * @param ContainerInterface $container
39
     */
40 10
    public function __construct(
41
        MainConfig $config,
42
        ContainerInterface $adaptorManager,
43
        ContainerInterface $cacheManager,
44
        ContainerInterface $container
45
    ) {
46 10
        $this->config = $config;
47 10
        $this->adaptorManager = $adaptorManager;
48 10
        $this->cacheManager = $cacheManager;
49 10
        $this->container = $container;
50 10
    }
51
52
    /**
53
     * @param string $id
54
     *
55
     * @return Filesystem|MountManager
56
     */
57 6
    public function get($id)
58
    {
59 6
        if (key_exists($id, $this->systems)) {
60 1
            return $this->systems[$id];
61
        }
62
63 6
        $fileSystemConfig = $this->config->getFileSystemConfig($id);
64
65 6
        if (!$fileSystemConfig) {
66 1
            throw new UnknownFileSystemException(
67 1
                'Unable to locate file system '.$id.'.  Please check your configuration.'
68
            );
69
        }
70
71 5
        if (!$fileSystemConfig->isManager()) {
72 4
            return $this->getFileSystem($id, $fileSystemConfig);
73
        }
74
75 1
        $fileSystems = [];
76
77 1
        foreach ($fileSystemConfig->getFileSystems() as $name => $managerSystemConfig) {
78 1
            $fileSystems[$name] = $this->getFileSystem($name, $managerSystemConfig);
79
        }
80
81 1
        $manager = new MountManager($fileSystems);
82
83 1
        if ($fileSystemConfig->getPlugins()) {
84 1
            $this->setPlugins($manager, $fileSystemConfig->getPlugins());
85
        }
86
87 1
        $this->systems[$id] = $manager;
88 1
        return $this->systems[$id];
89
    }
90
91
    /**
92
     * @param $id
93
     * @param FileSystemConfig $fileSystemConfig
94
     * @return Filesystem
95
     */
96 5
    protected function getFileSystem($id, FileSystemConfig $fileSystemConfig)
97
    {
98 5
        $adaptor = $this->adaptorManager->get($fileSystemConfig->getAdaptor());
99 5
        $cache = $this->cacheManager->get($fileSystemConfig->getCache());
100 5
        $cachedAdaptor = new CachedAdapter($adaptor, $cache);
101 5
        $fileSystem = new Filesystem($cachedAdaptor);
102
103 5
        if ($fileSystemConfig->getPlugins()) {
104 3
            $this->setPlugins($fileSystem, $fileSystemConfig->getPlugins());
105
        }
106
107 4
        $this->systems[$id] = $fileSystem;
108 4
        return $fileSystem;
109
    }
110
111
    /**
112
     * @param FilesystemInterface|MountManager $filesystem
113
     * @param array $plugins
114
     */
115 4
    protected function setPlugins($filesystem, array $plugins = [])
116
    {
117 4
        foreach ($plugins as $plugin) {
118 4
            $this->addPlugin($filesystem, $plugin);
119
        }
120 3
    }
121
122
    /**
123
     * @param FilesystemInterface|MountManager $filesystem
124
     * @param string $pluginName
125
     */
126 4
    protected function addPlugin($filesystem, string $pluginName)
127
    {
128 4
        if (!$this->container->has($pluginName)) {
129 1
            throw new UnknownPluginException(
130 1
                'Unable to locate plugin service '.$pluginName.' in the container.  Please check your config.'
131
            );
132
        }
133
134 3
        $filesystem->addPlugin($this->container->get($pluginName));
135 3
    }
136
137 2
    public function has($id)
138
    {
139 2
        return $this->config->hasFileSystemConfig($id);
140
    }
141
}
142