Completed
Push — master ( a95984...72cf29 )
by Sebastian
15s queued 10s
created

AmazonS3   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 164
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 20 2
A validateConfig() 0 8 3
A simulate() 0 10 1
A useMultiPartUpload() 0 8 3
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
8
/**
9
 * Amazon S3 Sync base class
10
 *
11
 * @package    phpbu
12
 * @subpackage Backup
13
 * @author     Sebastian Feldmann <[email protected]>
14
 * @copyright  Sebastian Feldmann <[email protected]>
15
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
16
 * @link       http://phpbu.de/
17
 * @since      Class available since Release 3.0.0
18
 */
19
abstract class AmazonS3 implements Simulator
20
{
21
    /**
22
     * AWS key
23
     *
24
     * @var  string
25
     */
26
    protected $key;
27
28
    /**
29
     * AWS secret
30
     *
31
     * @var  string
32
     */
33
    protected $secret;
34
35
    /**
36
     * AWS S3 bucket
37
     *
38
     * @var string
39
     */
40
    protected $bucket;
41
42
    /**
43
     * TTL for all items in this bucket.
44
     *
45
     * @var int
46
     */
47
    protected $bucketTTL;
48
49
    /**
50
     * AWS S3 region
51
     *
52
     * @var string
53
     */
54
    protected $region;
55
56
    /**
57
     * AWS remote path / object key
58
     *
59
     * @var string
60
     */
61
    protected $path;
62
63
    /**
64
     * Unix timestamp of generating path from placeholder.
65
     *
66
     * @var int
67
     */
68
    protected $time;
69
70
    /**
71
     * AWS remote raw path / object key
72
     *
73
     * @var string
74
     */
75
    protected $pathRaw;
76
77
    /**
78
     * AWS acl
79
     * 'private' by default
80
     *
81
     * @var string
82
     */
83
    protected $acl;
84
85
    /**
86
     * Use multi part config
87
     *
88
     * @var boolean
89
     */
90
    protected $multiPartUpload;
91
92
    /**
93
     * Min multi part upload size
94
     *
95
     * @var int
96
     */
97
    protected $minMultiPartUploadSize = 5242880;
98
99
    /**
100
     * Max stream upload size
101
     *
102
     * @var int
103
     */
104
    protected $maxStreamUploadSize = 104857600;
105
106
    /**
107
     * Configure the sync.
108
     *
109
     * @see    \phpbu\App\Backup\Sync::setup()
110
     * @param  array $config
111
     * @throws \phpbu\App\Backup\Sync\Exception
112
     */
113 9
    public function setup(array $config)
114
    {
115 9
        if (!class_exists('\\Aws\\S3\\S3Client')) {
116
            throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"');
117
        }
118
119
        // check for mandatory options
120 9
        $this->validateConfig($config, ['key', 'secret', 'bucket', 'region', 'path']);
121
122 4
        $this->time            = time();
123 4
        $this->key             = $config['key'];
124 4
        $this->secret          = $config['secret'];
125 4
        $this->bucket          = $config['bucket'];
126 4
        $this->bucketTTL       = Util\Arr::getValue($config, 'bucketTTL');
127 4
        $this->region          = $config['region'];
128 4
        $this->path            = Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path'], $this->time));
129 4
        $this->pathRaw         = $config['path'];
130 4
        $this->acl             = Util\Arr::getValue($config, 'acl', 'private');
131 4
        $this->multiPartUpload = Util\Str::toBoolean(Util\Arr::getValue($config, 'useMultiPartUpload'), false);
132 4
    }
133
134
    /**
135
     * Make sure all mandatory keys are present in given config.
136
     *
137
     * @param  array    $config
138
     * @param  string[] $keys
139
     * @throws Exception
140
     */
141 9
    protected function validateConfig(array $config, array $keys)
142
    {
143 9
        foreach ($keys as $option) {
144 9
            if (!Util\Arr::isSetAndNotEmptyString($config, $option)) {
145 9
                throw new Exception($option . ' is mandatory');
146
            }
147
        }
148 4
    }
149
150
    /**
151
     * Simulate the sync execution.
152
     *
153
     * @param \phpbu\App\Backup\Target $target
154
     * @param \phpbu\App\Result        $result
155
     */
156 1
    public function simulate(Target $target, Result $result)
157
    {
158 1
        $result->debug(
159 1
            'sync backup to Amazon S3' . PHP_EOL
160 1
            . '  region:   ' . $this->region . PHP_EOL
161 1
            . '  key:      ' . $this->key . PHP_EOL
162 1
            . '  secret:    ********' . PHP_EOL
163 1
            . '  location: ' . $this->bucket
164
        );
165 1
    }
166
167
    /**
168
     * Should multi part upload be used.
169
     *
170
     * @param  \phpbu\App\Backup\Target $target
171
     * @return bool
172
     * @throws \phpbu\App\Exception
173
     */
174
    protected function useMultiPartUpload(Target $target)
175
    {
176
        // files bigger 5GB have to be uploaded via multi part
177
        // files uploaded with multi part upload has to be at least 5MB
178
        return (
179
            $target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload
180
        ) && $target->getSize() > $this->minMultiPartUploadSize;
181
    }
182
}
183