Completed
Push — master ( ccde76...68a462 )
by Sebastian
12s
created

Dropbox::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\Result;
9
use phpbu\App\Backup\Target;
10
use phpbu\App\Util\Arr;
11
use phpbu\App\Util\Str;
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 string
47
     */
48
    protected $path;
49
50
    /**
51
     * Dropbox api client
52
     *
53
     * @var DropboxApi
54
     */
55
    protected $client;
56
57
    /**
58
     * (non-PHPDoc)
59
     *
60
     * @see    \phpbu\App\Backup\Sync::setup()
61
     * @param  array $config
62
     * @throws \phpbu\App\Backup\Sync\Exception
63
     * @throws \phpbu\App\Exception
64
     */
65 5
    public function setup(array $config)
66
    {
67 5
        if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) {
68
            throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"');
69
        }
70 5
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
71 1
            throw new Exception('API access token is mandatory');
72
        }
73 4
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
74 1
            throw new Exception('dropbox path is mandatory');
75
        }
76
        // make sure the path contains leading and trailing slashes
77 3
        $this->path  = Str::withLeadingSlash(Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])));
78 3
        $this->token = $config['token'];
79
80 3
        $this->setUpClearable($config);
81 3
    }
82
83
    /**
84
     * (non-PHPDoc)
85
     *
86
     * @see    \phpbu\App\Backup\Sync::sync()
87
     * @param  \phpbu\App\Backup\Target $target
88
     * @param  \phpbu\App\Result        $result
89
     * @throws \phpbu\App\Backup\Sync\Exception
90
     */
91
    public function sync(Target $target, Result $result)
92
    {
93
        $sourcePath  = $target->getPathname();
94
        $dropboxPath = $this->path . $target->getFilename();
95
        if (!$this->client) {
96
            $this->connect();
97
        }
98
        try {
99
            $file = new DropboxFile($sourcePath);
100
            $meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]);
101
        } catch (\Exception $e) {
102
            throw new Exception($e->getMessage(), null, $e);
103
        }
104
        // run remote cleanup
105
        $this->cleanup($target, $result);
106
        $result->debug('upload: done  (' . $meta->getSize() . ')');
107
    }
108
109
    /**
110
     * Simulate the sync execution.
111
     *
112
     * @param \phpbu\App\Backup\Target $target
113
     * @param \phpbu\App\Result        $result
114
     */
115 2
    public function simulate(Target $target, Result $result)
116
    {
117 2
        $result->debug(
118 2
            'sync backup to dropbox' . PHP_EOL
119 2
            . '  token:    ********' . PHP_EOL
120 2
            . '  location: ' . $this->path . PHP_EOL
121
        );
122
123 2
        $this->simulateRemoteCleanup($target, $result);
124 2
    }
125
126
    /**
127
     * Creates collector for Dropbox
128
     *
129
     * @param \phpbu\App\Backup\Target $target
130
     * @return \phpbu\App\Backup\Collector
131
     */
132
    protected function createCollector(Target $target): Collector
133
    {
134
        return new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path);
135
    }
136
137
    /**
138
     * Create Dropbox api client
139
     */
140
    protected function connect()
141
    {
142
        $config       = new DropboxConfig("id", "secret", $this->token);
143
        $this->client = new DropboxApi($config);
144
    }
145
}
146