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 |
||
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) |
|
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) |
||
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) |
||
186 | } |
||
187 |