Passed
Push — 0.8.x ( d7e9f1...7104c2 )
by Alexander
06:43 queued 03:17
created

FilesystemManager::resolve()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 21
rs 9.9332
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2023 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Components\Filesystem;
24
25
use InvalidArgumentException;
26
use Syscodes\Components\Contracts\Filesystem\Factory;
27
28
/**
29
 * Allows manage the distint adapters of file system.
30
 */
31
class FilesystemManager implements Factory
32
{
33
    /**
34
     * The application instance.
35
     * 
36
     * @var \Syscodes\Components\Contracts\Core\Application $app
37
     */
38
    protected $app;
39
    
40
    /**
41
     * The registered custom driver creators.
42
     * 
43
     * @var array $cumtomCreators
44
     */
45
    protected $customCreators = [];
46
    
47
    /**
48
     * The array of resolved filesystem drivers.
49
     * 
50
     * @var array $disks
51
     */
52
    protected $disks = [];
53
54
    /**
55
     * Constructor. Create a new FilesystemManager instance.
56
     * 
57
     * @param  \Syscodes\Components\Contracts\Core\Application  $app
58
     * 
59
     * @return void
60
     */
61
    public function __construct($app)
62
    {
63
        $this->app = $app;
64
    }
65
    
66
    /**
67
     * Get a filesystem instance.
68
     * 
69
     * @param  string|null  $name
70
     * 
71
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem 
72
     */
73
    public function disk($name = null)
74
    {
75
        $name = $name ?: $this->getDefaultDriver();
76
        
77
        return $this->disks[$name] = $this->get($name);
78
    }
79
    
80
    /**
81
     * Attempt to get the disk from the local cache.
82
     * 
83
     * @param  string  $name
84
     * 
85
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
86
     */
87
    protected function get($name)
88
    {
89
        return $this->disks[$name] ?? $this->resolve($name);
90
    }
91
    
92
    /**
93
     * Resolve the given disk.
94
     * 
95
     * @param  string  $name
96
     * @param  array|null  $config
97
     * 
98
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
99
     * 
100
     * @throws \InvalidArgumentException
101
     */
102
    protected function resolve($name, $config = null)
103
    {
104
        $config = $config ?? $this->getConfig($name);
105
        
106
        if (empty($config['driver'])) {
107
            throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver");
108
        }
109
        
110
        $name = $config['driver'];
111
112
        if (isset($this->customCreators[$name])) {
113
            return $this->callCustomCreator($config);
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type string; however, parameter $config of Syscodes\Components\File...er::callCustomCreator() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            return $this->callCustomCreator(/** @scrutinizer ignore-type */ $config);
Loading history...
114
        }
115
        
116
        $driverMethod = 'create'.ucfirst($name).'Driver';
117
        
118
        if ( ! method_exists($this, $driverMethod)) {
119
            throw new InvalidArgumentException("Driver [{$name}] is not supported");
120
        }
121
        
122
        return $this->{$driverMethod}($config);
123
    }
124
    
125
    /**
126
     * Call a custom driver creator.
127
     * 
128
     * @param  array  $config
129
     * 
130
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
131
     */
132
    protected function callCustomCreator(array $config)
133
    {
134
        return $this->customCreators[$config['driver']]($this->app, $config);
135
    }
136
    
137
    /**
138
     * Create an instance of the local driver.
139
     * 
140
     * @param  array  $config
141
     * 
142
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
143
     */
144
    public function createLocalDriver(array $config)
145
    {
146
        $adapter = new Filesystem;
147
148
        return new FilesystemAdapter($adapter, $config);
149
    }
150
        
151
    /**
152
     * Set the given disk instance.
153
     * 
154
     * @param  string  $name
155
     * @param  mixed  $disk
156
     * 
157
     * @return static
158
     */
159
    public function set($name, $disk): static
160
    {
161
        $this->disks[$name] = $disk;
162
        
163
        return $this;
164
    }
165
    
166
    /**
167
     * Get the filesystem connection configuration.
168
     * 
169
     * @param  string  $name
170
     * 
171
     * @return array
172
     */
173
    protected function getConfig($name): string
174
    {
175
        return $this->app['config']["filesystems.disks.{$name}"] ?: [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->app['confi...sks.'.$name] ?: array() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
176
    }
177
    
178
    /**
179
     * Get the default driver name.
180
     * 
181
     * @return string
182
     */
183
    public function getDefaultDriver(): string
184
    {
185
        return $this->app['config']['filesystems.default'];
186
    }
187
}