Passed
Push — main ( e17069...9116fa )
by Sugeng
03:34
created

ZipArchiveComponent::init()   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 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use League\Flysystem\ChecksumAlgoIsNotSupported;
6
use League\Flysystem\ChecksumProvider;
7
use League\Flysystem\Config;
8
use League\Flysystem\UnableToGeneratePublicUrl;
9
use League\Flysystem\UnableToGenerateTemporaryUrl;
10
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
11
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
12
use League\Flysystem\WebDAV\WebDAVAdapter;
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
 * - PublicUrl setting or retrieving is not supported.
24
 * - TemporaryUrl setting or retrieving is not supported.
25
 * @see https://flysystem.thephpleague.com/docs/adapter/zip-archive/
26
 * 
27
 * ```php
28
 * 'components' => [
29
 *     'fs' => [
30
 *         'class' => \diecoding\flysystem\ZipArchiveComponent::class,
31
 *         'pathToZip' => dirname(dirname(__DIR__)) . '/storage.zip', // or you can use @alias
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
    /**
44
     * @var string
45
     */
46
    public $pathToZip;
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function init()
52
    {
53
        if (empty($this->pathToZip)) {
54
            throw new InvalidConfigException('The "pathToZip" property must be set.');
55
        }
56
57
        parent::init();
58
    }
59
60
    /**
61
     * @return WebDAVAdapter
62
     */
63
    protected function initAdapter()
64
    {
65
        $this->pathToZip = (string) Yii::getAlias($this->pathToZip);
66
67
        return new ZipArchiveAdapter(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new League\Flysys...hToZip), $this->prefix) returns the type League\Flysystem\ZipArchive\ZipArchiveAdapter which is incompatible with the documented return type League\Flysystem\WebDAV\WebDAVAdapter.
Loading history...
68
            new FilesystemZipArchiveProvider($this->pathToZip),
69
            $this->prefix
70
        );
71
    }
72
73
    public function publicUrl(string $path, Config $config): string
74
    {
75
        if ($this->debug) {
76
            throw new UnableToGeneratePublicUrl('ZipArchiveComponent does not support this operation.', $path);
77
        }
78
79
        return '';
80
    }
81
82
    public function temporaryUrl(string $path, \DateTimeInterface $expiresAt, Config $config): string
83
    {
84
        if ($this->debug) {
85
            throw new UnableToGenerateTemporaryUrl('ZipArchiveComponent does not support this operation.', $path);
86
        }
87
88
        return '';
89
    }
90
91
    public function checksum(string $path, Config $config): string
92
    {
93
        if ($this->debug) {
94
            throw new ChecksumAlgoIsNotSupported('ZipArchiveComponent does not support this operation.');
95
        }
96
97
        return '';
98
    }
99
}