Test Failed
Push — main ( 1f6f9b...f1a6e0 )
by Sugeng
03:22
created

LocalComponent::publicUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\traits\UrlGeneratorTrait;
6
use League\Flysystem\Local\LocalFilesystemAdapter;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Class LocalComponent
12
 * 
13
 * ```php
14
 * 'components' => [
15
 *     'fs' => [
16
 *         'class' => \diecoding\flysystem\LocalComponent::class,
17
 *         'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias
18
 *         'key' => 'my-key',
19
 *         'secret' => 'my-secret', 
20
 *         'action' => '/site/file',
21
 *         'prefix' => '',
22
 *     ],
23
 * ],
24
 * ```
25
 * 
26
 * @package diecoding\flysystem
27
 * 
28
 * @link      https://sugengsulistiyawan.my.id/
29
 * @author    Sugeng Sulistiyawan <[email protected]>
30
 * @copyright Copyright (c) 2023
31
 */
32
class LocalComponent extends AbstractComponent
33
{
34
    use UrlGeneratorTrait;
35
36
    /**
37
     * @var string
38
     */
39
    public $path;
40
41
    /**
42
     * @var string
43
     */
44
    public $secret;
45
46
    /**
47
     * @var string
48
     */
49
    public $key;
50
51
    /**
52
     * @var string
53
     */
54
    protected $_location;
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function init()
60
    {
61
        if (empty($this->path)) {
62
            throw new InvalidConfigException('The "path" property must be set.');
63
        }
64
        if (empty($this->secret)) {
65
            throw new InvalidConfigException('The "secret" property must be set.');
66
        }
67
        if (empty($this->key)) {
68
            throw new InvalidConfigException('The "key" property must be set.');
69
        }
70
71
        $this->setEncrypter($this->secret, $this->key);
72
73
        parent::init();
74
    }
75
76
    /**
77
     * @return LocalFilesystemAdapter
78
     */
79
    protected function initAdapter()
80
    {
81
        $this->path      = (string) Yii::getAlias($this->path);
82
        $this->_location = $this->normalizePath($this->path . '/' . $this->prefix);
83
84
        return new LocalFilesystemAdapter($this->_location);
85
    }
86
}
87