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

SoftLayer::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
ccs 0
cts 0
cp 0
cc 1
eloc 8
nc 1
nop 2
crap 2
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 View Code Duplication
    public function setup(array $config)
69
    {
70 6
        if (!class_exists('\\ObjectStorage')) {
71
            throw new Exception('SoftLayer SDK not loaded: use composer "softlayer/objectstorage": "dev-master" to install');
72
        }
73 6
        if (!Arr::isSetAndNotEmptyString($config, 'user')) {
74 1
            throw new Exception('SoftLayer user is mandatory');
75
        }
76 5
        if (!Arr::isSetAndNotEmptyString($config, 'secret')) {
77 1
            throw new Exception('SoftLayer password is mandatory');
78
        }
79 4
        if (!Arr::isSetAndNotEmptyString($config, 'container')) {
80 1
            throw new Exception('SoftLayer container name is mandatory');
81
        }
82 3
        if (!Arr::isSetAndNotEmptyString($config, 'host')) {
83 1
            throw new Exception('SoftLayer host is mandatory');
84
        }
85 2
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
86 1
            throw new Exception('SoftLayer path is mandatory');
87
        }
88 1
        $this->user      = $config['user'];
89 1
        $this->secret    = $config['secret'];
90 1
        $this->container = $config['container'];
91 1
        $this->host      = $config['host'];
92 1
        $this->path      = Str::withLeadingSlash(
93 1
            Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']))
94 1
        );
95 1
    }
96
97
    /**
98
     * (non-PHPDoc)
99
     *
100
     * @see    \phpbu\App\Backup\Sync::sync()
101
     * @param  \phpbu\App\backup\Target $target
102
     * @param  \phpbu\App\Result        $result
103
     * @throws \phpbu\App\Backup\Sync\Exception
104
     */
105
    public function sync(Target $target, Result $result)
106
    {
107
        $sourcePath = $target->getPathname();
108
        $targetPath = $this->path . $target->getFilename();
109
110
        $options       = array('adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20);
111
        $objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options);
112
113
        $result->debug('softlayer source: ' . $sourcePath);
114
        $result->debug('softlayer target: ' . $targetPath);
115
116
        try {
117
            /** @var \ObjectStorage_Container $container */
118
            $container = $objectStorage->with($this->container . $targetPath)
119
                                       ->setLocalFile($sourcePath)
120
                                       ->setMeta('description', 'PHPBU Backup: ' . date('r', time()))
121
                                       ->setHeader('Content-Type', $target->getMimeType());
122
            $container->create();
123
        } catch (\Exception $e) {
124
            throw new Exception($e->getMessage(), null, $e);
125
        }
126
127
        $result->debug('upload: done');
128
    }
129
130
    /**
131
     * Simulate the sync execution.
132
     *
133
     * @param \phpbu\App\Backup\Target $target
134
     * @param \phpbu\App\Result        $result
135
     */
136 View Code Duplication
    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...
137
    {
138
        $result->debug(
139
            'sync backup to SoftLayer' . PHP_EOL
140
            . '  host:      ' . $this->host . PHP_EOL
141
            . '  user:      ' . $this->user . PHP_EOL
142
            . '  secret:     ********' . PHP_EOL
143
            . '  conatiner: ' . $this->container . PHP_EOL
144
            . '  location:  ' . $this->path
145
        );
146
    }
147
}
148