Passed
Push — main ( 9b4a35...59a807 )
by Sugeng
02:50
created

ZipArchiveComponent::temporaryUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\traits\UrlGeneratorTrait;
6
use League\Flysystem\ChecksumAlgoIsNotSupported;
7
use League\Flysystem\ChecksumProvider;
8
use League\Flysystem\Config;
9
use League\Flysystem\UnableToGeneratePublicUrl;
10
use League\Flysystem\UnableToGenerateTemporaryUrl;
11
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
12
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
13
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
14
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
18
/**
19
 * Interacting with an ZipArchive filesystem
20
 * ! Notice
21
 * It's important to know this adapter does not fully comply with the adapter contract. The difference(s) is/are:
22
 * - Checksum setting or retrieving is not supported.
23
 * @see https://flysystem.thephpleague.com/docs/adapter/zip-archive/
24
 * 
25
 * ```php
26
 * 'components' => [
27
 *     'fs' => [
28
 *         'class' => \diecoding\flysystem\ZipArchiveComponent::class,
29
 *         'pathToZip' => dirname(dirname(__DIR__)) . '/storage.zip', // or you can use @alias
30
 *         'secret' => 'my-secret', // for secure route url
31
 *         // 'action' => '/site/file', // action route
32
 *         // 'prefix' => '', // root directory inside zip file
33
 *     ],
34
 * ],
35
 * ```
36
 * 
37
 * @link      https://sugengsulistiyawan.my.id/
38
 * @author    Sugeng Sulistiyawan <[email protected]>
39
 * @copyright Copyright (c) 2023
40
 */
41
class ZipArchiveComponent extends AbstractComponent implements PublicUrlGenerator, TemporaryUrlGenerator, ChecksumProvider
42
{
43
    use UrlGeneratorTrait;
44
45
    /**
46
     * @var string
47
     */
48
    public $pathToZip;
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        if (empty($this->pathToZip)) {
56
            throw new InvalidConfigException('The "pathToZip" property must be set.');
57
        }
58
        if (empty($this->secret)) {
0 ignored issues
show
Bug Best Practice introduced by
The property secret does not exist on diecoding\flysystem\ZipArchiveComponent. Since you implemented __get, consider adding a @property annotation.
Loading history...
59
            throw new InvalidConfigException('The "secret" property must be set.');
60
        }
61
62
        $this->initEncrypter($this->secret);
63
64
        parent::init();
65
    }
66
67
    /**
68
     * @return ZipArchiveAdapter
69
     */
70
    protected function initAdapter()
71
    {
72
        $this->pathToZip = (string) Yii::getAlias($this->pathToZip);
73
74
        return new ZipArchiveAdapter(
75
            new FilesystemZipArchiveProvider($this->pathToZip),
76
            (string) $this->prefix
77
        );
78
    }
79
80
    public function checksum(string $path, Config $config): string
81
    {
82
        if ($this->debug) {
83
            throw new ChecksumAlgoIsNotSupported('ZipArchiveComponent does not support this operation.');
84
        }
85
86
        return '';
87
    }
88
}