Test Failed
Pull Request — master (#24)
by Filippo
12:43
created

FileSystem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 19
dl 0
loc 78
rs 10
c 2
b 1
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isLocal() 0 8 1
A instance() 0 8 1
A filesystem() 0 3 1
A makeDirectory() 0 7 2
1
<?php
2
3
namespace Jobtech\LaravelChunky\Support;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Filesystem\Factory;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use Illuminate\Support\Traits\ForwardsCalls;
10
use League\Flysystem\Adapter\Local;
11
12
abstract class FileSystem
13
{
14
    use ForwardsCalls;
15
16
    /** @var \Illuminate\Contracts\Filesystem\Factory */
17
    private Factory $filesystem;
18
19
    /** @var string|null */
20
    protected ?string $disk = null;
21
22
    /** @var string|null */
23
    protected ?string $folder = null;
24
25
    public function __construct(Factory $filesystem)
26
    {
27
        $this->filesystem = $filesystem;
28
    }
29
30
    /**
31
     * @return \Illuminate\Contracts\Filesystem\Factory
32
     */
33
    public function filesystem(): Factory
34
    {
35
        return $this->filesystem;
36
    }
37
38
    /**
39
     * Check if filesystem is using local adapter.
40
     *
41
     * @return bool
42
     */
43
    public function isLocal(): bool
44
    {
45
        $driver = $this->filesystem()
46
            ->disk($this->disk)
47
            ->getDriver()
48
            ->getAdapter();
49
50
        return $driver instanceof Local;
51
    }
52
53
    /**
54
     * @param string $folder
55
     * @return bool
56
     */
57
    public function makeDirectory(string $folder): bool
58
    {
59
        if (! Str::startsWith($folder, $this->folder)) {
60
            $folder = $this->folder.$folder;
61
        }
62
63
        return $this->filesystem->disk($this->disk)->makeDirectory($folder);
64
    }
65
66
    /**
67
     * Disk getter and setter.
68
     *
69
     * @param string|null $disk
70
     * @return string|null
71
     */
72
    abstract public function disk($disk = null): ?string;
73
74
    /**
75
     * Folder getter and setter.
76
     *
77
     * @param string|null $folder
78
     * @return string|null
79
     */
80
    abstract public function folder($folder = null): ?string;
81
82
    public static function instance(array $config): FileSystem
83
    {
84
        $filesystem = Container::getInstance()->make(get_called_class());
85
86
        $filesystem->disk(Arr::get($config, 'disk'));
87
        $filesystem->folder(Arr::get($config, 'folder'));
88
89
        return $filesystem;
90
    }
91
}
92