Completed
Push — master ( 4f1d49...8186c0 )
by Sebastian
07:15
created

Dropbox::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 0
cp 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use Dropbox as DropboxApi;
5
use phpbu\App\Result;
6
use phpbu\App\Backup\Sync;
7
use phpbu\App\Backup\Target;
8
use phpbu\App\Util\Arr;
9
use phpbu\App\Util\Str;
10
11
/**
12
 * Dropbox
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 1.1.1
21
 */
22
class Dropbox implements Simulator
23
{
24
    /**
25
     * API access token
26
     *
27
     * Goto https://www.dropbox.com/developers/apps
28
     * create your app
29
     *  - Dropbox api app
30
     *  - files and datastore
31
     *  - yes
32
     *  - provide some app name "my-dropbox-app"
33
     *  - generate access token to authenticate connection to your dropbox
34
     *
35
     * @var  string
36
     */
37
    protected $token;
38
39
    /**
40
     * Remote path
41
     *
42
     * @var string
43
     */
44
    protected $path;
45
46
    /**
47
     * (non-PHPDoc)
48
     *
49
     * @see    \phpbu\App\Backup\Sync::setup()
50
     * @param  array $config
51
     * @throws \phpbu\App\Backup\Sync\Exception
52
     */
53 3
    public function setup(array $config)
54
    {
55 3
        if (!class_exists('\\Dropbox\\Client')) {
56
            throw new Exception('Dropbox sdk not loaded: use composer "dropbox/dropbox-sdk": "1.1.*" to install');
57
        }
58 3
        if (!Arr::isSetAndNotEmptyString($config, 'token')) {
59 1
            throw new Exception('API access token is mandatory');
60
        }
61 2
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
62 1
            throw new Exception('dropbox path is mandatory');
63
        }
64 1
        $this->token = $config['token'];
65 1
        $this->path  = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
66 1
    }
67
68
    /**
69
     * (non-PHPDoc)
70
     *
71
     * @see    \phpbu\App\Backup\Sync::sync()
72
     * @param  \phpbu\App\Backup\Target $target
73
     * @param  \phpbu\App\Result        $result
74
     * @throws \phpbu\App\Backup\Sync\Exception
75
     */
76
    public function sync(Target $target, Result $result)
77
    {
78
        $sourcePath  = $target->getPathname();
79
        $dropboxPath = $this->path . $target->getFilename();
80
        $client      = new DropboxApi\Client($this->token, "phpbu/1.1.0");
81
        $pathError   = DropboxApi\Path::findErrorNonRoot($dropboxPath);
82
83
        if (substr(__FILE__, 0, 7) == 'phar://') {
84
            DropboxApi\RootCertificates::useExternalPaths();
85
        }
86
87
        if ($pathError !== null) {
88
            throw new Exception(sprintf('Invalid \'dropbox-path\': %s', $pathError));
89
        }
90
91
        $size = null;
92
        if (stream_is_local($sourcePath)) {
93
            $size = filesize($sourcePath);
94
        }
95
96
        try {
97
            $fp  = fopen($sourcePath, 'rb');
98
            $res = $client->uploadFile($dropboxPath, DropboxApi\WriteMode::add(), $fp, $size);
99
            fclose($fp);
100
        } catch (\Exception $e) {
101
            throw new Exception($e->getMessage(), null, $e);
102
        }
103
        $result->debug('upload: done  (' . $res['size'] . ')');
104
    }
105
106
    /**
107
     * Simulate the sync execution.
108
     *
109
     * @param \phpbu\App\Backup\Target $target
110
     * @param \phpbu\App\Result        $result
111
     */
112
    public function simulate(Target $target, Result $result)
113
    {
114
        $result->debug(
115
            'sync backup to dropbox' . PHP_EOL
116
            . '  token:    ********' . PHP_EOL
117
            . '  location: ' . $this->path
118
        );
119
    }
120
}
121