Passed
Push — main ( 57d795...1b512a )
by Sugeng
03:46
created

ZipArchiveAdapter::setAdapter()   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 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace diecoding\flysystem\adapter;
6
7
use DateTimeInterface;
8
use diecoding\flysystem\traits\ChecksumAdapterTrait;
9
use diecoding\flysystem\traits\UrlGeneratorAdapterTrait;
10
use League\Flysystem\ChecksumProvider;
11
use League\Flysystem\Config;
12
use League\Flysystem\UnixVisibility\VisibilityConverter;
13
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
14
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
15
use League\Flysystem\ZipArchive\ZipArchiveAdapter as LeagueZipArchiveAdapter;
16
use League\Flysystem\ZipArchive\ZipArchiveProvider;
17
use League\MimeTypeDetection\MimeTypeDetector;
18
use yii\helpers\Json;
19
use yii\helpers\Url;
20
21
/**
22
 * @method bool fileExists(string $location)
23
 * @method bool directoryExists(string $location)
24
 * @method bool has(string $location) check fileExists or directoryExists
25
 * @method void write(string $location, string $contents, array $config = [])
26
 * @method void writeStream(string $location, $contents, array $config = [])
27
 * @method string read(string $location)
28
 * @method resource readStream(string $location)
29
 * @method void delete(string $location)
30
 * @method void deleteDirectory(string $location)
31
 * @method void createDirectory(string $location, array $config = [])
32
 * @method \League\Flysystem\DirectoryListing listContents(string $location, bool = \League\Flysystem\Filesystem::LIST_SHALLOW)
33
 * @method void move(string $source, string $destination, array $config = [])
34
 * @method void copy(string $source, string $destination, array $config = [])
35
 * @method int lastModified(string $path)
36
 * @method int fileSize(string $path)
37
 * @method string mimeType(string $path)
38
 * @method void setVisibility(string $path, string $visibility)
39
 * @method string visibility(string $path)
40
 * 
41
 * @link      https://sugengsulistiyawan.my.id/
42
 * @author    Sugeng Sulistiyawan <[email protected]>
43
 * @copyright Copyright (c) 2024
44
 */
45
final class ZipArchiveAdapter implements ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator
46
{
47
    use UrlGeneratorAdapterTrait, ChecksumAdapterTrait;
0 ignored issues
show
introduced by
The trait diecoding\flysystem\trai...rlGeneratorAdapterTrait requires some properties which are not provided by diecoding\flysystem\adapter\ZipArchiveAdapter: $prefix, $action
Loading history...
48
49
    /**
50
     * @var LeagueZipArchiveAdapter
51
     */
52
    protected $_adapter;
53
54
    public function __construct(
55
        private ZipArchiveProvider $zipArchiveProvider,
56
        string $root = '',
57
        ?MimeTypeDetector $mimeTypeDetector = null,
58
        ?VisibilityConverter $visibility = null,
59
        private bool $detectMimeTypeUsingPath = false,
60
    ) {
61
        $this->setAdapter(new LeagueZipArchiveAdapter($zipArchiveProvider, $root, $mimeTypeDetector, $visibility, $detectMimeTypeUsingPath));
62
    }
63
64
    /**
65
     * @param string $method
66
     * @param array $parameters
67
     * @return mixed
68
     */
69
    public function __call($method, $parameters)
70
    {
71
        return call_user_func_array([$this->getAdapter(), $method], $parameters);
72
    }
73
74
    /**
75
     * @return LeagueZipArchiveAdapter
76
     */
77
    public function getAdapter()
78
    {
79
        return $this->_adapter;
80
    }
81
82
    /**
83
     * @param LeagueZipArchiveAdapter $value
84
     * @return void
85
     */
86
    public function setAdapter(LeagueZipArchiveAdapter $value)
87
    {
88
        $this->_adapter = $value;
89
    }
90
91
    public function publicUrl(string $path, Config $config): string
92
    {
93
        // TODO: Use absolute path and don't encrypt
94
        $params = [
95
            'path' => $path,
96
            'expires' => 0,
97
        ];
98
99
        return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
0 ignored issues
show
Bug introduced by
The method encrypt() does not exist on diecoding\flysystem\AbstractComponent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

99
        return Url::toRoute([$this->component?->action, 'data' => $this->component?->/** @scrutinizer ignore-call */ encrypt(Json::encode($params))], true);
Loading history...
Bug Best Practice introduced by
The property action does not exist on diecoding\flysystem\AbstractComponent. Since you implemented __get, consider adding a @property annotation.
Loading history...
100
    }
101
102
    public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
103
    {
104
        // TODO: Use absolute path and don't encrypt
105
        $params = [
106
            'path' => $path,
107
            'expires' => (int) $expiresAt->getTimestamp(),
108
        ];
109
110
        return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
0 ignored issues
show
Bug Best Practice introduced by
The property action does not exist on diecoding\flysystem\AbstractComponent. Since you implemented __get, consider adding a @property annotation.
Loading history...
111
    }
112
}
113