Completed
Push — master ( d6a771...3f2a2f )
by Sebastian
03:29
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 20
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 6
    public function setup(array $config)
69
    {
70 6
        if (!class_exists('\\ObjectStorage')) {
71
            throw new Exception('SoftLayer SDK not loaded: use composer to install "softlayer/objectstorage"');
72
        }
73 6
        // check for mandatory options
74 1 View Code Duplication
        foreach (['user', 'secret', 'container', 'host', 'path'] as $option) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            if (!Arr::isSetAndNotEmptyString($config, $option)) {
76 5
                throw new Exception('SoftLayer ' . $option . ' is mandatory');
77 1
            }
78
        }
79 4
80 1
        $this->user      = $config['user'];
81
        $this->secret    = $config['secret'];
82 3
        $this->container = $config['container'];
83 1
        $this->host      = $config['host'];
84
        $this->path      = Str::withLeadingSlash(
85 2
            Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']))
86 1
        );
87
    }
88 1
89 1
    /**
90 1
     * (non-PHPDoc)
91 1
     *
92 1
     * @see    \phpbu\App\Backup\Sync::sync()
93 1
     * @param  \phpbu\App\backup\Target $target
94 1
     * @param  \phpbu\App\Result        $result
95 1
     * @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
    public function simulate(Target $target, Result $result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $result->debug(
131
            'sync backup to SoftLayer' . PHP_EOL
132
            . '  host:      ' . $this->host . PHP_EOL
133
            . '  user:      ' . $this->user . PHP_EOL
134
            . '  secret:     ********' . PHP_EOL
135
            . '  container: ' . $this->container . PHP_EOL
136
            . '  location:  ' . $this->path
137
        );
138
    }
139
}
140