Completed
Push — master ( ccbe0b...982bf9 )
by Oscar
02:19
created

Container   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __callStatic() 0 24 5
A __get() 0 8 2
A __set() 0 4 1
1
<?php
2
3
namespace FlyCrud;
4
5
use League\Flysystem\FilesystemInterface;
6
7
class Container
8
{
9
    protected $repositories = [];
10
11
    /**
12
     * Create a container with some repositories.
13
     * 
14
     * @param FilesystemInterface $filesystem
0 ignored issues
show
Bug introduced by
There is no parameter named $filesystem. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
15
     * @param string              $class
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
16
     */
17
    public static function __callStatic($name, $args)
18
    {
19
        $filesystem = array_shift($args);
20
21
        if (!($filesystem instanceof FilesystemInterface)) {
22
            throw new \InvalidArgumentException(sprintf('Invalid argument. Expected a %s but got %s', FilesystemInterface::class, gettype($filesystem)));
23
        }
24
25
        $class = __NAMESPACE__.'\\'.ucfirst($name);
26
27
        if (!class_exists($class)) {
28
            throw new BadMethodCallException(sprintf('The repository class "%s" does not exists', $class));
29
        }
30
31
        $container = new static();
32
33
        foreach ($filesystem->listContents() as $info) {
34
            if ($info['type'] === 'dir') {
35
                $container->{$info['filename']} = new $class($filesystem, $info['filename']);
36
            }
37
        }
38
39
        return $container;
40
    }
41
42
    /**
43
     * Returns a repository.
44
     * 
45
     * @param string $name
46
     * 
47
     * @return Repository
48
     */
49
    public function __get($name)
50
    {
51
        if (!isset($this->repositories[$name])) {
52
            throw new \InvalidArgumentException(sprintf('The repository %s does not exist', $name));
53
        }
54
55
        return $this->repositories[$name];
56
    }
57
58
    /**
59
     * Adds a new repository to the container.
60
     *
61
     * @param string     $name
62
     * @param Repository $repository
63
     *
64
     * @return self
65
     */
66
    public function __set($name, Repository $repository)
67
    {
68
        $this->repositories[$name] = $repository;
69
    }
70
}
71