Completed
Pull Request — master (#140)
by
unknown
10:09 queued 03:44
created

AmazonS3::sync()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
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\Arr;
7
use phpbu\App\Util\Str;
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 implements Simulator
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 acl
66
     * 'private' by default
67
     *
68
     * @var string
69
     */
70
    protected $acl;
71
72
    /**
73
     * Use multi part config
74
     *
75
     * @var boolean
76
     */
77
    protected $multiPartUpload;
78
79
    /**
80
     * Min multi part upload size
81
     *
82
     * @var int
83
     */
84
    protected $minMultiPartUploadSize = 5242880;
85
86
    /**
87
     * Max stream upload size
88
     *
89
     * @var int
90
     */
91
    protected $maxStreamUploadSize = 104857600;
92
93
    /**
94
     * Configure the sync.
95
     *
96
     * @see    \phpbu\App\Backup\Sync::setup()
97
     * @param  array $config
98
     * @throws \phpbu\App\Backup\Sync\Exception
99
     */
100 9
    public function setup(array $config)
101
    {
102 9
        if (!class_exists('\\Aws\\S3\\S3Client')) {
103
            throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"');
104
        }
105
106
        // check for mandatory options
107 9
        $this->validateConfig($config);
108
109 4
        $this->key             = $config['key'];
110 4
        $this->secret          = $config['secret'];
111 4
        $this->bucket          = $config['bucket'];
112 4
        $this->bucketTTL       = Arr::getValue($config, 'bucketTTL');
113 4
        $this->region          = $config['region'];
114 4
        $this->path            = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
115 4
        $this->acl             = Arr::getValue($config, 'acl', 'private');
116 4
        $this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false);
117 4
    }
118
119
    /**
120
     * Make sure all mandatory keys are present in given config.
121
     *
122
     * @param  array $config
123
     * @throws \phpbu\App\Backup\Sync\Exception
124
     */
125 9
    protected function validateConfig(array $config)
126
    {
127 9 View Code Duplication
        foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) {
128 9
            if (!Arr::isSetAndNotEmptyString($config, $option)) {
129 9
                throw new Exception('AWS S3 ' . $option . ' is mandatory');
130
            }
131
        }
132 4
    }
133
134
    /**
135
     * Execute the sync
136
     *
137
     * @see    \phpbu\App\Backup\Sync::sync()
138
     * @param  \phpbu\App\Backup\Target $target
139
     * @param  \phpbu\App\Result        $result
140
     * @throws \phpbu\App\Backup\Sync\Exception
141
     */
142
    abstract public function sync(Target $target, Result $result);
143
144
    /**
145
     * Simulate the sync execution.
146
     *
147
     * @param \phpbu\App\Backup\Target $target
148
     * @param \phpbu\App\Result        $result
149
     */
150 1
    public function simulate(Target $target, Result $result)
151
    {
152 1
        $result->debug(
153 1
            'sync backup to Amazon S3' . PHP_EOL
154 1
            . '  region:   ' . $this->region . PHP_EOL
155 1
            . '  key:      ' . $this->key . PHP_EOL
156 1
            . '  secret:    ********' . PHP_EOL
157 1
            . '  location: ' . $this->bucket
158
        );
159 1
    }
160
161
    /**
162
     * Should multi part upload be used.
163
     *
164
     * @param  \phpbu\App\Backup\Target $target
165
     * @return bool
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
    /**
177
     * Execute the remote clean up if needed
178
     *
179
     * @param \phpbu\App\Backup\Target $target
180
     * @param \phpbu\App\Result        $result
181
     */
182
    public function cleanup(Target $target, Result $result)
183
    {
184
        // TODO: Implement cleanup() method.
185
    }
186
}
187