Completed
Push — master ( 7a77f2...11b46c )
by Nikola
03:06 queued 48s
created

FlysystemDestination::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4286
cc 3
eloc 6
nc 3
nop 0
crap 3
1
<?php
2
3
namespace RunOpenCode\Backup\Destination;
4
5
use League\Flysystem\FilesystemInterface;
6
use RunOpenCode\Backup\Backup\Backup;
7
use RunOpenCode\Backup\Backup\File;
8
use RunOpenCode\Backup\Contract\BackupInterface;
9
use RunOpenCode\Backup\Contract\FileInterface;
10
use RunOpenCode\Backup\Exception\DestinationException;
11
12
class FlysystemDestination extends BaseDestination
13
{
14
    /**
15
     * @var FilesystemInterface
16
     */
17
    protected $flysystem;
18
19 4
    public function __construct(FilesystemInterface $flysystem)
20
    {
21 4
        $this->flysystem = $flysystem;
22 4
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function delete($name)
28
    {
29
        try {
30
            $this->flysystem->deleteDir($name);
31
        } catch (\Exception $e) {
32
            throw new DestinationException(sprintf('Unable to remove backup "%s" from flysystem destination.', $name), 0, $e);
33
        }
34
35
        if (!is_null($this->backups)) {
36
            unset($this->backups[$name]);
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function push(BackupInterface $backup)
44
    {
45 4
        $backupDirectory = $backup->getName();
46
47 4
        if (!$this->flysystem->has($backupDirectory) && !$this->flysystem->createDir($backupDirectory)) {
48
            throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backupDirectory));
49
        }
50
51 4
        $removedBackupFiles = $this->getFiles($backupDirectory);
52
53
        /**
54
         * @var FileInterface $backupFile
55
         */
56 4
        foreach ($backup->getFiles() as $backupFile) {
57
58 4
            if (isset($removedBackupFiles[$backupFile->getRelativePath()])) {
59 2
                unset($removedBackupFiles[$backupFile->getRelativePath()]);
60 1
            }
61
62 4
            $path = $backupDirectory . '/' . $backupFile->getRelativePath();
63
64
            try {
65
66 4
                if ($this->flysystem->has($path)) {
67
68 2
                    if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) {
69
                        $resource = fopen($backupFile->getPath(), 'r');
70
                        $this->flysystem->updateStream($path,  $resource);
71 1
                        fclose($resource);
72
                    }
73
74 1
                } else {
75 4
                    $resource = fopen($backupFile->getPath(), 'r');
76 4
                    $this->flysystem->putStream($path,  $resource);
77 4
                    fclose($resource);
78
                }
79
80 2
            } catch (\Exception $e) {
81 2
                throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e);
82
            }
83 2
        }
84
85
        /**
86
         * @var FileInterface $removedBackupFile
87
         */
88 4
        foreach ($removedBackupFiles as $removedBackupFile) {
89
90 2
            $path = $backupDirectory . '/' . $removedBackupFile->getRelativePath();
91
92
            try {
93 2
                $this->flysystem->delete($path);
94 1
            } catch (\Exception $e) {
95 1
                throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e);
96
            }
97 2
        }
98
99 4
        $this->removeEmptyDirectories($backupDirectory);
100
101 4
        if (!empty($this->backups)) {
102
            $this->backups[] = $backup;
103
        }
104 4
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 4
    protected function load()
110
    {
111 4
        $this->backups = array();
112
113
        /**
114
         * @var \SplFileInfo $content
115
         */
116 4
        foreach ($this->flysystem->listContents() as $content) {
117
118 4
            if ($content['type'] == 'dir') {
119
120 4
                $backup = new Backup($content['basename'], $this->getFiles($content['path']), $content['timestamp'], $content['timestamp']);
121
122 4
                $this->backups[$backup->getName()] = $backup;
123 2
            }
124 2
        }
125 4
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 4
    protected function getFiles($path)
131
    {
132 4
        $result = array();
133
134
        /**
135
         * @var \SplFileInfo $file
136
         */
137 4
        foreach ($this->flysystem->listContents($path, true) as $file) {
138
139 4
            if ($file['type'] == 'file') {
140 4
                $file = File::fromFlysystemMetadata($file, $path);
141 4
                $result[$file->getRelativePath()] = $file;
142 2
            }
143 2
        }
144
145 4
        return $result;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 4
    protected function removeEmptyDirectories($backupName)
152
    {
153
        /**
154
         * @var \SplFileInfo $dir
155
         */
156 4
        foreach ($this->flysystem->listContents($backupName) as $dir) {
157
158 4
            if ($dir['type'] != 'dir') {
159 4
                continue;
160
            }
161
162 2
            if (count($this->flysystem->listContents($dir['path'])) > 0) {
163 2
                $this->removeEmptyDirectories($dir['path']);
164 1
            } else {
165 2
                $this->flysystem->deleteDir($dir['path']);
166
            }
167 2
        }
168 4
    }
169
}
170