Completed
Push — master ( 42678d...a4b858 )
by Sebastian
02:50
created

LaravelStorage::sync()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 6
1
<?php
2
namespace phpbu\Laravel\Backup\Sync;
3
4
use Illuminate\Support\Facades\Storage;
5
use phpbu\App\Backup\Sync\Simulator;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Exception;
8
use phpbu\App\Result;
9
10
/**
11
 * Class LaravelStorage
12
 *
13
 * Syncs a backup with the Laravel filesystem classes.
14
 *
15
 * @package    phpbu\Laravel
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    http://www.opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 */
21
class LaravelStorage implements Simulator
22
{
23
    /**
24
     * Laravel filesystem to sync to.
25
     *
26
     * @var string
27
     */
28
    private $filesystem;
29
30
    /**
31
     * Path to copy the backup.
32
     *
33
     * @var string
34
     */
35
    private $path;
36
37
    /**
38
     * Setup the source.
39
     *
40
     * @param  array $conf
41
     * @throws \phpbu\App\Exception
42
     */
43 3
    public function setup(array $conf = [])
44
    {
45 3
        if (empty($conf['filesystem'])) {
46 1
            throw new Exception('no filesystem configured');
47
        }
48 2
        $this->filesystem = $conf['filesystem'];
49 2
        $this->path       = !empty($conf['path']) ? $conf['path'] : '';
50 2
    }
51
52
    /**
53
     * Execute the Sync
54
     * Copy your backup to another location
55
     *
56
     * @param \phpbu\App\Backup\Target $target
57
     * @param \phpbu\App\Result $result
58
     */
59
    public function sync(Target $target, Result $result)
60
    {
61
        $result->debug('syncing backup');
62
        $storage = Storage::disk($this->filesystem);
63
64
        // if a path is specified make sure the directory exists
65
        if (!empty($this->path)) {
66
            $storage->makeDirectory($this->path);
67
        }
68
        $storage->getDriver()->writeStream(
69
            $this->path . '/' . $target->getFilename(),
70
            fopen($target->getPathname(), 'r+')
71
        );
72
    }
73
74
    /**
75
     * Simulate the sync execution.
76
     *
77
     * @param \phpbu\App\Backup\Target $target
78
     * @param \phpbu\App\Result        $result
79
     */
80 1
    public function simulate(Target $target, Result $result)
81
    {
82 1
        $result->debug(
83 1
            'sync backup to ' . $this->filesystem . PHP_EOL
84 1
            . ' target path: ' . $this->path
85
        );
86 1
    }
87
}
88