Passed
Push — master ( e4bd6c...14411d )
by Alexander
11:26
created

FilesystemManager::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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
27
/**
28
 * Allows manage the distint adapters of file system.
29
 */
30
class FilesystemManager
31
{
32
    /**
33
     * The application instance.
34
     * 
35
     * @var \Syscodes\Components\Contracts\Core\Application $app
36
     */
37
    protected $app;
38
    
39
    /**
40
     * The array of resolved filesystem drivers.
41
     * 
42
     * @var array $disks
43
     */
44
    protected $disks = [];
45
46
    /**
47
     * Constructor. Create a new FilesystemManager instance.
48
     * 
49
     * @param  \Syscodes\Components\Contracts\Core\Application  $app
50
     * 
51
     * @return void
52
     */
53
    public function __construct($app)
54
    {
55
        $this->app = $app;
56
    }
57
58
    /**
59
     * Get a filesystem instance.
60
     *
61
     * @param  string|null  $name
62
     * @return \Illuminate\Contracts\Filesystem\Filesystem
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
     */
64
    public function disk($name = null)
65
    {
66
        $name = $name ?: $this->getDefaultDriver();
67
68
        return $this->disks[$name] = $this->get($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->disks[$name] = $this->get($name) returns the type Syscodes\Components\Cont...s\Filesystem\Filesystem which is incompatible with the documented return type Illuminate\Contracts\Filesystem\Filesystem.
Loading history...
69
    }
70
    
71
    /**
72
     * Attempt to get the disk from the local cache.
73
     * 
74
     * @param  string  $name
75
     * 
76
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
77
     */
78
    protected function get($name)
79
    {
80
        return $this->disks[$name] ?? $this->resolve($name);
81
    }
82
    
83
    /**
84
     * Resolve the given disk.
85
     * 
86
     * @param  string  $name
87
     * @param  array|null  $config
88
     * 
89
     * @return \Syscodes\Components\Contracts\Filesystem\Filesystem
90
     * 
91
     * @throws \InvalidArgumentException
92
     */
93
    protected function resolve($name, $config = null)
94
    {
95
        $config ??= $this->getConfig($name);
96
        
97
        if (empty($config['driver'])) {
98
            throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver");
99
        }
100
        
101
        $name = $config['driver'];
102
        
103
        $driverMethod = 'create'.ucfirst($name).'Driver';
104
        
105
        if ( ! method_exists($this, $driverMethod)) {
106
            throw new InvalidArgumentException("Driver [{$name}] is not supported");
107
        }
108
        
109
        return $this->{$driverMethod}($config);
110
    }
111
        
112
    /**
113
     * Set the given disk instance.
114
     * 
115
     * @param  string  $name
116
     * @param  mixed  $disk
117
     * 
118
     * @return static
119
     */
120
    public function set($name, $disk): static
121
    {
122
        $this->disks[$name] = $disk;
123
        
124
        return $this;
125
    }
126
    
127
    /**
128
     * Get the filesystem connection configuration.
129
     * 
130
     * @param  string  $name
131
     * 
132
     * @return array
133
     */
134
    protected function getConfig($name): string
135
    {
136
        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...
137
    }
138
    
139
    /**
140
     * Get the default driver name.
141
     * 
142
     * @return string
143
     */
144
    public function getDefaultDriver(): string
145
    {
146
        return $this->app['config']['filesystems.default'];
147
    }
148
}