SftpComponent::init()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\adapter\SftpAdapter;
6
use diecoding\flysystem\traits\UrlGeneratorComponentTrait;
7
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
8
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
9
use Yii;
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * Interacting with an SFTP filesystem
14
 * This implementation uses version 3 of phpseclib
15
 * @see https://flysystem.thephpleague.com/docs/adapter/sftp-v3/
16
 * 
17
 * ```php
18
 * 'components' => [
19
 *     'fs' => [
20
 *         'class' => \diecoding\flysystem\SftpComponent::class,
21
 *         'host' => 'hostname',
22
 *         'username' => 'username',
23
 *         'password' => null, // password (optional, default: null) set to null if privateKey is used
24
 *         // 'privateKey' => '/path/to/my/private_key', // private key (optional, default: null) can be used instead of password, set to null if password is set
25
 *         // 'passphrase' => 'super-secret-password', // passphrase (optional, default: null), set to null if privateKey is not used or has no passphrase
26
 *         // 'port' => 22,
27
 *         // 'useAgent' => true,
28
 *         // 'timeout' => 10,
29
 *         // 'maxTries' => 4,
30
 *         // 'hostFingerprint' => null,
31
 *         // 'connectivityChecker' => null, // connectivity checker (must be an implementation of `League\Flysystem\PhpseclibV2\ConnectivityChecker` to check if a connection can be established (optional, omit if you don't need some special handling for setting reliable connections)
32
 *         // 'preferredAlgorithms' => [],
33
 *         // 'root' => '/root/path/', // or you can use @alias
34
 *         // 'action' => '/site/file', // action route
35
 *         // 'prefix' => '',
36
 *     ],
37
 * ],
38
 * ```
39
 * 
40
 * @link      https://sugengsulistiyawan.my.id/
41
 * @author    Sugeng Sulistiyawan <[email protected]>
42
 * @copyright Copyright (c) 2023
43
 */
44
class SftpComponent extends AbstractComponent
45
{
46
    use UrlGeneratorComponentTrait;
47
48
    /**
49
     * @var string
50
     */
51
    public $host;
52
53
    /**
54
     * @var string
55
     */
56
    public $username;
57
58
    /**
59
     * @var string
60
     */
61
    public $password;
62
63
    /**
64
     * @var string
65
     */
66
    public $privateKey;
67
68
    /**
69
     * @var string
70
     */
71
    public $passphrase;
72
73
    /**
74
     * @var int
75
     */
76
    public $port;
77
78
    /**
79
     * @var bool
80
     */
81
    public $useAgent;
82
83
    /**
84
     * @var int
85
     */
86
    public $timeout;
87
    /**
88
     * @var int
89
     */
90
    public $maxTries;
91
92
    /**
93
     * @var string
94
     */
95
    public $hostFingerprint;
96
97
    /**
98
     * @var \League\Flysystem\PhpseclibV3\ConnectivityChecker
99
     */
100
    public $connectivityChecker;
101
102
    /**
103
     * @var array
104
     */
105
    public $preferredAlgorithms;
106
107
    /**
108
     * @var string
109
     */
110
    public $root;
111
112
    /**
113
     * @var string[]
114
     */
115
    protected $_availableOptions = [
116
        'host',
117
        'username',
118
        'password',
119
        'privateKey',
120
        'passphrase',
121
        'port',
122
        'useAgent',
123
        'timeout',
124
        'maxTries',
125
        'hostFingerprint',
126
        'connectivityChecker',
127
        'preferredAlgorithms',
128
    ];
129
130
    /**
131
     * @var SftpConnectionProvider
132
     */
133
    protected $_connectionProvider;
134
135
    /**
136
     * @inheritdoc
137
     * @throws InvalidConfigException
138
     */
139
    public function init()
140
    {
141
        $this->validateProperties([
142
            'host',
143
            'username',
144
        ]);
145
146
        $this->passphrase = $this->passphrase ?: ($this->password ?: ($this->username ?: Yii::$app->id));
147
        $this->initEncrypter($this->passphrase);
148
149
        parent::init();
150
    }
151
152
    /**
153
     * @return SftpAdapter|PathPrefixedAdapter
154
     */
155
    protected function initAdapter()
156
    {
157
        $this->root = (string) Yii::getAlias($this->root);
158
159
        $options = [];
160
        foreach ($this->_availableOptions as $property) {
161
            if ($this->$property !== null) {
162
                $options[$property] = $this->$property;
163
            }
164
        }
165
166
        $this->_connectionProvider = SftpConnectionProvider::fromArray($options);
167
168
        $adapter = new SftpAdapter($this->_connectionProvider, $this->root);
169
        // for UrlGeneratorAdapterTrait
170
        $adapter->component = $this;
171
172
        if ($this->prefix) {
173
            $adapter = new PathPrefixedAdapter($adapter, $this->prefix);
174
        }
175
176
        return $adapter;
177
    }
178
}
179