SoftLayer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 57.5%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 125
ccs 23
cts 40
cp 0.575
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A simulate() 0 9 1
A setup() 0 14 2
A validateConfig() 0 5 3
A sync() 0 23 2
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Result;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Util;
7
use ObjectStorage_Http_Client;
8
use ObjectStorage;
9
10
/**
11
 * SoftLayer  ObjectStorage Sync
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Petr Cervenka <[email protected]>
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.6
21
 */
22
class SoftLayer implements Simulator
23
{
24
    /**
25
     * SoftLayer user
26
     *
27
     * @var  string
28
     */
29
    protected $user;
30
31
    /**
32
     * SoftLayer secret
33
     *
34
     * @var  string
35
     */
36
    protected $secret;
37
38
    /**
39
     * SoftLayer container
40
     *
41
     * @var string
42
     */
43
    protected $container;
44
45
    /**
46
     * SoftLayer host
47
     *
48
     * @var string
49
     */
50
    protected $host;
51
52
    /**
53
     * SoftLayer 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 7
    public function setup(array $config)
67
    {
68 7
        if (!class_exists('\\ObjectStorage')) {
69
            throw new Exception('SoftLayer SDK not loaded: use composer to install "softlayer/objectstorage"');
70
        }
71
        // check for mandatory options
72 7
        $this->validateConfig($config, ['user', 'secret', 'container', 'host', 'path']);
73
74 2
        $this->user      = $config['user'];
75 2
        $this->secret    = $config['secret'];
76 2
        $this->container = $config['container'];
77 2
        $this->host      = $config['host'];
78 2
        $this->path      = Util\Path::withLeadingSlash(
79 2
            Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path']))
80
        );
81 2
    }
82
83
    /**
84
     * Make sure all mandatory keys are present in given config.
85
     *
86
     * @param  array    $config
87
     * @param  string[] $keys
88
     * @throws Exception
89
     */
90 7
    protected function validateConfig(array $config, array $keys)
91
    {
92 7
        foreach ($keys as $option) {
93 7
            if (!Util\Arr::isSetAndNotEmptyString($config, $option)) {
94 7
                throw new Exception($option . ' is mandatory');
95
            }
96
        }
97 2
    }
98
99
    /**
100
     * (non-PHPDoc)
101
     *
102
     * @see    \phpbu\App\Backup\Sync::sync()
103
     * @param  \phpbu\App\Backup\Target $target
104
     * @param  \phpbu\App\Result        $result
105
     * @throws \phpbu\App\Backup\Sync\Exception
106
     */
107
    public function sync(Target $target, Result $result)
108
    {
109
        $sourcePath = $target->getPathname();
110
        $targetPath = $this->path . $target->getFilename();
111
112
        $options       = ['adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20];
113
        $objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options);
114
115
        $result->debug('softlayer source: ' . $sourcePath);
116
        $result->debug('softlayer target: ' . $targetPath);
117
118
        try {
119
            /** @var \ObjectStorage_Container $container */
120
            $container = $objectStorage->with($this->container . $targetPath)
121
                                       ->setLocalFile($sourcePath)
0 ignored issues
show
Bug introduced by
The method setLocalFile() does not exist on ObjectStorage_Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
                                       ->/** @scrutinizer ignore-call */ setLocalFile($sourcePath)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
                                       ->setMeta('description', 'PHPBU Backup: ' . date('r', time()))
123
                                       ->setHeader('Content-Type', $target->getMimeType());
124
            $container->create();
125
        } catch (\Exception $e) {
126
            throw new Exception($e->getMessage(), null, $e);
127
        }
128
129
        $result->debug('upload: done');
130
    }
131
132
    /**
133
     * Simulate the sync execution.
134
     *
135
     * @param \phpbu\App\Backup\Target $target
136
     * @param \phpbu\App\Result        $result
137
     */
138 1
    public function simulate(Target $target, Result $result)
139
    {
140 1
        $result->debug(
141 1
            'sync backup to SoftLayer' . PHP_EOL
142 1
            . '  host:      ' . $this->host . PHP_EOL
143 1
            . '  user:      ' . $this->user . PHP_EOL
144 1
            . '  secret:     ********' . PHP_EOL
145 1
            . '  container: ' . $this->container . PHP_EOL
146 1
            . '  location:  ' . $this->path
147
        );
148 1
    }
149
}
150