Completed
Push — master ( fc422d...111ee1 )
by Sebastian
09:14
created

Dropbox   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 29.03%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 84
ccs 9
cts 31
cp 0.2903
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 14 4
A sync() 0 14 2
A simulate() 0 8 1
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 3
     */
54
    public function setup(array $config)
55 3
    {
56
        if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) {
57
            throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"');
58 3
        }
59 1
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
60
            throw new Exception('API access token is mandatory');
61 2
        }
62 1
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
63
            throw new Exception('dropbox path is mandatory');
64 1
        }
65 1
        $this->token = $config['token'];
66 1
        $this->path  = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
67
    }
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
    public function simulate(Target $target, Result $result)
99
    {
100
        $result->debug(
101
            'sync backup to dropbox' . PHP_EOL
102
            . '  token:    ********' . PHP_EOL
103
            . '  location: ' . $this->path
104
        );
105
    }
106
}
107