Completed
Push — master ( 4f6d0f...7ea7eb )
by Sebastian
02:56
created

AmazonS3v3   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B sync() 0 26 3
A getFileHandle() 0 8 2
A getUploadPath() 0 8 3
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
9
/**
10
 * Amazon S3 Sync
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
class AmazonS3v3 extends AmazonS3
21
{
22
    /**
23
     * Execute the sync.
24
     *
25
     * @see    \phpbu\App\Backup\Sync::sync()
26
     * @param  \phpbu\App\Backup\Target $target
27
     * @param  \phpbu\App\Result        $result
28
     * @throws \phpbu\App\Backup\Sync\Exception
29
     */
30
    public function sync(Target $target, Result $result)
31
    {
32
        $s3 = new S3Client([
33
            'region'  => $this->region,
34
            'version' => '2006-03-01',
35
            'credentials' => [
36
                'key'    => $this->key,
37
                'secret' => $this->secret,
38
            ]
39
        ]);
40
41
        $s3->registerStreamWrapper();
42
        $source = $this->getFileHandle($target->getPathname(), 'r');
43
        $stream = $this->getFileHandle($this->getUploadPath($target), 'w');
44
45
        try {
46
            while(!feof($source)) {
47
                fwrite($stream, fread($source, 4096));
48
            }
49
            fclose($stream);
50
51
        } catch (\Exception $e) {
52
            throw new Exception($e->getMessage(), null, $e);
53
        }
54
        $result->debug('upload: done');
55
    }
56
57
    /**
58
     * Open stream and validate it.
59
     *
60
     * @param  string $path
61
     * @param  string $mode
62
     * @return resource
63
     * @throws \phpbu\App\Backup\Sync\Exception
64
     */
65
    private function getFileHandle($path, $mode)
66
    {
67
        $handle = fopen($path, $mode);
68
        if (!is_resource($handle)) {
69
            throw new Exception('fopen failed: could not open stream ' . $path);
70
        }
71
        return $handle;
72
    }
73
74
    /**
75
     * Get the s3 upload path
76
     *
77
     * @param \phpbu\App\Backup\Target $target
78
     * @return string
79
     */
80
    public function getUploadPath(Target $target)
81
    {
82
        return 's3://' . $this->bucket
83
               . (substr($this->path, 0, 1) == '/' ? '' : '/')
84
               . $this->path
85
               . (substr($this->path, -1, 1) == '/' ? '' : '/')
86
               . $target->getFilename();
87
    }
88
}
89