Completed
Push — master ( 3eed6d...7635b2 )
by Sebastian
04:09 queued 01:07
created

SoftLayer::setup()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 5
Ratio 25 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 5
loc 20
c 0
b 0
f 0
ccs 12
cts 13
cp 0.9231
rs 9.2
cc 4
eloc 12
nc 4
nop 1
crap 4.0072
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Result;
5
use phpbu\App\Backup\Sync;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Util\Arr;
8
use phpbu\App\Util\Str;
9
use ObjectStorage_Http_Client;
10
use ObjectStorage;
11
12
/**
13
 * SoftLayer  ObjectStorage Sync
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Petr Cervenka <[email protected]>
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.6
23
 */
24
class SoftLayer implements Simulator
25
{
26
    /**
27
     * SoftLayer user
28
     *
29
     * @var  string
30
     */
31
    protected $user;
32
33
    /**
34
     * SoftLayer secret
35
     *
36
     * @var  string
37
     */
38
    protected $secret;
39
40
    /**
41
     * SoftLayer container
42
     *
43
     * @var string
44
     */
45
    protected $container;
46
47
    /**
48
     * SoftLayer host
49
     *
50
     * @var string
51
     */
52
    protected $host;
53
54
    /**
55
     * SoftLayer remote path
56
     *
57
     * @var string
58
     */
59
    protected $path;
60
61
    /**
62
     * (non-PHPDoc)
63
     *
64
     * @see    \phpbu\App\Backup\Sync::setup()
65
     * @param  array $config
66
     * @throws \phpbu\App\Backup\Sync\Exception
67
     */
68 7
    public function setup(array $config)
69
    {
70 7
        if (!class_exists('\\ObjectStorage')) {
71
            throw new Exception('SoftLayer SDK not loaded: use composer to install "softlayer/objectstorage"');
72
        }
73
        // check for mandatory options
74 7 View Code Duplication
        foreach (['user', 'secret', 'container', 'host', 'path'] as $option) {
75 7
            if (!Arr::isSetAndNotEmptyString($config, $option)) {
76 5
                throw new Exception('SoftLayer ' . $option . ' is mandatory');
77
            }
78
        }
79
80 2
        $this->user      = $config['user'];
81 2
        $this->secret    = $config['secret'];
82 2
        $this->container = $config['container'];
83 2
        $this->host      = $config['host'];
84 2
        $this->path      = Str::withLeadingSlash(
85 2
            Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']))
86
        );
87 2
    }
88
89
    /**
90
     * (non-PHPDoc)
91
     *
92
     * @see    \phpbu\App\Backup\Sync::sync()
93
     * @param  \phpbu\App\backup\Target $target
94
     * @param  \phpbu\App\Result        $result
95
     * @throws \phpbu\App\Backup\Sync\Exception
96
     */
97
    public function sync(Target $target, Result $result)
98
    {
99
        $sourcePath = $target->getPathname();
100
        $targetPath = $this->path . $target->getFilename();
101
102
        $options       = ['adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20];
103
        $objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options);
104
105
        $result->debug('softlayer source: ' . $sourcePath);
106
        $result->debug('softlayer target: ' . $targetPath);
107
108
        try {
109
            /** @var \ObjectStorage_Container $container */
110
            $container = $objectStorage->with($this->container . $targetPath)
111
                                       ->setLocalFile($sourcePath)
112
                                       ->setMeta('description', 'PHPBU Backup: ' . date('r', time()))
113
                                       ->setHeader('Content-Type', $target->getMimeType());
114
            $container->create();
115
        } catch (\Exception $e) {
116
            throw new Exception($e->getMessage(), null, $e);
117
        }
118
119
        $result->debug('upload: done');
120
    }
121
122
    /**
123
     * Simulate the sync execution.
124
     *
125
     * @param \phpbu\App\Backup\Target $target
126
     * @param \phpbu\App\Result        $result
127
     */
128 1
    public function simulate(Target $target, Result $result)
129
    {
130 1
        $result->debug(
131 1
            'sync backup to SoftLayer' . PHP_EOL
132 1
            . '  host:      ' . $this->host . PHP_EOL
133 1
            . '  user:      ' . $this->user . PHP_EOL
134 1
            . '  secret:     ********' . PHP_EOL
135 1
            . '  container: ' . $this->container . PHP_EOL
136 1
            . '  location:  ' . $this->path
137
        );
138 1
    }
139
}
140