Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
21 | abstract class AmazonS3 implements Simulator |
||
22 | { |
||
23 | /** |
||
24 | * AWS key |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $key; |
||
29 | |||
30 | /** |
||
31 | * AWS secret |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $secret; |
||
36 | |||
37 | /** |
||
38 | * AWS S3 bucket |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $bucket; |
||
43 | |||
44 | /** |
||
45 | * TTL for all items in this bucket. |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $bucketTTL; |
||
50 | |||
51 | /** |
||
52 | * AWS S3 region |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $region; |
||
57 | |||
58 | /** |
||
59 | * AWS remote path / object key |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $path; |
||
64 | |||
65 | /** |
||
66 | * AWS acl |
||
67 | * 'private' by default |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $acl; |
||
72 | |||
73 | /** |
||
74 | * Use multi part config |
||
75 | * |
||
76 | * @var boolean |
||
77 | */ |
||
78 | protected $multiPartUpload; |
||
79 | |||
80 | /** |
||
81 | * Min multi part upload size |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | protected $minMultiPartUploadSize = 5242880; |
||
86 | |||
87 | /** |
||
88 | * Max stream upload size |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | protected $maxStreamUploadSize = 104857600; |
||
93 | |||
94 | /** |
||
95 | * Configure the sync. |
||
96 | * |
||
97 | * @see \phpbu\App\Backup\Sync::setup() |
||
98 | * @param array $config |
||
99 | * @throws \phpbu\App\Backup\Sync\Exception |
||
100 | */ |
||
101 | 9 | public function setup(array $config) |
|
102 | { |
||
103 | 9 | if (!class_exists('\\Aws\\S3\\S3Client')) { |
|
104 | throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
||
105 | } |
||
106 | |||
107 | // check for mandatory options |
||
108 | 9 | $this->validateConfig($config); |
|
109 | |||
110 | 4 | $this->key = $config['key']; |
|
111 | 4 | $this->secret = $config['secret']; |
|
112 | 4 | $this->bucket = $config['bucket']; |
|
113 | 4 | $this->bucketTTL = Arr::getValue($config, 'bucketTTL'); |
|
114 | 4 | $this->region = $config['region']; |
|
115 | 4 | $this->path = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])); |
|
116 | 4 | $this->acl = Arr::getValue($config, 'acl', 'private'); |
|
117 | 4 | $this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false); |
|
118 | 4 | } |
|
119 | |||
120 | /** |
||
121 | * Make sure all mandatory keys are present in given config. |
||
122 | * |
||
123 | * @param array $config |
||
124 | * @throws \phpbu\App\Backup\Sync\Exception |
||
125 | */ |
||
126 | 9 | protected function validateConfig(array $config) |
|
127 | { |
||
128 | 9 | View Code Duplication | foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) { |
129 | 9 | if (!Arr::isSetAndNotEmptyString($config, $option)) { |
|
130 | 9 | throw new Exception('AWS S3 ' . $option . ' is mandatory'); |
|
131 | } |
||
132 | } |
||
133 | 4 | } |
|
134 | |||
135 | /** |
||
136 | * Execute the sync |
||
137 | * |
||
138 | * @see \phpbu\App\Backup\Sync::sync() |
||
139 | * @param \phpbu\App\Backup\Target $target |
||
140 | * @param \phpbu\App\Result $result |
||
141 | * @throws \phpbu\App\Backup\Sync\Exception |
||
142 | */ |
||
143 | abstract public function sync(Target $target, Result $result); |
||
144 | |||
145 | /** |
||
146 | * Simulate the sync execution. |
||
147 | * |
||
148 | * @param \phpbu\App\Backup\Target $target |
||
149 | * @param \phpbu\App\Result $result |
||
150 | */ |
||
151 | 1 | public function simulate(Target $target, Result $result) |
|
161 | |||
162 | /** |
||
163 | * Should multi part upload be used. |
||
164 | * |
||
165 | * @param \phpbu\App\Backup\Target $target |
||
166 | * @return bool |
||
167 | */ |
||
168 | protected function useMultiPartUpload(Target $target) |
||
176 | |||
177 | /** |
||
178 | * Execute the remote clean up if needed |
||
179 | * |
||
180 | * @param \phpbu\App\Backup\Target $target |
||
181 | * @param \phpbu\App\Result $result |
||
182 | */ |
||
183 | public function cleanup(Target $target, Result $result) |
||
187 | } |
||
188 |