Completed
Pull Request — master (#145)
by Vitaly
10:03
created

Dropbox::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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