Completed
Push — master ( a2c400...8a7993 )
by Nikola
09:09
created

LocalDestination::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Destination;
14
15
use RunOpenCode\Backup\Backup\Backup;
16
use RunOpenCode\Backup\Backup\File;
17
use RunOpenCode\Backup\Contract\BackupInterface;
18
use RunOpenCode\Backup\Contract\FileInterface;
19
use RunOpenCode\Backup\Exception\DestinationException;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\Finder\Finder;
22
23
/**
24
 * Class LocalDestination
25
 *
26
 * Stream destination is local, mountable, destination.
27
 *
28
 * @package RunOpenCode\Backup\Destination
29
 */
30
class LocalDestination extends BaseDestination
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $directory;
36
37
    /**
38
     * @var Filesystem
39
     */
40
    private $filesystem;
41
42
    public function __construct($directory, Filesystem $filesystem = null)
43
    {
44
        $this->directory = rtrim($directory, '/\\');
45
        $this->filesystem = is_null($filesystem) ? new Filesystem() : $filesystem;
46
47
        if (!$this->filesystem->exists($this->directory)) {
48
49
            $this->filesystem->mkdir($this->directory);
50
51
        } elseif (!is_dir($this->directory)) {
52
            throw new \RuntimeException(sprintf('Provided location "%s" is not directory.', $this->directory));
53
        } elseif (!is_writable($this->directory)) {
54
            throw new \RuntimeException(sprintf('Provided location "%s" is not writeable.', $this->directory));
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 View Code Duplication
    protected function getDirectoryForBackup(BackupInterface $backup)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        $backupDirectory = $this->directory . DIRECTORY_SEPARATOR . $backup->getName();
64
65
        try {
66
            $this->filesystem->mkdir($backupDirectory);
67
        } catch (\Exception $e) {
68
            throw new DestinationException(sprintf('Unable to create backup directory "%s" for backup "%s" in local destination.', $backupDirectory, $backup->getName()));
69
        }
70
71
        return $backupDirectory;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function getFiles($path)
78
    {
79
        $result = array();
80
81
        foreach (Finder::create()->in($path)->files() as $file) {
82
            $file = File::fromSplFileInfo($file, $path);
83
            $result[$file->getRelativePath()] = $file;
84
        }
85
86
        return $result;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function pushFile($backupDirectory, FileInterface $backupFile)
93
    {
94
        try {
95
            $this->filesystem->copy($backupFile->getPath(), sprintf('%s%s%s', $backupDirectory, DIRECTORY_SEPARATOR, $backupFile->getRelativePath()));
96
        } catch (\Exception $e) {
97
            throw new DestinationException(sprintf('Unable to backup file "%s" to destination "%s".', $backupFile->getPath(), $this->directory), 0, $e);
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function load()
105
    {
106
        $this->backups = array();
107
108
        /**
109
         * @var \SplFileInfo $backupDirectory
110
         */
111
        foreach (Finder::create()->in($this->directory)->depth(0)->directories()->sortByModifiedTime() as $backupDirectory) {
112
113
            $backup = new Backup($backupDirectory->getBasename(), $this->getFiles($backupDirectory->getPathname()), 0, $backupDirectory->getCTime(), $backupDirectory->getMTime());
114
115
            $this->backups[$backup->getName()] = $backup;
116
        }
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    protected function doDelete($name)
123
    {
124
        try {
125
            $this->filesystem->remove(sprintf('%s%s%s', rtrim($this->directory, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, $name));
126
        } catch (\Exception $e) {
127
            throw new DestinationException(sprintf('Unable to remove backup "%s" from stream destination "%s".', $name, $this->directory), 0, $e);
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 View Code Duplication
    protected function removeFile($backupDirectory, FileInterface $backupFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
135
    {
136
        $path = $backupDirectory . DIRECTORY_SEPARATOR . $backupFile->getRelativePath();
137
138
        try {
139
            $this->filesystem->remove($path);
140
        } catch (\Exception $e) {
141
            throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e);
142
        }
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected function removeEmptyDirectories($backupDirectory)
149
    {
150
        /**
151
         * @var \SplFileInfo $dir
152
         */
153
        foreach (Finder::create()->directories()->in($backupDirectory)->depth(0) as $dir) {
154
155
            if (Finder::create()->files()->in($dir->getPathname())->count() > 0) {
156
                $this->removeEmptyDirectories($dir->getPathname());
157
            } else {
158
                $this->filesystem->remove($dir->getPathname());
159
            }
160
        }
161
    }
162
}
163