Completed
Push — master ( 3eed6d...7635b2 )
by Sebastian
04:09 queued 01:07
created

Dropbox::setup()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4.016
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
    /**
26
     * API access token
27
     *
28
     * Goto https://www.dropbox.com/developers/apps
29
     * create your app
30
     *  - Dropbox api app
31
     *  - files and datastore
32
     *  - yes
33
     *  - provide some app name "my-dropbox-app"
34
     *  - generate access token to authenticate connection to your dropbox
35
     *
36
     * @var  string
37
     */
38
    protected $token;
39
40
    /**
41
     * Remote path
42
     *
43
     * @var string
44
     */
45
    protected $path;
46
47
    /**
48
     * (non-PHPDoc)
49
     *
50
     * @see    \phpbu\App\Backup\Sync::setup()
51
     * @param  array $config
52
     * @throws \phpbu\App\Backup\Sync\Exception
53
     */
54 4
    public function setup(array $config)
55
    {
56 4
        if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) {
57
            throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"');
58
        }
59 4
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
60 1
            throw new Exception('API access token is mandatory');
61
        }
62 3
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
63 1
            throw new Exception('dropbox path is mandatory');
64
        }
65 2
        $this->token = $config['token'];
66 2
        $this->path  = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
67 2
    }
68
69
    /**
70
     * (non-PHPDoc)
71
     *
72
     * @see    \phpbu\App\Backup\Sync::sync()
73
     * @param  \phpbu\App\Backup\Target $target
74
     * @param  \phpbu\App\Result        $result
75
     * @throws \phpbu\App\Backup\Sync\Exception
76
     */
77
    public function sync(Target $target, Result $result)
78
    {
79
        $sourcePath  = $target->getPathname();
80
        $dropboxPath = $this->path . $target->getFilename();
81
        $config      = new DropboxConfig("id", "secret", $this->token);
82
        $client      = new DropboxApi($config);
83
        try {
84
            $file = new DropboxFile($sourcePath);
85
            $meta = $client->upload($file, $dropboxPath, ['autorename' => true]);
86
        } catch (\Exception $e) {
87
            throw new Exception($e->getMessage(), null, $e);
88
        }
89
        $result->debug('upload: done  (' . $meta->getSize() . ')');
90
    }
91
92
    /**
93
     * Simulate the sync execution.
94
     *
95
     * @param \phpbu\App\Backup\Target $target
96
     * @param \phpbu\App\Result        $result
97
     */
98 1
    public function simulate(Target $target, Result $result)
99
    {
100 1
        $result->debug(
101 1
            'sync backup to dropbox' . PHP_EOL
102 1
            . '  token:    ********' . PHP_EOL
103 1
            . '  location: ' . $this->path
104
        );
105 1
    }
106
}
107