Passed
Push — master ( 5b5e9c...5b3e8e )
by Jay
04:01
created

FtpImplicitSSL::phpCurlError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author jyamin
4
 */
5
6
namespace FMUP\Ftp\Driver;
7
8
use FMUP\Ftp\FtpAbstract;
9
use FMUP\Ftp\Exception as FtpException;
10
use FMUP\Logger;
11
12
class FtpImplicitSSL extends FtpAbstract implements Logger\LoggerInterface
13
{
14
    use Logger\LoggerTrait;
15
16
    const CURL_OPTIONS = 'curl_options';
17
    const PASSIVE_MODE = 'passive_mode';
18
19
    /** @var string $url */
20
    private $url;
21
22
    /**
23
     * @return array
24
     */
25 1
    protected function getCurlOptions()
26
    {
27 1
        return isset($this->settings[self::CURL_OPTIONS]) ? $this->settings[self::CURL_OPTIONS] : array();
28
    }
29
30
    /**
31
     * @return bool
32
     */
33 1
    protected function getPassiveMode()
34
    {
35 1
        return isset($this->settings[self::PASSIVE_MODE]) ? $this->settings[self::PASSIVE_MODE] : false;
36
    }
37
38
    /**
39
     * @return string
40
     */
41 1
    protected function getUrl()
42
    {
43 1
        return $this->url;
44
    }
45
46
    /**
47
     * @param string $url
48
     * @return $this
49
     */
50 1
    protected function setUrl($url)
51
    {
52 1
        $this->url = $url;
53 1
        return $this;
54
    }
55
56
    /**
57
     * FtpImplicitSSL constructor.
58
     * @param array $params
59
     */
60 12
    public function __construct($params = array())
61
    {
62 12
        $params = array_replace_recursive(array(
63 12
            self::CURL_OPTIONS => array(
64 12
                CURLOPT_FTP_SSL => CURLFTPSSL_ALL,
65 12
                CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT,
66 12
                CURLOPT_SSL_VERIFYHOST => false,
67 12
                CURLOPT_SSL_VERIFYPEER => false,
68 12
                CURLOPT_TIMEOUT => 30,
69 12
            ),
70 12
         ), $params);
71 12
        parent::__construct($params);
72 12
    }
73
74
    /**
75
     * @param string $host
76
     * @param int $port
77
     * @throws FtpException
78
     * @return $this
79
     */
80 3
    public function connect($host, $port = 990)
81
    {
82 3
        $this->setSession($this->phpCurlInit());
83 3
        if (!$this->getSession()) {
84 1
            $this->log(Logger::ERROR, "Could not initialize cURL", (array)$this->getSettings());
85 1
            throw new FtpException("Could not initialize cURL");
86
        }
87 2
        $this->settings[self::CURL_OPTIONS][CURLOPT_PORT] = $port;
88 2
        $this->setUrl('ftps://' . $host . '/');
89 2
        if (!$this->getPassiveMode()) {
90 1
            $this->settings[self::CURL_OPTIONS][CURLOPT_FTPPORT] = '-';
91 1
        }
92 2
        return $this;
93
    }
94
95
    /**
96
     * @param string $user
97
     * @param string $pass
98
     * @throws FtpException
99
     * @return bool
100
     */
101 2
    public function login($user, $pass)
102
    {
103 2
        $this->settings[self::CURL_OPTIONS][CURLOPT_USERPWD] = $user . ':' . $pass;
104
105 2
        foreach ($this->getCurlOptions() as $optName => $optValue) {
106 2
            if (!$this->phpCurlSetOpt($this->getSession(), $optName, $optValue)) {
107 1
                $this->log(Logger::ERROR, 'Unable to set cURL option : ' . $optName, (array)$this->getSettings());
108 1
                throw new FtpException('Unable to set cURL option : ' . $optName);
109
            }
110 1
        }
111 1
        return true;
112
    }
113
114
    /**
115
     * @param string $localFile
116
     * @param string $remoteFile
117
     * @throws FtpException
118
     * @return bool
119
     */
120 2
    public function get($localFile, $remoteFile)
121
    {
122 2
        $file = $this->phpFopen($localFile, 'w');
123 2 View Code Duplication
        if (!$file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124 1
            $this->log(Logger::ERROR, 'Unable to open file to write : ' . $localFile, (array)$this->getSettings());
125 1
            throw new FtpException('Unable to open file to write : ' . $localFile);
126
        }
127 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_URL, $this->getUrl() . $remoteFile);
128 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_FOLLOWLOCATION, 1);
129 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_RETURNTRANSFER, 1);
130 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_UPLOAD, false);
131 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_FILE, $file);
132 1
        $result = $this->phpCurlExec($this->getSession());
133 1
        $this->phpFclose($file);
134 1
        return $result;
135
    }
136
137
    /**
138
     * Put file on ftp server
139
     * @param string $remoteFile
140
     * @param string $localFile
141
     * @return bool
142
     * @throws FtpException
143
     */
144 2
    public function put($remoteFile, $localFile)
145
    {
146 2
        $file = $this->phpFopen($localFile, 'r');
147 2 View Code Duplication
        if (!$file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148 1
            $this->log(Logger::ERROR, 'Unable to open file to read : ' . $localFile, (array)$this->getSettings());
149 1
            throw new FtpException('Unable to open file to read : ' . $localFile);
150
        }
151 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_URL, $this->getUrl() . $remoteFile);
152 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_UPLOAD, 1);
153 1
        $this->phpCurlSetOpt($this->getSession(), CURLOPT_INFILE, $file);
154 1
        $this->phpCurlExec($this->getSession());
155 1
        $this->phpFclose($file);
156 1
        return !$this->phpCurlError($this->getSession());
157
    }
158
159
160
    /**
161
     * @param string $file
162
     * @return bool
163
     */
164 1
    public function delete($file)
165
    {
166 1
        return false;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172 1
    public function close()
173
    {
174 1
        $this->phpCurlClose($this->getSession());
175 1
        return true;
176
    }
177
178
    /**
179
     * @param string|null $url
180
     * @return resource
181
     * @codeCoverageIgnore
182
     */
183
    protected function phpCurlInit($url = null)
184
    {
185
        return curl_init($url);
186
    }
187
188
    /**
189
     * @param resource $ch
190
     * @return mixed
191
     * @codeCoverageIgnore
192
     */
193
    protected function phpCurlExec($ch)
194
    {
195
        return curl_exec($ch);
196
    }
197
198
    /**
199
     * @param resource $ch
200
     * @param int $option
201
     * @param mixed $value
202
     * @return bool
203
     * @codeCoverageIgnore
204
     */
205
    protected function phpCurlSetOpt($ch, $option, $value)
206
    {
207
        return curl_setopt($ch, $option, $value);
208
    }
209
210
    /**
211
     * @param resource $ch
212
     * @return string
213
     * @codeCoverageIgnore
214
     */
215
    protected function phpCurlError($ch)
216
    {
217
        return curl_error($ch);
218
    }
219
220
    /**
221
     * @param resource $ch
222
     * @codeCoverageIgnore
223
     */
224
    protected function phpCurlClose($ch)
225
    {
226
        curl_close($ch);
227
    }
228
229
    /**
230
     * @param string $filename
231
     * @param string $mode
232
     * @return resource
233
     * @codeCoverageIgnore
234
     */
235
    protected function phpFopen($filename, $mode)
236
    {
237
        return fopen($filename, $mode);
238
    }
239
240
    /**
241
     * @param resource $handle
242
     * @return bool
243
     * @codeCoverageIgnore
244
     */
245
    protected function phpFclose($handle)
246
    {
247
        return fclose($handle);
248
    }
249
}
250