Completed
Pull Request — master (#140)
by
unknown
05:09
created

Sftp::getBackupFiles()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 19
cp 0
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 12
nc 6
nop 0
crap 42
1
<?php
2
namespace phpbu\App\Backup\Collector;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Backup\Target;
6
7
class Sftp extends Collector
8
{
9
    /**
10
     * @var \phpseclib\Net\SFTP
11
     */
12
    protected $sftp;
13
14
    /**
15
     * OpenStack remote path
16
     *
17
     * @var string
18
     */
19
    protected $path;
20
21
    /**
22
     * OpenStack constructor.
23
     *
24
     * @param \phpbu\App\Backup\Target $target
25
     * @param \phpseclib\Net\SFTP      $sftp
26
     * @param string                   $path
27
     */
28
    public function __construct(Target $target, \phpseclib\Net\SFTP $sftp, string $path)
29
    {
30
        $this->sftp = $sftp;
31
        $this->path = $path;
32
        $this->setUp($target);
33
    }
34
35
    /**
36
     * Get all created backups.
37
     *
38
     * @return \phpbu\App\Backup\File[]
39
     */
40
    public function getBackupFiles(): array
41
    {
42
        $list = $this->sftp->_list($this->path);
43
        foreach ($list as $filename => $fileInfo) {
44
            if (in_array($filename, ['.', '..'])) {
45
                continue;
46
            }
47
            if ($fileInfo['type'] === 2) {
48
                continue;
49
            }
50
            // skip currently created backup
51
            if ($fileInfo['filename'] == $this->target->getFilename()) {
52
                continue;
53
            }
54
            if ($this->isFilenameMatch($fileInfo['filename'])) {
55
                $this->files[] = new \phpbu\App\Backup\File\Sftp($this->sftp, $fileInfo, $this->path);
56
            }
57
        }
58
59
        return $this->files;
60
    }
61
}