Completed
Pull Request — master (#140)
by
unknown
04:37
created

Dropbox::sync()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 6
nop 2
crap 12
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use Kunnu\Dropbox\DropboxApp as DropboxConfig;
5
use Kunnu\Dropbox\Dropbox as DropboxApi;
6
use Kunnu\Dropbox\DropboxFile;
7
use phpbu\App\Result;
8
use phpbu\App\Backup\Target;
9
use phpbu\App\Util\Arr;
10
use phpbu\App\Util\Str;
11
12
/**
13
 * Dropbox
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 1.1.1
22
 */
23
class Dropbox implements Simulator
24
{
25
    use Clearable;
26
27
    /**
28
     * API access token
29
     *
30
     * Goto https://www.dropbox.com/developers/apps
31
     * create your app
32
     *  - Dropbox api app
33
     *  - files and datastore
34
     *  - yes
35
     *  - provide some app name "my-dropbox-app"
36
     *  - generate access token to authenticate connection to your dropbox
37
     *
38
     * @var  string
39
     */
40
    protected $token;
41
42
    /**
43
     * Remote path
44
     *
45
     * @var string
46
     */
47
    protected $path;
48
49
    /**
50
     * Dropbox api client
51
     *
52
     * @var DropboxApi
53
     */
54
    protected $client;
55
56
    /**
57
     * (non-PHPDoc)
58
     *
59
     * @see    \phpbu\App\Backup\Sync::setup()
60
     * @param  array $config
61
     * @throws \phpbu\App\Backup\Sync\Exception
62
     * @throws \phpbu\App\Exception
63
     */
64 5
    public function setup(array $config)
65
    {
66 5
        if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) {
67
            throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"');
68
        }
69 5
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
70 1
            throw new Exception('API access token is mandatory');
71
        }
72 4
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
73 1
            throw new Exception('dropbox path is mandatory');
74
        }
75
        // make sure the path contains leading and trailing slashes
76 3
        $this->path  = Str::withLeadingSlash(Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])));
77 3
        $this->token = $config['token'];
78
79 3
        $this->setUpClearable($config);
80 3
    }
81
82
    /**
83
     * (non-PHPDoc)
84
     *
85
     * @see    \phpbu\App\Backup\Sync::sync()
86
     * @param  \phpbu\App\Backup\Target $target
87
     * @param  \phpbu\App\Result        $result
88
     * @throws \phpbu\App\Backup\Sync\Exception
89
     */
90
    public function sync(Target $target, Result $result)
91
    {
92
        $sourcePath  = $target->getPathname();
93
        $dropboxPath = $this->path . $target->getFilename();
94
        if (!$this->client) {
95
            $this->connect();
96
        }
97
        try {
98
            $file = new DropboxFile($sourcePath);
99
            $meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]);
100
        } catch (\Exception $e) {
101
            throw new Exception($e->getMessage(), null, $e);
102
        }
103
        // run remote cleanup
104
        $this->cleanup($target, $result);
105
        $result->debug('upload: done  (' . $meta->getSize() . ')');
106
    }
107
108
    /**
109
     * Simulate the sync execution.
110
     *
111
     * @param \phpbu\App\Backup\Target $target
112
     * @param \phpbu\App\Result        $result
113
     */
114 2
    public function simulate(Target $target, Result $result)
115
    {
116 2
        $result->debug(
117 2
            'sync backup to dropbox' . PHP_EOL
118 2
            . '  token:    ********' . PHP_EOL
119 2
            . '  location: ' . $this->path . PHP_EOL
120
        );
121
122 2
        $this->simulateRemoteCleanup($target, $result);
123 2
    }
124
125
    /**
126
     * Execute the remote clean up if needed
127
     *
128
     * @param \phpbu\App\Backup\Target $target
129
     * @param \phpbu\App\Result        $result
130
     */
131 View Code Duplication
    public function cleanup(Target $target, Result $result)
132
    {
133
        if (!$this->cleaner) {
134
            return;
135
        }
136
137
        $collector = new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path);
138
        $this->cleaner->cleanup($target, $collector, $result);
139
    }
140
141
    /**
142
     * Create Dropbox api client
143
     */
144
    protected function connect()
145
    {
146
        $config       = new DropboxConfig("id", "secret", $this->token);
147
        $this->client = new DropboxApi($config);
148
    }
149
}
150