Completed
Push — @debug ( b8003a...96cfc2 )
by Maarten
49:23
created

SFtpService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the Amplexor\XConnect library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/amplexor-drupal/xconnect/
7
 * @version 1.0.0
8
 * @package Amplexor.XConnect
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Amplexor\XConnect\Service;
15
16
use Amplexor\XConnect\Request\File\FileInterface;
17
18
/**
19
 * SFTP Service.
20
 */
21
class SFtpService extends FtpService
22
{
23
    /**
24
     * The transfer mode.
25
     *
26
     * Value is substitute for the NET_SFTP_LOCAL_FILE constant.
27
     *
28
     * @var int
29
     */
30
    const TRANSFER_MODE = 1;
31
32
    /**
33
     * The SFTP connection.
34
     *
35
     * @var \Net_SFTP
36
     */
37
    private $sftp;
38
39
    /**
40
     * Is the connection logged in?
41
     *
42
     * @var bool
43
     */
44
    private $loggedIn = false;
45
46
47
    /**
48
     * @inheritDoc
49
     */
50 39
    public function __construct(array $config)
51
    {
52 39
        if (!isset($config['port'])) {
53 39
            $config['port'] = 22;
54 39
        }
55
56 39
        parent::__construct($config);
57 39
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 15
    public function send(FileInterface $file)
63
    {
64 15
        $connection = $this->getConnection();
65 12
        $from = $file->getPath();
66 12
        $to = $this->getDirectorySend() . '/' . $file->getFileName();
67
68 12
        $result = $connection->put($to, $from, self::TRANSFER_MODE);
69 12
        if (!$result) {
70 3
            throw new ServiceException(
71 3
                sprintf(
72 3
                    'File "%s" could not be uploaded to "%s".',
73 3
                    $from,
74
                    $to
75 3
                )
76 3
            );
77
        }
78
79 9
        return $result;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 6
    public function scan()
86
    {
87 6
        $connection = $this->getConnection();
88 6
        $files = array();
89
90 6
        $rawFiles = $connection->rawlist($this->getDirectoryReceive());
91 6
        if (empty($rawFiles)) {
92 3
            return $files;
93
        }
94
95
        // We only want files, no directories.
96 3
        foreach ($rawFiles as $rawFile) {
97 3
            if ($rawFile['type'] !== 1) {
98 3
                continue;
99
            }
100
101 3
            $files[] = $rawFile['filename'];
102 3
        }
103
104 3
        return $files;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 9
    public function receive($fileName, $localDirectory)
111
    {
112 9
        $connection = $this->getConnection();
113
114 9
        $to = $localDirectory . '/' . $fileName;
115 9
        $from = $this->getDirectoryReceive() . '/' . $fileName;
116 9
        $result = $connection->get($from, $to);
117
118 9
        if (!$result) {
119 3
            throw new ServiceException(
120 3
                sprintf(
121 3
                    'File "%s" could not be downloaded to "%s".',
122 3
                    $from,
123
                    $to
124 3
                )
125 3
            );
126
        }
127
128 6
        return $to;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 9
    public function processed($fileName)
135
    {
136 9
        $connection = $this->getConnection();
137 9
        $from = $this->getDirectoryReceive() . '/' . $fileName;
138 9
        $to = $this->getDirectoryReceiveProcessed() . '/' . $fileName;
139
140 9
        if ($this->isDebug()) {
141
            $result = $connection->rename($from, $to);
142
        }
143
        else {
144 9
            $result = $connection->delete($from);
145
        }
146
147 9
        if (!$result) {
148 9
            throw new ServiceException(
149 9
                sprintf(
150 9
                    'File "%s" could not be moved to "%s".',
151 9
                    $from,
152
                    $to
153 9
                )
154 9
            );
155
        }
156
157
        return $result;
158
    }
159
160
    /**
161
     * Inject the connection for testing purpose.
162
     *
163
     * @param \Net_SFTP $connection
164
     *   The connection object.
165
     */
166 39
    public function setConnection(\Net_SFTP $connection)
167
    {
168 39
        $this->sftp = $connection;
169 39
    }
170
171
    /**
172
     * Get the connection.
173
     *
174
     * @return \Net_SFTP
175
     */
176 39
    protected function getConnection()
177
    {
178 39
        $this->sftp = $this->sftp ?: new \Net_SFTP($this->getHostname(), $this->getPort(), $this->getTimeout());
179 39
        $this->login();
180 36
        return $this->sftp;
181
    }
182
183
    /**
184
     * Login to the SFTP service.
185
     *
186
     * @throws ServiceException
187
     *   Whe we can't login to the SFTP server.
188
     */
189 39
    protected function login()
190
    {
191 39
        if ($this->loggedIn) {
192 3
            return true;
193
        }
194
195 39
        $result = $this->sftp->login(
196 39
            $this->getUsername(),
197 39
            $this->getPassword()
198 39
        );
199 39
        if (!$result) {
200 3
            throw new ServiceException(
201 3
                sprintf('Can\'t login with user "%s".', $this->getUsername())
202 3
            );
203
        }
204
205 36
        $this->loggedIn = true;
206 36
    }
207
}
208