Completed
Pull Request — master (#1)
by Westin
16:17 queued 06:16
created

FlySystemFactory::__callStatic()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem;
5
6
use League\Flysystem\Filesystem;
7
use League\Flysystem\MountManager;
8
use Psr\Container\ContainerInterface;
9
use WShafer\PSR11FlySystem\Exception\InvalidContainerException;
10
11
class FlySystemFactory
12
{
13
    /** @var string */
14
    protected $fileSystemName = 'default';
15
16
    /** @var FlySystemManager */
17
    protected static $flySystemManager;
18
19
    /**
20
     * @param ContainerInterface $container
21
     *
22
     * @return Filesystem|MountManager
23
     */
24
    public function __invoke(ContainerInterface $container)
25
    {
26
        $manager = static::getFlySystemManager($container);
27
        $fileSystemName = $this->getFileSystemName();
28
        return $manager->get($fileSystemName);
29
    }
30
31
    /**
32
     * Magic method for constructing FileSystems by service name
33
     *
34
     * @param $name
35
     * @param $arguments
36
     *
37
     * @return Filesystem|MountManager
38
     */
39
    public static function __callStatic($name, $arguments)
40
    {
41
        if (empty($arguments[0])
42
            || !$arguments[0] instanceof ContainerInterface
43
        ) {
44
            throw new InvalidContainerException(
45
                'Argument 0 must be an instance of a PSR-11 container'
46
            );
47
        }
48
49
        $factory = new static();
50
        $factory->setFileSystemName($name);
51
        return $factory($arguments[0]);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getFileSystemName(): string
58
    {
59
        return $this->fileSystemName;
60
    }
61
62
    /**
63
     * @param string $fileSystemName
64
     */
65
    public function setFileSystemName(string $fileSystemName)
66
    {
67
        $this->fileSystemName = $fileSystemName;
68
    }
69
70
    public static function getFlySystemManager(ContainerInterface $container) : FlySystemManager
71
    {
72
        // @codeCoverageIgnoreStart
73
        if (!static::$flySystemManager) {
74
            $factory = new FlySystemManagerFactory();
75
            static::setFlySystemManager($factory($container));
76
        }
77
        // @codeCoverageIgnoreEnd
78
79
        return static::$flySystemManager;
80
    }
81
82
    public static function setFlySystemManager(FlySystemManager $flySystemManager)
83
    {
84
        static::$flySystemManager = $flySystemManager;
85
    }
86
}
87