Completed
Pull Request — master (#140)
by
unknown
05:29
created

Dropbox::setup()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.024

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 3
cts 5
cp 0.6
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 5.024
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 5
    protected $client;
55
56 5
    /**
57
     * (non-PHPDoc)
58
     *
59 5
     * @see    \phpbu\App\Backup\Sync::setup()
60 1
     * @param  array $config
61
     * @throws \phpbu\App\Backup\Sync\Exception
62 4
     * @throws \phpbu\App\Exception
63 1
     */
64
    public function setup(array $config)
65
    {
66 3
        if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) {
67 3
            throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"');
68 3
        }
69
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
70
            throw new Exception('API access token is mandatory');
71
        }
72
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
73
            throw new Exception('dropbox path is mandatory');
74
        }
75
        // make sure the path contains leading and trailing slashes
76
        $this->path  = Str::withLeadingSlash(Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])));
77
        $this->token = $config['token'];
78
79
        $this->setUpClearable($config);
80
    }
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 2
            $meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]);
100
        } catch (\Exception $e) {
101 2
            throw new Exception($e->getMessage(), null, $e);
102 2
        }
103 2
        // run remote cleanup
104 2
        $this->cleanup($target, $result);
105
        $result->debug('upload: done  (' . $meta->getSize() . ')');
106 2
    }
107
108
    /**
109
     * Simulate the sync execution.
110
     *
111
     * @param \phpbu\App\Backup\Target $target
112
     * @param \phpbu\App\Result        $result
113
     */
114
    public function simulate(Target $target, Result $result)
115
    {
116
        $result->debug(
117
            'sync backup to dropbox' . PHP_EOL
118
            . '  token:    ********' . PHP_EOL
119
            . '  location: ' . $this->path . PHP_EOL
120
            . $this->getSimulateInfo()
121
        );
122
    }
123
124
    /**
125
     * Execute the remote clean up if needed
126
     *
127
     * @param \phpbu\App\Backup\Target $target
128
     * @param \phpbu\App\Result        $result
129
     */
130 View Code Duplication
    public function cleanup(Target $target, Result $result)
131
    {
132
        if (!$this->cleaner) {
133
            return;
134
        }
135
136
        $collector = new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path);
137
        $this->cleaner->cleanup($target, $collector, $result);
138
    }
139
140
    /**
141
     * Create Dropbox api client
142
     */
143
    protected function connect()
144
    {
145
        $config       = new DropboxConfig("id", "secret", $this->token);
146
        $this->client = new DropboxApi($config);
147
    }
148
}
149