Passed
Push — main ( 989c75...a2b15c )
by Sugeng
03:11
created

WebDavComponent::checksum()   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 2
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\UrlGeneration\TemporaryUrlGenerator;
9
use League\Flysystem\WebDAV\WebDAVAdapter;
10
use Sabre\DAV\Client;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Interacting with an webdav filesystem
15
 * ! Notice
16
 * It's important to know this adapter does not fully comply with the adapter contract. The difference(s) is/are:
17
 * - Visibility setting or retrieving is not supported.
18
 * - Checksum setting or retrieving is not supported.
19
 * - TemporaryUrl setting or retrieving equal to PublicUrl.
20
 * @see https://flysystem.thephpleague.com/docs/adapter/webdav/
21
 * 
22
 * ```php
23
 * 'components' => [
24
 *     'fs' => [
25
 *         'class' => \diecoding\flysystem\WebDavComponent::class,
26
 *         'baseUri' => 'http://your-webdav-server.org/',
27
 *         'userName' => 'your_user',
28
 *         'password' => 'superSecret1234',
29
 *         'prefix' => '',
30
 *         // 'proxy' => '',
31
 *         // 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
32
 *         // 'encoding' => \Sabre\DAV\Client::ENCODING_IDENTITY,
33
 *     ],
34
 * ],
35
 * ```
36
 * 
37
 * @link      https://sugengsulistiyawan.my.id/
38
 * @author    Sugeng Sulistiyawan <[email protected]>
39
 * @copyright Copyright (c) 2023
40
 */
41
class WebDavComponent extends AbstractComponent implements TemporaryUrlGenerator, ChecksumProvider
42
{
43
    /**
44
     * @var string
45
     */
46
    public $baseUri;
47
48
    /**
49
     * @var string
50
     */
51
    public $userName;
52
53
    /**
54
     * @var string
55
     */
56
    public $password;
57
58
    /**
59
     * @var mixed
60
     */
61
    public $proxy;
62
63
    /**
64
     *  authType must be a bitmap, using Client::AUTH_BASIC, Client::AUTH_DIGEST
65
     *  and Client::AUTH_NTLM. If you know which authentication method will be
66
     *  used, it's recommended to set it, as it will save a great deal of
67
     *  requests to 'discover' this information.
68
     * 
69
     * @var int
70
     */
71
    public $authType;
72
73
    /**
74
     * Encoding is a bitmap with one of the ENCODING constants.
75
     * 
76
     * @var int
77
     */
78
    public $encoding;
79
80
    /**
81
     * @var Client
82
     */
83
    protected $client;
84
85
    /**
86
     * @var array
87
     */
88
    protected $_availableOptions = [
89
        'baseUri',
90
        'userName',
91
        'password',
92
        'proxy',
93
        'authType',
94
        'encoding',
95
    ];
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function init()
101
    {
102
        if (empty($this->baseUri)) {
103
            throw new InvalidConfigException('The "baseUri" property must be set.');
104
        }
105
106
        parent::init();
107
    }
108
109
    /**
110
     * @return WebDAVAdapter
111
     */
112
    protected function initAdapter()
113
    {
114
        $config = [];
115
        foreach ($this->_availableOptions as $property) {
116
            if ($this->$property !== null) {
117
                $config[$property] = $this->$property;
118
            }
119
        }
120
121
        $this->client = new Client($config);
122
123
        return new WebDAVAdapter($this->client, $this->bucket, $this->debug ? WebDAVAdapter::ON_VISIBILITY_THROW_ERROR : WebDAVAdapter::ON_VISIBILITY_IGNORE);
0 ignored issues
show
Bug Best Practice introduced by
The property bucket does not exist on diecoding\flysystem\WebDavComponent. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
    }
125
126
    public function temporaryUrl(string $path, \DateTimeInterface $expiresAt, Config $config): string
127
    {
128
        return $this->publicUrl($path, $config);
129
    }
130
131
    public function checksum(string $path, Config $config): string
132
    {
133
        if ($this->debug) {
134
            throw new ChecksumAlgoIsNotSupported('WebDavComponent does not support this operation.');
135
        }
136
137
        return '';
138
    }
139
}