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

Copycom::sync()   B

Complexity

Conditions 3
Paths 12

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 24
ccs 0
cts 16
cp 0
rs 8.9713
cc 3
eloc 15
nc 12
nop 2
crap 12
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use Barracuda\Copy\API as CopycomApi;
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
 * Copycom
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.2
21
 */
22
class Copycom implements Simulator
23
{
24
    /**
25
     * API access key
26
     *
27
     * @var  string
28
     */
29
    protected $appKey;
30
31
    /**
32
     * API access token
33
     *
34
     * @var  string
35
     */
36
    protected $appSecret;
37
38
    /**
39
     * API access key
40
     *
41
     * @var  string
42
     */
43
    protected $userKey;
44
45
    /**
46
     * API access token
47
     *
48
     * @var  string
49
     */
50
    protected $userSecret;
51
52
    /**
53
     * Remote path
54
     *
55
     * @var string
56
     */
57
    protected $path;
58
59
    /**
60
     * (non-PHPDoc)
61
     *
62
     * @see    \phpbu\App\Backup\Sync::setup()
63
     * @param  array $config
64
     * @throws \phpbu\App\Backup\Sync\Exception
65
     */
66 6 View Code Duplication
    public function setup(array $config)
67
    {
68 6
        if (!class_exists('\\Barracuda\\Copy\\API')) {
69
            throw new Exception('Copy api not loaded: use composer "barracuda/copy": "1.1.*" to install');
70
        }
71 6
        if (!Arr::isSetAndNotEmptyString($config, 'app.key')) {
72 1
            throw new Exception('API access key is mandatory');
73
        }
74 5
        if (!Arr::isSetAndNotEmptyString($config, 'app.secret')) {
75 1
            throw new Exception('API access secret is mandatory');
76
        }
77 4
        if (!Arr::isSetAndNotEmptyString($config, 'user.key')) {
78 1
            throw new Exception('User access key is mandatory');
79
        }
80 3
        if (!Arr::isSetAndNotEmptyString($config, 'user.secret')) {
81 1
            throw new Exception('User access secret is mandatory');
82
        }
83 2
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
84 1
            throw new Exception('copy.com path is mandatory');
85
        }
86 1
        $this->appKey     = $config['app.key'];
87 1
        $this->appSecret  = $config['app.secret'];
88 1
        $this->userKey    = $config['user.key'];
89 1
        $this->userSecret = $config['user.secret'];
90 1
        $this->path       = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
91 1
    }
92
93
    /**
94
     * (non-PHPDoc)
95
     *
96
     * @see    \phpbu\App\Backup\Sync::sync()
97
     * @param  \phpbu\App\Backup\Target $target
98
     * @param  \phpbu\App\Result        $result
99
     * @throws \phpbu\App\Backup\Sync\Exception
100
     */
101
    public function sync(Target $target, Result $result)
102
    {
103
        $sourcePath = $target->getPathname();
104
        $targetPath = $this->path . $target->getFilename();
105
106
        $copy = new CopycomApi($this->appKey, $this->appSecret, $this->userKey, $this->userSecret);
107
108
        try {
109
            // open a file to upload
110
            $fh = fopen($sourcePath, 'rb');
111
            // upload the file in 1MB chunks
112
            $parts = array();
113
            while ($data = fread($fh, 1024 * 1024)) {
114
                $part = $copy->sendData($data);
115
                array_push($parts, $part);
116
            }
117
            fclose($fh);
118
            // finalize the file
119
            $copy->createFile($targetPath, $parts);
120
        } catch (\Exception $e) {
121
            throw new Exception($e->getMessage(), null, $e);
122
        }
123
        $result->debug('upload: done');
124
    }
125
126
    /**
127
     * Simulate the sync execution.
128
     *
129
     * @param \phpbu\App\Backup\Target $target
130
     * @param \phpbu\App\Result        $result
131
     */
132
    public function simulate(Target $target, Result $result)
133
    {
134
        $result->debug(
135
            'sync backup to copy.com' . PHP_EOL
136
            . '  app.key:  ' . $this->appKey . PHP_EOL
137
            . '  user.key: ' . $this->userKey . PHP_EOL
138
            . '  secret:    ********' . PHP_EOL
139
            . '  location: ' . $this->path
140
141
        );
142
    }
143
}
144