1 | <?php |
||
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(); |
|
|
|||
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) |
||
175 | } |
||
176 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: