Completed
Pull Request — master (#140)
by
unknown
03:00
created

SoftLayer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 3.94 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 52.5%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 5
loc 127
ccs 21
cts 40
cp 0.525
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 5 20 4
B sync() 0 24 2
A simulate() 0 11 1
A cleanup() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\File\Remote;
5
use phpbu\App\Result;
6
use phpbu\App\Backup\Sync;
7
use phpbu\App\Backup\Target;
8
use phpbu\App\Util\Arr;
9
use phpbu\App\Util\Str;
10
use ObjectStorage_Http_Client;
11
use ObjectStorage;
12
13
/**
14
 * SoftLayer  ObjectStorage Sync
15
 *
16
 * @package    phpbu
17
 * @subpackage Backup
18
 * @author     Petr Cervenka <[email protected]>
19
 * @author     Sebastian Feldmann <[email protected]>
20
 * @copyright  Sebastian Feldmann <[email protected]>
21
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
22
 * @link       http://phpbu.de/
23
 * @since      Class available since Release 1.1.6
24
 */
25
class SoftLayer implements Simulator
26
{
27
    /**
28
     * SoftLayer user
29
     *
30
     * @var  string
31
     */
32
    protected $user;
33
34
    /**
35
     * SoftLayer secret
36
     *
37
     * @var  string
38
     */
39
    protected $secret;
40
41
    /**
42
     * SoftLayer container
43
     *
44
     * @var string
45
     */
46
    protected $container;
47
48
    /**
49
     * SoftLayer host
50
     *
51
     * @var string
52
     */
53
    protected $host;
54
55
    /**
56
     * SoftLayer remote path
57
     *
58
     * @var string
59
     */
60
    protected $path;
61
62
    /**
63
     * (non-PHPDoc)
64
     *
65
     * @see    \phpbu\App\Backup\Sync::setup()
66
     * @param  array $config
67
     * @throws \phpbu\App\Backup\Sync\Exception
68
     */
69 7
    public function setup(array $config)
70
    {
71 7
        if (!class_exists('\\ObjectStorage')) {
72
            throw new Exception('SoftLayer SDK not loaded: use composer to install "softlayer/objectstorage"');
73
        }
74
        // check for mandatory options
75 7 View Code Duplication
        foreach (['user', 'secret', 'container', 'host', 'path'] as $option) {
76 7
            if (!Arr::isSetAndNotEmptyString($config, $option)) {
77 7
                throw new Exception('SoftLayer ' . $option . ' is mandatory');
78
            }
79
        }
80
81 2
        $this->user      = $config['user'];
82 2
        $this->secret    = $config['secret'];
83 2
        $this->container = $config['container'];
84 2
        $this->host      = $config['host'];
85 2
        $this->path      = Str::withLeadingSlash(
86 2
            Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']))
87
        );
88 2
    }
89
90
    /**
91
     * (non-PHPDoc)
92
     *
93
     * @see    \phpbu\App\Backup\Sync::sync()
94
     * @param  \phpbu\App\backup\Target $target
95
     * @param  \phpbu\App\Result        $result
96
     * @throws \phpbu\App\Backup\Sync\Exception
97
     */
98
    public function sync(Target $target, Result $result)
99
    {
100
        $sourcePath = $target->getPathname();
101
        $targetPath = $this->path . $target->getFilename();
102
103
        $options       = ['adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20];
104
        $objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options);
105
106
        $result->debug('softlayer source: ' . $sourcePath);
107
        $result->debug('softlayer target: ' . $targetPath);
108
109
        try {
110
            /** @var \ObjectStorage_Container $container */
111
            $container = $objectStorage->with($this->container . $targetPath)
112
                                       ->setLocalFile($sourcePath)
113
                                       ->setMeta('description', 'PHPBU Backup: ' . date('r', time()))
114
                                       ->setHeader('Content-Type', $target->getMimeType());
115
            $container->create();
116
        } catch (\Exception $e) {
117
            throw new Exception($e->getMessage(), null, $e);
118
        }
119
120
        $result->debug('upload: done');
121
    }
122
123
    /**
124
     * Simulate the sync execution.
125
     *
126
     * @param \phpbu\App\Backup\Target $target
127
     * @param \phpbu\App\Result        $result
128
     */
129 1
    public function simulate(Target $target, Result $result)
130
    {
131 1
        $result->debug(
132 1
            'sync backup to SoftLayer' . PHP_EOL
133 1
            . '  host:      ' . $this->host . PHP_EOL
134 1
            . '  user:      ' . $this->user . PHP_EOL
135 1
            . '  secret:     ********' . PHP_EOL
136 1
            . '  container: ' . $this->container . PHP_EOL
137 1
            . '  location:  ' . $this->path
138
        );
139 1
    }
140
141
    /**
142
     * Execute the remote clean up if needed
143
     *
144
     * @param \phpbu\App\Backup\Target $target
145
     * @param \phpbu\App\Result        $result
146
     */
147
    public function cleanup(Target $target, Result $result)
148
    {
149
        // TODO: Implement cleanup() method.
150
    }
151
}
152