Passed
Push — main ( 201c2b...35e5a4 )
by Sugeng
03:27
created

LocalComponent::initAdapter()   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 League\Flysystem\Local\LocalFilesystemAdapter;
6
use Yii;
7
use yii\base\InvalidConfigException;
8
use yii\helpers\Json;
9
use yii\helpers\StringHelper;
10
use yii\helpers\Url;
11
12
/**
13
 * Class LocalComponent
14
 *
15
 * @package diecoding\flysystem
16
 * 
17
 * ```php
18
 * 'components' => [
19
 *     'fs' => [
20
 *         'class' => \diecoding\flysystem\LocalComponent::class,
21
 *         'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias
22
 *         'cipherAlgo' => 'aes-128-cbc',
23
 *         'secret' => 'my-secret',
24
 *         'basePath' => '', // for multiple project in single storage, will be format to `$basePath . '/' . $path`
25
 *     ],
26
 * ],
27
 * ```
28
 * 
29
 * @link      https://sugengsulistiyawan.my.id/
30
 * @author    Sugeng Sulistiyawan <[email protected]>
31
 * @copyright Copyright (c) 2023
32
 */
33
class LocalComponent extends AbstractComponent
34
{
35
    /**
36
     * @var string
37
     */
38
    public $path;
39
40
    /**
41
     * @var string
42
     */
43
    public $cipherAlgo = 'aes-128-cbc';
44
45
    /**
46
     * @var string
47
     */
48
    public $secret;
49
50
    /**
51
     * @var string
52
     */
53
    public $action = '/site/file';
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function init()
59
    {
60
        if (empty($this->path)) {
61
            throw new InvalidConfigException('The "path" property must be set.');
62
        }
63
64
        if (empty($this->secret)) {
65
            throw new InvalidConfigException('The "secret" property must be set.');
66
        }
67
68
        $this->path = $this->normalizePath(Yii::getAlias($this->path));
0 ignored issues
show
Bug introduced by
It seems like Yii::getAlias($this->path) can also be of type false; however, parameter $path of diecoding\flysystem\Abst...ponent::normalizePath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $this->path = $this->normalizePath(/** @scrutinizer ignore-type */ Yii::getAlias($this->path));
Loading history...
69
70
        parent::init();
71
    }
72
73
    /**
74
     * Get a URL
75
     * 
76
     * @param string $filePath
77
     * @return string
78
     */
79
    public function getUrl(string $filePath)
80
    {
81
        $params = [
82
            'filePath' => $filePath,
83
            'expires' => null,
84
        ];
85
86
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
87
    }
88
89
    /**
90
     * Get a pre-signed URL
91
     * 
92
     * @param string $filePath
93
     * @param int|string|\DateTimeInterface $expires
94
     * @return string
95
     */
96
    public function getPresignedUrl(string $filePath, $expires = '+5 Minutes')
97
    {
98
        $params = [
99
            'filePath' => $filePath,
100
            'expires' => $this->convertToTimestamp($expires),
101
        ];
102
103
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
104
    }
105
106
    /**
107
     * @return LocalFilesystemAdapter
108
     */
109
    protected function initAdapter()
110
    {
111
        return new LocalFilesystemAdapter($this->path);
112
    }
113
114
    /**
115
     * Encrypts a string.
116
     * 
117
     * @param string $string the string to encrypt
118
     * @return string the encrypted string
119
     */
120
    public function encrypt($string)
121
    {
122
        $encryptedString = openssl_encrypt($string, $this->cipherAlgo, $this->secret);
123
        $encryptedString = StringHelper::base64UrlEncode(base64_encode($encryptedString));
124
125
        return $encryptedString;
126
    }
127
128
    /**
129
     * Decrypts a string. 
130
     * False is returned in case it was not possible to decrypt it.
131
     * 
132
     * @param string $string the string to decrypt
133
     * @return string the decrypted string
134
     */
135
    public function decrypt($string)
136
    {
137
        $decodedString = base64_decode(StringHelper::base64UrlDecode($string));
138
        $decodedString = openssl_decrypt($decodedString, $this->cipherAlgo, $this->secret);
139
140
        return $decodedString;
141
    }
142
143
    /**
144
     * Convert To Timestamp
145
     *
146
     * @param int|string|\DateTimeInterface $dateValue
147
     * @param int|string|null $relativeTimeBase
148
     * @return int|false
149
     */
150
    public function convertToTimestamp($dateValue, $relativeTimeBase = null)
151
    {
152
        if ($dateValue instanceof \DateTimeInterface) {
153
            $timestamp = $dateValue->getTimestamp();
154
        } elseif (!is_numeric($dateValue)) {
155
            $timestamp = strtotime($dateValue,
156
                $relativeTimeBase === null ? time() : $relativeTimeBase
0 ignored issues
show
Bug introduced by
It seems like $relativeTimeBase === nu...e() : $relativeTimeBase can also be of type string; however, parameter $baseTimestamp of strtotime() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
                /** @scrutinizer ignore-type */ $relativeTimeBase === null ? time() : $relativeTimeBase
Loading history...
157
            );
158
        } else {
159
            $timestamp = $dateValue;
160
        }
161
162
        return $timestamp;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $timestamp also could return the type string which is incompatible with the documented return type false|integer.
Loading history...
163
    }
164
}
165