AbstractComponent   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 19
c 2
b 0
f 0
dl 0
loc 106
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilesystem() 0 3 1
A __call() 0 3 1
A setFilesystem() 0 3 1
A init() 0 4 1
A convertToDateTime() 0 7 2
A validateProperties() 0 5 3
A normalizePath() 0 5 2
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 yii\base\Component;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * Class AbstractComponent
16
 * 
17
 * @method bool fileExists(string $location)
18
 * @method bool directoryExists(string $location)
19
 * @method bool has(string $location) check fileExists or directoryExists
20
 * @method void write(string $location, string $contents, array $config = [])
21
 * @method void writeStream(string $location, $contents, array $config = [])
22
 * @method string read(string $location)
23
 * @method resource readStream(string $location)
24
 * @method void delete(string $location)
25
 * @method void deleteDirectory(string $location)
26
 * @method void createDirectory(string $location, array $config = [])
27
 * @method \League\Flysystem\DirectoryListing listContents(string $location, bool = \League\Flysystem\Filesystem::LIST_SHALLOW)
28
 * @method void move(string $source, string $destination, array $config = [])
29
 * @method void copy(string $source, string $destination, array $config = [])
30
 * @method int lastModified(string $path)
31
 * @method int fileSize(string $path)
32
 * @method string mimeType(string $path)
33
 * @method void setVisibility(string $path, string $visibility)
34
 * @method string visibility(string $path)
35
 * @method string publicUrl(string $path, array $config = [])
36
 * @method string temporaryUrl(string $path, \DateTimeInterface $expiresAt, array $config = [])
37
 * @method string checksum(string $path, array $config = [])
38
 * 
39
 * @property string $action
40
 * 
41
 * @link      https://sugengsulistiyawan.my.id/
42
 * @author    Sugeng Sulistiyawan <[email protected]>
43
 * @copyright Copyright (c) 2023
44
 */
45
abstract class AbstractComponent extends Component
46
{
47
    /**
48
     * @var array
49
     */
50
    public $config = [];
51
52
    /** 
53
     * @var string 
54
     */
55
    public $prefix;
56
57
    /** 
58
     * @var string 
59
     */
60
    public $directorySeparator = DIRECTORY_SEPARATOR;
61
62
    /**
63
     * @var bool
64
     */
65
    public $debug = false;
66
67
    private $_filesystem;
68
69
    /**
70
     * @param string $method
71
     * @param array $parameters
72
     * @return mixed
73
     */
74
    public function __call($method, $parameters)
75
    {
76
        return call_user_func_array([$this->getFilesystem(), $method], $parameters);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function init()
83
    {
84
        $adapter = $this->initAdapter();
85
        $this->setFilesystem(new Filesystem($adapter, $this->config));
86
    }
87
88
    /**
89
     * @return Filesystem
90
     */
91
    public function getFilesystem()
92
    {
93
        return $this->_filesystem;
94
    }
95
96
    /**
97
     * @param Filesystem $value
98
     * @return void
99
     */
100
    public function setFilesystem(Filesystem $value)
101
    {
102
        $this->_filesystem = $value;
103
    }
104
105
    /**
106
     * Normalizes a file/directory path.
107
     * 
108
     * @param string $path
109
     * @param PathNormalizer|null $pathNormalizer
110
     * @return string
111
     */
112
    public function normalizePath(string $path, PathNormalizer $pathNormalizer = null)
113
    {
114
        $pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
115
116
        return $pathNormalizer->normalizePath($path);
117
    }
118
119
    /**
120
     * Convert Time To DateTimeImmutable
121
     *
122
     * @param int|string|DateTimeInterface $dateValue
123
     * @return DateTimeImmutable
124
     */
125
    public function convertToDateTime($dateValue)
126
    {
127
        if ($dateValue instanceof DateTimeInterface) {
128
            return DateTimeImmutable::createFromInterface($dateValue);
129
        }
130
131
        return new DateTimeImmutable($dateValue);
132
    }
133
134
    /**
135
     * @return FilesystemAdapter
136
     */
137
    abstract protected function initAdapter();
138
139
    /**
140
     * Validate property
141
     * 
142
     * @param array $properties
143
     * @return void
144
     * @throws InvalidConfigException
145
     */
146
    public function validateProperties(array $properties = [])
147
    {
148
        foreach ($properties as $property) {
149
            if (empty($this->$property)) {
150
                throw new InvalidConfigException("The \"$property\" property must be set.");
151
            }
152
        }
153
    }
154
}
155