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

AmazonS3::validateConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 5
Ratio 62.5 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 5
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Sync;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, phpbu\App\Backup\Sync\Sync.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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