Passed
Push — main ( e627b1...57d795 )
by Sugeng
03:50
created

AbstractComponent::validateProperties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 3
nop 1
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
 * @link      https://sugengsulistiyawan.my.id/
40
 * @author    Sugeng Sulistiyawan <[email protected]>
41
 * @copyright Copyright (c) 2023
42
 */
43
abstract class AbstractComponent extends Component
44
{
45
    /**
46
     * @var array
47
     */
48
    public $config = [];
49
50
    /** 
51
     * @var string 
52
     */
53
    public $prefix;
54
55
    /** 
56
     * @var string 
57
     */
58
    public $directorySeparator = DIRECTORY_SEPARATOR;
59
60
    /**
61
     * @var bool
62
     */
63
    public $debug = false;
64
65
    private $_filesystem;
66
67
    /**
68
     * @param string $method
69
     * @param array $parameters
70
     * @return mixed
71
     */
72
    public function __call($method, $parameters)
73
    {
74
        return call_user_func_array([$this->getFilesystem(), $method], $parameters);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function init()
81
    {
82
        $adapter = $this->initAdapter();
83
        $this->setFilesystem(new Filesystem($adapter, $this->config));
84
    }
85
86
    /**
87
     * @return Filesystem
88
     */
89
    public function getFilesystem()
90
    {
91
        return $this->_filesystem;
92
    }
93
94
    /**
95
     * @param Filesystem $value
96
     * @return void
97
     */
98
    public function setFilesystem(Filesystem $value)
99
    {
100
        $this->_filesystem = $value;
101
    }
102
103
    /**
104
     * Normalizes a file/directory path.
105
     * 
106
     * @param string $path
107
     * @param PathNormalizer|null $pathNormalizer
108
     * @return string
109
     */
110
    public function normalizePath(string $path, PathNormalizer $pathNormalizer = null)
111
    {
112
        $pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
113
114
        return $pathNormalizer->normalizePath($path);
115
    }
116
117
    /**
118
     * Convert Time To DateTimeImmutable
119
     *
120
     * @param int|string|DateTimeInterface $dateValue
121
     * @return DateTimeImmutable
122
     */
123
    public function convertToDateTime($dateValue)
124
    {
125
        if ($dateValue instanceof DateTimeInterface) {
126
            return DateTimeImmutable::createFromInterface($dateValue);
127
        }
128
129
        return new DateTimeImmutable($dateValue);
130
    }
131
132
    /**
133
     * @return FilesystemAdapter
134
     */
135
    abstract protected function initAdapter();
136
137
    /**
138
     * Validate property
139
     * 
140
     * @param array $properties
141
     * @return void
142
     * @throws InvalidConfigException
143
     */
144
    public function validateProperties(array $properties = [])
145
    {
146
        foreach ($properties as $property) {
147
            if (empty($this->$property)) {
148
                throw new InvalidConfigException("The \"$property\" property must be set.");
149
            }
150
        }
151
    }
152
}
153