Passed
Push — main ( 99c3ef...81ef04 )
by Sugeng
02:45
created

SftpComponent::initAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 26
rs 9.6
cc 2
nc 2
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\traits\UrlGeneratorTrait;
6
use League\Flysystem\PhpseclibV3\SftpAdapter;
7
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
8
use Yii;
9
use yii\base\InvalidConfigException;
10
11
/**
12
 * Class SftpComponent
13
 * 
14
 * ```php
15
 * 'components' => [
16
 *     'fs' => [
17
 *         'class' => \diecoding\flysystem\SftpComponent::class,
18
 *         'host' => 'hostname',
19
 *         'username' => 'username',
20
 *         'password' => null, // password (optional, default: null) set to null if privateKey is used
21
 *         'privateKey' => '/path/to/my/private_key', // private key (optional, default: null) can be used instead of password, set to null if password is set
22
 *         'passphrase' => 'super-secret-password', // passphrase (optional, default: null), set to null if privateKey is not used or has no passphrase
23
 *         'port' => 22,
24
 *         'useAgent' => true,
25
 *         'timeout' => 10,
26
 *         'maxTries' => 4,
27
 *         'hostFingerprint' => null,
28
 *         '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)
29
 *         'preferredAlgorithms' => [],
30
 *         'root' => '/root/path/', // or you can use @alias
31
 *         'action' => '/site/file',
32
 *         'prefix' => '', 
33
 *     ],
34
 * ],
35
 * ```
36
 * 
37
 * @link      https://sugengsulistiyawan.my.id/
38
 * @author    Sugeng Sulistiyawan <[email protected]>
39
 * @copyright Copyright (c) 2023
40
 */
41
class SftpComponent extends AbstractComponent
42
{
43
    use UrlGeneratorTrait;
44
45
    /**
46
     * @var string
47
     */
48
    public $host;
49
50
    /**
51
     * @var string
52
     */
53
    public $username;
54
55
    /**
56
     * @var string|null
57
     */
58
    public $password = null;
59
60
    /**
61
     * @var string|null
62
     */
63
    public $privateKey = null;
64
65
    /**
66
     * @var string|null
67
     */
68
    public $passphrase = null;
69
70
    /**
71
     * @var int
72
     */
73
    public $port = 22;
74
75
    /**
76
     * @var bool
77
     */
78
    public $useAgent = false;
79
80
    /**
81
     * @var int
82
     */
83
    public $timeout = 10;
84
    /**
85
     * @var int
86
     */
87
    public $maxTries = 4;
88
89
    /**
90
     * @var string|null
91
     */
92
    public $hostFingerprint = null;
93
94
    /**
95
     * @var \League\Flysystem\PhpseclibV3\ConnectivityChecker|null
96
     */
97
    public $connectivityChecker = null;
98
99
    /**
100
     * @var array
101
     */
102
    public $preferredAlgorithms = [];
103
104
    /**
105
     * @var string
106
     */
107
    public $root;
108
109
    /**
110
     * @var SftpConnectionOptions
0 ignored issues
show
Bug introduced by
The type diecoding\flysystem\SftpConnectionOptions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
     */
112
    protected $_connectionOptions;
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function init()
118
    {
119
        if (empty($this->host)) {
120
            throw new InvalidConfigException('The "host" property must be set.');
121
        }
122
        if (empty($this->username)) {
123
            throw new InvalidConfigException('The "username" property must be set.');
124
        }
125
126
        $this->initEncrypter($this->passphrase ?? $this->password ?? $this->username, $this->username);
127
128
        parent::init();
129
    }
130
131
    /**
132
     * @return SftpAdapter
133
     */
134
    protected function initAdapter()
135
    {
136
        $this->root = (string) Yii::getAlias($this->root);
137
        $this->root = $this->normalizePath($this->root . '/' . $this->prefix);
138
139
        $options = [];
140
        foreach ([
141
            'host',
142
            'username',
143
            'password',
144
            'privateKey',
145
            'passphrase',
146
            'port',
147
            'useAgent',
148
            'timeout',
149
            'maxTries',
150
            'hostFingerprint',
151
            'connectivityChecker',
152
            'preferredAlgorithms',
153
        ] as $property) {
154
            $options[$property] = $this->$property;
155
        }
156
157
        $this->_connectionOptions = SftpConnectionProvider::fromArray($options);
0 ignored issues
show
Documentation Bug introduced by
It seems like League\Flysystem\Phpsecl...er::fromArray($options) of type League\Flysystem\Phpsecl...\SftpConnectionProvider is incompatible with the declared type diecoding\flysystem\SftpConnectionOptions of property $_connectionOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
158
159
        return new SftpAdapter($this->_connectionOptions, $this->root);
160
    }
161
}
162