Passed
Push — main ( 2456f4...ce01ad )
by Sugeng
04:03
created

AbstractComponent::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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