Completed
Pull Request — master (#1)
by Westin
06:59
created

FlySystemFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A __callStatic() 0 14 3
A getFileSystemName() 0 4 1
A setFileSystemName() 0 4 1
A getFlySystemManager() 0 11 2
A setFlySystemManager() 0 4 1
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
0 ignored issues
show
Bug introduced by
The class Psr\Container\ContainerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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