Completed
Push — master ( 236d2e...a95984 )
by Sebastian
03:12
created

Dropbox::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 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\Backup\Collector;
8
use phpbu\App\Result;
9
use phpbu\App\Backup\Target;
10
use phpbu\App\Util;
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 (!Util\Arr::isSetAndNotEmptyString($config, 'token')) {
70 1
            throw new Exception('API access token is mandatory');
71
        }
72 4
        if (!Util\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->token = $config['token'];
77 3
        $this->path  = Util\Path::withLeadingSlash(
78 3
            Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path']))
79
        );
80
81 3
        $this->setUpClearable($config);
82 3
    }
83
84
    /**
85
     * (non-PHPDoc)
86
     *
87
     * @see    \phpbu\App\Backup\Sync::sync()
88
     * @param  \phpbu\App\Backup\Target $target
89
     * @param  \phpbu\App\Result        $result
90
     * @throws \phpbu\App\Backup\Sync\Exception
91
     */
92
    public function sync(Target $target, Result $result)
93
    {
94
        $sourcePath  = $target->getPathname();
95
        $dropboxPath = $this->path . $target->getFilename();
96
        if (!$this->client) {
97
            $this->connect();
98
        }
99
        try {
100
            $file = new DropboxFile($sourcePath);
101
            $meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]);
102
        } catch (\Exception $e) {
103
            throw new Exception($e->getMessage(), null, $e);
104
        }
105
        // run remote cleanup
106
        $this->cleanup($target, $result);
107
        $result->debug('upload: done  (' . $meta->getSize() . ')');
108
    }
109
110
    /**
111
     * Simulate the sync execution.
112
     *
113
     * @param \phpbu\App\Backup\Target $target
114
     * @param \phpbu\App\Result        $result
115
     */
116 2
    public function simulate(Target $target, Result $result)
117
    {
118 2
        $result->debug(
119 2
            'sync backup to dropbox' . PHP_EOL
120 2
            . '  token:    ********' . PHP_EOL
121 2
            . '  location: ' . $this->path . PHP_EOL
122
        );
123
124 2
        $this->simulateRemoteCleanup($target, $result);
125 2
    }
126
127
    /**
128
     * Creates collector for Dropbox
129
     *
130
     * @param \phpbu\App\Backup\Target $target
131
     * @return \phpbu\App\Backup\Collector
132
     */
133
    protected function createCollector(Target $target): Collector
134
    {
135
        return new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path);
136
    }
137
138
    /**
139
     * Create Dropbox api client
140
     */
141
    protected function connect()
142
    {
143
        $config       = new DropboxConfig("id", "secret", $this->token);
144
        $this->client = new DropboxApi($config);
145
    }
146
}
147