Completed
Push — master ( 4f1d49...8186c0 )
by Sebastian
07:15
created

AmazonS3   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 52.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 131
rs 10
ccs 19
cts 36
cp 0.5278

3 Methods

Rating   Name   Duplication   Size   Complexity  
C setup() 0 27 7
B sync() 0 25 2
A simulate() 0 10 1
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use Aws\S3\S3Client;
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
11
/**
12
 * Amazon S3 Sync
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 1.1.4
21
 */
22
class AmazonS3 implements Simulator
23
{
24
    /**
25
     * AWS key
26
     *
27
     * @var  string
28
     */
29
    protected $key;
30
31
    /**
32
     * AWS secret
33
     *
34
     * @var  string
35
     */
36
    protected $secret;
37
38
    /**
39
     * AWS S3 bucket
40
     *
41
     * @var string
42
     */
43
    protected $bucket;
44
45
    /**
46
     * AWS S3 region
47
     *
48
     * @var string
49
     */
50
    protected $region;
51
52
    /**
53
     * AWS remote path / object key
54
     *
55
     * @var string
56
     */
57
    protected $path;
58
59
    /**
60
     * AWS acl
61
     * 'private' by default
62
     *
63
     * @var string
64
     */
65
    protected $acl;
66
67
    /**
68
     * (non-PHPDoc)
69
     *
70
     * @see    \phpbu\App\Backup\Sync::setup()
71
     * @param  array $config
72
     * @throws \phpbu\App\Backup\Sync\Exception
73
     */
74 6
    public function setup(array $config)
75
    {
76 6
        if (!class_exists('\\Aws\\S3\\S3Client')) {
77
            throw new Exception('Amazon SDK not loaded: use composer "aws/aws-sdk-php": "2.7.*" to install');
78
        }
79 6
        if (!Arr::isSetAndNotEmptyString($config, 'key')) {
80 1
            throw new Exception('AWS key is mandatory');
81
        }
82 5
        if (!Arr::isSetAndNotEmptyString($config, 'secret')) {
83 1
            throw new Exception('AWS secret is mandatory');
84
        }
85 4
        if (!Arr::isSetAndNotEmptyString($config, 'bucket')) {
86 1
            throw new Exception('AWS S3 bucket name is mandatory');
87
        }
88 3
        if (!Arr::isSetAndNotEmptyString($config, 'region')) {
89 1
            throw new Exception('AWS S3 region is mandatory');
90
        }
91 2
        if (!Arr::isSetAndNotEmptyString($config, 'path')) {
92 1
            throw new Exception('AWS S3 path / object-key is mandatory');
93
        }
94 1
        $this->key    = $config['key'];
95 1
        $this->secret = $config['secret'];
96 1
        $this->bucket = $config['bucket'];
97 1
        $this->region = $config['region'];
98 1
        $this->path   = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']));
99 1
        $this->acl    = Arr::getValue($config, 'acl', 'private');
100 1
    }
101
102
    /**
103
     * Execute the sync
104
     *
105
     * @see    \phpbu\App\Backup\Sync::sync()
106
     * @param  \phpbu\App\Backup\Target $target
107
     * @param  \phpbu\App\Result        $result
108
     * @throws \phpbu\App\Backup\Sync\Exception
109
     */
110
    public function sync(Target $target, Result $result)
111
    {
112
        $sourcePath = $target->getPathname();
113
        $targetPath = $this->path . $target->getFilename();
114
115
        $s3 = S3Client::factory(
116
            [
117
                'signature' => 'v4',
118
                'region'    => $this->region,
119
                'credentials' => [
120
                    'key'    => $this->key,
121
                    'secret' => $this->secret,
122
                ]
123
            ]
124
        );
125
126
        try {
127
            $fh = fopen($sourcePath, 'r');
128
            $s3->upload($this->bucket, $targetPath, $fh, $this->acl);
129
        } catch (\Exception $e) {
130
            throw new Exception($e->getMessage(), null, $e);
131
        }
132
133
        $result->debug('upload: done');
134
    }
135
136
    /**
137
     * Simulate the sync execution.
138
     *
139
     * @param \phpbu\App\Backup\Target $target
140
     * @param \phpbu\App\Result        $result
141
     */
142
    public function simulate(Target $target, Result $result)
143
    {
144
        $result->debug(
145
            'sync backup to Amazon S3' . PHP_EOL
146
            . '  region:   ' . $this->region . PHP_EOL
147
            . '  key:      ' . $this->key . PHP_EOL
148
            . '  secret:    ********' . PHP_EOL
149
            . '  location: ' . $this->bucket
150
        );
151
    }
152
}
153