Completed
Pull Request — master (#145)
by Vitaly
02:37
created

SoftLayer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 52.78%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 112
ccs 19
cts 36
cp 0.5278
rs 10
c 0
b 0
f 0

3 Methods

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