Passed
Push — main ( e5d18b...282b2b )
by Sugeng
03:08
created

AbstractComponent::getFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\FilesystemAdapter;
9
use League\Flysystem\PathNormalizer;
10
use League\Flysystem\WhitespacePathNormalizer;
11
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
12
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
13
use yii\base\Component;
14
15
/**
16
 * Class AbstractComponent
17
 * 
18
 * @method bool fileExists(string $location)
19
 * @method bool directoryExists(string $location)
20
 * @method bool has(string $location) check fileExists or directoryExists
21
 * @method void write(string $location, string $contents, array $config = [])
22
 * @method void writeStream(string $location, $contents, array $config = [])
23
 * @method string read(string $location)
24
 * @method resource readStream(string $location)
25
 * @method void delete(string $location)
26
 * @method void deleteDirectory(string $location)
27
 * @method void createDirectory(string $location, array $config = [])
28
 * @method \League\Flysystem\DirectoryListing listContents(string $location, bool = \League\Flysystem\Filesystem::LIST_SHALLOW)
29
 * @method void move(string $source, string $destination, array $config = [])
30
 * @method void copy(string $source, string $destination, array $config = [])
31
 * @method int lastModified(string $path)
32
 * @method int fileSize(string $path)
33
 * @method string mimeType(string $path)
34
 * @method void setVisibility(string $path, string $visibility)
35
 * @method string visibility(string $path)
36
 * @method string publicUrl(string $path, array $config = [])
37
 * @method string temporaryUrl(string $path, \DateTimeInterface $expiresAt, array $config = [])
38
 * @method string checksum(string $path, array $config = [])
39
 * 
40
 * @link      https://sugengsulistiyawan.my.id/
41
 * @author    Sugeng Sulistiyawan <[email protected]>
42
 * @copyright Copyright (c) 2023
43
 */
44
abstract class AbstractComponent extends Component
45
{
46
    /**
47
     * @var array
48
     */
49
    public $config = [];
50
51
    /** 
52
     * @var string 
53
     */
54
    public $prefix;
55
56
    /** 
57
     * @var string 
58
     */
59
    public $directorySeparator = DIRECTORY_SEPARATOR;
60
61
    /**
62
     * @var bool
63
     */
64
    public $debug = false;
65
66
    private $_filesystem;
67
68
    /**
69
     * @param string $method
70
     * @param array $parameters
71
     * @return mixed
72
     */
73
    public function __call($method, $parameters)
74
    {
75
        return call_user_func_array([$this->getFilesystem(), $method], $parameters);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function init()
82
    {
83
        $adapter = $this->initAdapter();
84
        $this->setFilesystem(new Filesystem($adapter, $this->config));
85
    }
86
87
    /**
88
     * @return Filesystem
89
     */
90
    public function getFilesystem()
91
    {
92
        return $this->_filesystem;
93
    }
94
95
    /**
96
     * @param Filesystem $value
97
     * @return void
98
     */
99
    public function setFilesystem(Filesystem $value)
100
    {
101
        $this->_filesystem = $value;
102
    }
103
104
    /**
105
     * Normalizes a file/directory path.
106
     * 
107
     * @param string $path
108
     * @param PathNormalizer|null $pathNormalizer
109
     * @return string
110
     */
111
    public function normalizePath(string $path, PathNormalizer $pathNormalizer = null)
112
    {
113
        $pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
114
115
        return $pathNormalizer->normalizePath($path);
116
    }
117
118
    /**
119
     * Convert Time To DateTimeImmutable
120
     *
121
     * @param int|string|DateTimeInterface $dateValue
122
     * @return DateTimeImmutable
123
     */
124
    public function convertToDateTime($dateValue)
125
    {
126
        if ($dateValue instanceof DateTimeInterface) {
127
            return DateTimeImmutable::createFromInterface($dateValue);
128
        }
129
130
        return new DateTimeImmutable($dateValue);
131
    }
132
133
    /**
134
     * @return FilesystemAdapter
135
     */
136
    abstract protected function initAdapter();
137
}