Completed
Pull Request — master (#145)
by Vitaly
02:41
created

AmazonS3::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 15
cp 0.9333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2.0011
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
     * AWS remote raw path / object key
65
     *
66
     * @var string
67
     */
68
    protected $pathRaw;
69
70
    /**
71
     * AWS acl
72
     * 'private' by default
73
     *
74
     * @var string
75
     */
76
    protected $acl;
77
78
    /**
79
     * Use multi part config
80
     *
81
     * @var boolean
82
     */
83
    protected $multiPartUpload;
84
85
    /**
86
     * Min multi part upload size
87
     *
88
     * @var int
89
     */
90
    protected $minMultiPartUploadSize = 5242880;
91
92
    /**
93
     * Max stream upload size
94
     *
95
     * @var int
96
     */
97
    protected $maxStreamUploadSize = 104857600;
98
99
    /**
100
     * Configure the sync.
101
     *
102
     * @see    \phpbu\App\Backup\Sync::setup()
103
     * @param  array $config
104
     * @throws \phpbu\App\Backup\Sync\Exception
105
     */
106 9
    public function setup(array $config)
107
    {
108 9
        if (!class_exists('\\Aws\\S3\\S3Client')) {
109
            throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"');
110
        }
111
112
        // check for mandatory options
113 9
        $this->validateConfig($config, ['key', 'secret', 'bucket', 'region', 'path']);
114
115 4
        $this->time            = time();
0 ignored issues
show
Bug introduced by
The property time does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116 4
        $this->key             = $config['key'];
117 4
        $this->secret          = $config['secret'];
118 4
        $this->bucket          = $config['bucket'];
119 4
        $this->bucketTTL       = Util\Arr::getValue($config, 'bucketTTL');
120 4
        $this->region          = $config['region'];
121 4
        $this->path            = Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path'], $this->time));
122 4
        $this->pathRaw         = $config['path'];
123 4
        $this->acl             = Util\Arr::getValue($config, 'acl', 'private');
124 4
        $this->multiPartUpload = Util\Str::toBoolean(Util\Arr::getValue($config, 'useMultiPartUpload'), false);
125 4
    }
126
127
    /**
128
     * Make sure all mandatory keys are present in given config.
129
     *
130
     * @param  array    $config
131
     * @param  string[] $keys
132
     * @throws Exception
133
     */
134 9
    protected function validateConfig(array $config, array $keys)
135
    {
136 9
        foreach ($keys as $option) {
137 9
            if (!Util\Arr::isSetAndNotEmptyString($config, $option)) {
138 9
                throw new Exception($option . ' is mandatory');
139
            }
140
        }
141 4
    }
142
143
    /**
144
     * Simulate the sync execution.
145
     *
146
     * @param \phpbu\App\Backup\Target $target
147
     * @param \phpbu\App\Result        $result
148
     */
149 1
    public function simulate(Target $target, Result $result)
150
    {
151 1
        $result->debug(
152 1
            'sync backup to Amazon S3' . PHP_EOL
153 1
            . '  region:   ' . $this->region . PHP_EOL
154 1
            . '  key:      ' . $this->key . PHP_EOL
155 1
            . '  secret:    ********' . PHP_EOL
156 1
            . '  location: ' . $this->bucket
157
        );
158 1
    }
159
160
    /**
161
     * Should multi part upload be used.
162
     *
163
     * @param  \phpbu\App\Backup\Target $target
164
     * @return bool
165
     * @throws \phpbu\App\Exception
166
     */
167
    protected function useMultiPartUpload(Target $target)
168
    {
169
        // files bigger 5GB have to be uploaded via multi part
170
        // files uploaded with multi part upload has to be at least 5MB
171
        return (
172
            $target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload
173
        ) && $target->getSize() > $this->minMultiPartUploadSize;
174
    }
175
}
176