Passed
Push — main ( e5d18b...282b2b )
by Sugeng
03:08
created

SftpComponent::checksum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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