Completed
Push — master ( 3691e2...a5d74c )
by Patrick
02:26
created

Component::getDiskResolvers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Mosaic\Filesystem\Adapters\Flysystem;
4
5
use Aws\S3\S3Client;
6
use BadMethodCallException;
7
use Dropbox\Client;
8
use Interop\Container\Definition\DefinitionProviderInterface;
9
use League\Flysystem\Adapter\Ftp;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\AwsS3v3\AwsS3Adapter;
12
use League\Flysystem\Dropbox\DropboxAdapter;
13
use Mosaic\Common\Conventions\FolderStructureConvention;
14
use Mosaic\Filesystem\Providers\FlystemProvider;
15
16
class Component implements \Mosaic\Common\Components\Component
17
{
18
    /**
19
     * @var FolderStructureConvention
20
     */
21
    private $folderStructure;
22
23
    /**
24
     * @var DiskResolverCollection
25
     */
26
    private $diskResolvers;
27
28
    /**
29
     * @var array
30
     */
31
    private static $custom = [];
32
33
    /**
34
     * @param FolderStructureConvention $folderStructure
35
     */
36 10
    public function __construct(FolderStructureConvention $folderStructure)
37
    {
38 10
        $this->diskResolvers   = new DiskResolverCollection;
39 10
        $this->folderStructure = $folderStructure;
40
41 10
        $this->local('local', $this->folderStructure->storagePath());
42 10
    }
43
44
    /**
45
     * @return DefinitionProviderInterface[]
46
     */
47 1
    public function getProviders() : array
48
    {
49
        return [
50 1
            new FlystemProvider($this->getDiskResolvers())
51
        ];
52
    }
53
54
    /**
55
     * @param  string   $name
56
     * @param  callable $resolver
57
     * @param  array    $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. 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...
58
     * @return $this
59
     */
60 10
    public function disk(string $name, callable $resolver)
61
    {
62 10
        $this->diskResolvers->add($name, $resolver);
63
64 10
        return $this;
65
    }
66
67
    /**
68
     * @param  string $name
69
     * @param  string $path
70
     * @return $this
71
     */
72 10
    public function local(string $name, string $path)
73
    {
74
        $this->disk($name, function () use ($path) {
75 2
            return new Local($path);
76 10
        });
77
78 10
        return $this;
79
    }
80
81
    /**
82
     * @param  array $settings
83
     * @return $this
84
     */
85 1
    public function ftp(array $settings)
86
    {
87
        $this->disk('ftp', function () use ($settings) {
88
            return new Ftp($settings);
89 1
        });
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * @param  array $settings
96
     * @return $this
97
     */
98 1
    public function aws(string $bucket, array $settings)
99
    {
100
        $this->disk('aws', function () use ($bucket, $settings) {
101
102
            $client = S3Client::factory($settings);
103
104
            return new AwsS3Adapter($client, $bucket);
105 1
        });
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @param  string $accessToken
112
     * @param  string $appSecret
113
     * @return $this
114
     */
115
    public function dropbox(string $accessToken, string $appSecret)
116
    {
117 1
        $this->disk('dropbox', function () use ($accessToken, $appSecret) {
118
            return new DropboxAdapter(
119
                new Client($accessToken, $appSecret)
120
            );
121 1
        });
122
123 1
        return $this;
124
    }
125
126
    /**
127
     * @param string   $name
128
     * @param callable $callback
129
     */
130 1
    public static function extend(string $name, callable $callback)
131
    {
132 1
        static::$custom[$name] = $callback;
0 ignored issues
show
Bug introduced by
Since $custom is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $custom to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
133 1
    }
134
135
    /**
136
     * @return DiskResolverCollection
137
     */
138 8
    public function getDiskResolvers()
139
    {
140 8
        return $this->diskResolvers;
141
    }
142
143
    /**
144
     * @param  string $name
145
     * @param  array  $params
146
     * @return mixed
147
     */
148 2
    public function __call(string $name, array $params = [])
149
    {
150 2
        if (isset(static::$custom[$name])) {
0 ignored issues
show
Bug introduced by
Since $custom is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $custom to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
151 1
            return $this->disk($name, static::$custom[$name]);
0 ignored issues
show
Bug introduced by
Since $custom is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $custom to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
152
        }
153
154 1
        throw new BadMethodCallException;
155
    }
156
}
157