WebDavComponent::initAdapter()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 4
nc 3
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\adapter\WebDAVAdapter;
6
use Sabre\DAV\Client;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Interacting with an WebDAV filesystem
11
 * ! Notice
12
 * It's important to know this adapter does not fully comply with the adapter contract. The difference(s) is/are:
13
 * - Visibility setting or retrieving is not supported.
14
 * - TemporaryUrl setting or retrieving equal to PublicUrl.
15
 * @see https://flysystem.thephpleague.com/docs/adapter/webdav/
16
 * 
17
 * ```php
18
 * 'components' => [
19
 *     'fs' => [
20
 *         'class' => \diecoding\flysystem\WebDavComponent::class,
21
 *         'baseUri' => 'http://your-webdav-server.org/',
22
 *         'userName' => 'your_user',
23
 *         'password' => 'superSecret1234',
24
 *         // 'proxy' => '',
25
 *         // 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
26
 *         // 'encoding' => \Sabre\DAV\Client::ENCODING_IDENTITY,
27
 *         // 'prefix' => '',
28
 *     ],
29
 * ],
30
 * ```
31
 * 
32
 * @link      https://sugengsulistiyawan.my.id/
33
 * @author    Sugeng Sulistiyawan <[email protected]>
34
 * @copyright Copyright (c) 2023
35
 */
36
class WebDavComponent extends AbstractComponent
37
{
38
    /**
39
     * @var string
40
     */
41
    public $baseUri;
42
43
    /**
44
     * @var string
45
     */
46
    public $userName;
47
48
    /**
49
     * @var string
50
     */
51
    public $password;
52
53
    /**
54
     * @var mixed
55
     */
56
    public $proxy;
57
58
    /**
59
     *  authType must be a bitmap, using Client::AUTH_BASIC, Client::AUTH_DIGEST
60
     *  and Client::AUTH_NTLM. If you know which authentication method will be
61
     *  used, it's recommended to set it, as it will save a great deal of
62
     *  requests to 'discover' this information.
63
     * 
64
     * @var int
65
     */
66
    public $authType;
67
68
    /**
69
     * Encoding is a bitmap with one of the ENCODING constants.
70
     * 
71
     * @var int
72
     */
73
    public $encoding;
74
75
    /**
76
     * @var Client
77
     */
78
    protected $client;
79
80
    /**
81
     * @var string[]
82
     */
83
    protected $_availableOptions = [
84
        'baseUri',
85
        'userName',
86
        'password',
87
        'proxy',
88
        'authType',
89
        'encoding',
90
    ];
91
92
    /**
93
     * @inheritdoc
94
     * @throws InvalidConfigException
95
     */
96
    public function init()
97
    {
98
        $this->validateProperties([
99
            'baseUri',
100
        ]);
101
102
        parent::init();
103
    }
104
105
    /**
106
     * @return WebDAVAdapter
107
     */
108
    protected function initAdapter()
109
    {
110
        $config = [];
111
        foreach ($this->_availableOptions as $property) {
112
            if ($this->$property !== null) {
113
                $config[$property] = $this->$property;
114
            }
115
        }
116
117
        $this->client = new Client($config);
118
119
        return new WebDAVAdapter($this->client, (string) $this->prefix, $this->debug ? WebDAVAdapter::ON_VISIBILITY_THROW_ERROR : WebDAVAdapter::ON_VISIBILITY_IGNORE);
120
    }
121
}
122