Passed
Push — main ( 2f5f7f...11d41a )
by Sugeng
03:30
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\Config;
6
use League\Flysystem\Filesystem;
7
use League\Flysystem\FilesystemAdapter;
8
use yii\base\Component;
9
use yii\helpers\FileHelper;
10
use yii\helpers\StringHelper;
11
12
/**
13
 * Class AbstractComponent
14
 *
15
 * @package diecoding\flysystem
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 $deep = false)
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 Config|array|string|null
47
     */
48
    public $config;
49
50
    /** 
51
     * @var string|null 
52
     */
53
    public $basePath;
54
55
    /**
56
     * @var string
57
     */
58
    public $cipherAlgo = 'aes-128-cbc';
59
60
    /**
61
     * @var string
62
     */
63
    public $secret;
64
65
    /**
66
     * @var Filesystem
67
     */
68
    protected $filesystem;
69
70
    /**
71
     * @param string $method
72
     * @param array $parameters
73
     * @return mixed
74
     */
75
    public function __call($method, $parameters)
76
    {
77
        return call_user_func_array([$this->filesystem, $method], $parameters);
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function init()
84
    {
85
        $adapter          = $this->initAdapter();
86
        $this->config     = $this->config ?? [];
87
        $this->filesystem = new Filesystem($adapter, $this->config);
88
    }
89
90
    /**
91
     * @return Filesystem
92
     */
93
    public function getFilesystem()
94
    {
95
        return $this->filesystem;
96
    }
97
98
    /**
99
     * Normalizes a file/directory path.
100
     *
101
     * The normalization does the following work:
102
     *
103
     * - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "a/b/c")
104
     * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "a/b/c")
105
     * - Remove first directory separators (e.g. "/a/b/c" becomes "a/b/c")
106
     * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "a/b/c")
107
     * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "a/c")
108
     *
109
     * Note: For registered stream wrappers, the consecutive slashes rule
110
     * and ".."/"." translations are skipped.
111
     * 
112
     * @param string $path
113
     * @return string
114
     */
115
    public function normalizePath(string $path)
116
    {
117
        $basePath = $this->basePath ? "{$this->basePath}/" : '';
118
        $path     = FileHelper::normalizePath($basePath . $path, "/");
119
120
        return $path[0] === "/" ? substr($path, 1) : $path;
121
    }
122
123
    /**
124
     * Encrypts a string.
125
     * 
126
     * @param string $string the string to encrypt
127
     * @return string the encrypted string
128
     */
129
    public function encrypt($string)
130
    {
131
        $encryptedString = openssl_encrypt($string, $this->cipherAlgo, $this->secret);
132
        $encryptedString = StringHelper::base64UrlEncode(base64_encode($encryptedString));
133
134
        return $encryptedString;
135
    }
136
137
    /**
138
     * Decrypts a string. 
139
     * False is returned in case it was not possible to decrypt it.
140
     * 
141
     * @param string $string the string to decrypt
142
     * @return string the decrypted string
143
     */
144
    public function decrypt($string)
145
    {
146
        $decodedString = base64_decode(StringHelper::base64UrlDecode($string));
147
        $decodedString = openssl_decrypt($decodedString, $this->cipherAlgo, $this->secret);
148
149
        return $decodedString;
150
    }
151
152
    /**
153
     * Convert To Timestamp
154
     *
155
     * @param int|string|\DateTimeInterface $dateValue
156
     * @param int|null $relativeTimeBase
157
     * @return int|false
158
     */
159
    public function convertToTimestamp($dateValue, $relativeTimeBase = null)
160
    {
161
        if ($dateValue instanceof \DateTimeInterface) {
162
            $timestamp = $dateValue->getTimestamp();
163
        } elseif (!is_numeric($dateValue)) {
164
            $timestamp = strtotime(
165
                $dateValue,
166
                $relativeTimeBase === null ? time() : $relativeTimeBase
167
            );
168
        } else {
169
            $timestamp = (int) $dateValue;
170
        }
171
172
        return $timestamp;
173
    }
174
175
    /**
176
     * Get a URL
177
     * 
178
     * @param string $filePath
179
     * @return string
180
     */
181
    abstract public function getUrl(string $filePath);
182
183
    /**
184
     * Get a pre-signed URL
185
     * 
186
     * @param string $filePath
187
     * @param int|string|\DateTimeInterface $expires
188
     * @return string
189
     */
190
    abstract public function getPresignedUrl(string $filePath, $expires);
191
192
    /**
193
     * @return FilesystemAdapter $adapter
194
     */
195
    abstract protected function initAdapter();
196
}
197