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 |
||
46 | class OwnerStoreController extends AbstractController |
||
47 | { |
||
48 | /** |
||
49 | * @Inject("config") |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $appConfig; |
||
53 | |||
54 | /** |
||
55 | * @Inject(PluginRepository::class) |
||
56 | * @var PluginRepository |
||
57 | */ |
||
58 | protected $pluginRepository; |
||
59 | |||
60 | /** |
||
61 | * @Inject(PluginService::class) |
||
62 | * @var PluginService |
||
63 | */ |
||
64 | protected $pluginService; |
||
65 | |||
66 | /** |
||
67 | * @Inject("eccube.service.composer") |
||
68 | * @var ComposerServiceInterface |
||
69 | */ |
||
70 | protected $composerService; |
||
71 | |||
72 | /** |
||
73 | * @var EntityManager |
||
74 | * @Inject("orm.em") |
||
75 | */ |
||
76 | protected $em; |
||
77 | |||
78 | /** |
||
79 | * @Inject(SystemService::class) |
||
80 | * @var SystemService |
||
81 | */ |
||
82 | protected $systemService; |
||
83 | |||
84 | private static $vendorName = 'ec-cube'; |
||
85 | |||
86 | /** |
||
87 | * Owner's Store Plugin Installation Screen - Search function |
||
88 | * |
||
89 | * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search") |
||
90 | * @Template("Store/plugin_search.twig") |
||
91 | * @param Application $app |
||
92 | * @param Request $request |
||
93 | * @return array |
||
94 | */ |
||
95 | public function search(Application $app, Request $request) |
||
|
|||
96 | { |
||
97 | // Acquire downloadable plug-in information from owners store |
||
98 | $items = array(); |
||
99 | $promotionItems = array(); |
||
100 | $message = ''; |
||
101 | // Owner's store communication |
||
102 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'; |
||
103 | list($json, $info) = $this->getRequestApi($url); |
||
104 | if ($json === false) { |
||
105 | $message = $this->getResponseErrorMessage($info); |
||
106 | } else { |
||
107 | $data = json_decode($json, true); |
||
108 | if (isset($data['success']) && $data['success']) { |
||
109 | // Check plugin installed |
||
110 | $pluginInstalled = $this->pluginRepository->findAll(); |
||
111 | // Update_status 1 : not install/purchased 、2 : Installed、 3 : Update、4 : paid purchase |
||
112 | foreach ($data['item'] as $item) { |
||
113 | // Not install/purchased |
||
114 | $item['update_status'] = 1; |
||
115 | /** @var Plugin $plugin */ |
||
116 | foreach ($pluginInstalled as $plugin) { |
||
117 | if ($plugin->getSource() == $item['product_id']) { |
||
118 | // Installed |
||
119 | $item['update_status'] = 2; |
||
120 | if ($this->pluginService->isUpdate($plugin->getVersion(), $item['version'])) { |
||
121 | // Need update |
||
122 | $item['update_status'] = 3; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | $items[] = $item; |
||
127 | } |
||
128 | |||
129 | // EC-CUBE version check |
||
130 | foreach ($items as &$item) { |
||
131 | // Not applicable version |
||
132 | $item['version_check'] = 0; |
||
133 | if (in_array(Constant::VERSION, $item['eccube_version'])) { |
||
134 | // Match version |
||
135 | $item['version_check'] = 1; |
||
136 | } |
||
137 | if ($item['price'] != '0' && $item['purchased'] == '0') { |
||
138 | // Not purchased with paid items |
||
139 | $item['update_status'] = 4; |
||
140 | } |
||
141 | // Add plugin dependency |
||
142 | $item['depend'] = $this->pluginService->getRequirePluginName($items, $item); |
||
143 | } |
||
144 | unset($item); |
||
145 | |||
146 | // Promotion item |
||
147 | $i = 0; |
||
148 | View Code Duplication | foreach ($items as $item) { |
|
149 | if ($item['promotion'] == 1) { |
||
150 | $promotionItems[] = $item; |
||
151 | unset($items[$i]); |
||
152 | } |
||
153 | $i++; |
||
154 | } |
||
155 | } else { |
||
156 | $message = $app->trans('admin.plugin.authentication.fail'); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return [ |
||
161 | 'items' => $items, |
||
162 | 'promotionItems' => $promotionItems, |
||
163 | 'message' => $message, |
||
164 | ]; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Do confirm page |
||
169 | * |
||
170 | * @Route("/{_admin}/store/plugin/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_install_confirm") |
||
171 | * @Template("Store/plugin_confirm.twig") |
||
172 | * @param Application $app |
||
173 | * @param Request $request |
||
174 | * @param string $id |
||
175 | * @return array |
||
176 | */ |
||
177 | public function doConfirm(Application $app, Request $request, $id) |
||
178 | { |
||
179 | // Owner's store communication |
||
180 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'; |
||
181 | list($json, $info) = $this->getRequestApi($url); |
||
182 | $data = json_decode($json, true); |
||
183 | $items = $data['item']; |
||
184 | |||
185 | // Find plugin in api |
||
186 | $index = array_search($id, array_column($items, 'product_id')); |
||
187 | if ($index === false) { |
||
188 | throw new NotFoundHttpException(); |
||
189 | } |
||
190 | |||
191 | $pluginCode = $items[$index]['product_code']; |
||
192 | |||
193 | $plugin = $this->pluginService->buildInfo($items, $pluginCode); |
||
194 | |||
195 | // Prevent infinity loop: A -> B -> A. |
||
196 | $dependents[] = $plugin; |
||
197 | $dependents = $this->pluginService->getDependency($items, $plugin, $dependents); |
||
198 | // Unset first param |
||
199 | unset($dependents[0]); |
||
200 | |||
201 | return [ |
||
202 | 'item' => $plugin, |
||
203 | 'dependents' => $dependents, |
||
204 | 'is_update' => $request->get('is_update', false) |
||
205 | ]; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Api Install plugin by composer connect with package repo |
||
210 | * |
||
211 | * @Route("/{_admin}/store/plugin/api/{pluginCode}/{eccubeVersion}/{version}" , name="admin_store_plugin_api_install") |
||
212 | * |
||
213 | * @param Application $app |
||
214 | * @param Request $request |
||
215 | * @param string $pluginCode |
||
216 | * @param string $eccubeVersion |
||
217 | * @param string $version |
||
218 | * @return RedirectResponse |
||
219 | */ |
||
220 | public function apiInstall(Application $app, Request $request, $pluginCode, $eccubeVersion, $version) |
||
221 | { |
||
222 | // Check plugin code |
||
223 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'.'?eccube_version='.$eccubeVersion.'&plugin_code='.$pluginCode.'&version='.$version; |
||
224 | list($json, $info) = $this->getRequestApi($url); |
||
225 | $existFlg = false; |
||
226 | $data = json_decode($json, true); |
||
227 | if ($data && isset($data['success'])) { |
||
228 | $success = $data['success']; |
||
229 | if ($success == '1' && isset($data['item'])) { |
||
230 | foreach ($data['item'] as $item) { |
||
231 | if ($item['product_code'] == $pluginCode) { |
||
232 | $existFlg = true; |
||
233 | break; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | if ($existFlg === false) { |
||
239 | log_info(sprintf('%s plugin not found!', $pluginCode)); |
||
240 | $app->addError('admin.plugin.not.found', 'admin'); |
||
241 | |||
242 | return $app->redirect($app->url('admin_store_plugin_owners_search')); |
||
243 | } |
||
244 | $dependents = array(); |
||
245 | $items = $data['item']; |
||
246 | $plugin = $this->pluginService->buildInfo($items, $pluginCode); |
||
247 | $dependents[] = $plugin; |
||
248 | $dependents = $this->pluginService->getDependency($items, $plugin, $dependents); |
||
249 | |||
250 | // Unset first param |
||
251 | unset($dependents[0]); |
||
252 | $dependentModifier = []; |
||
253 | $packageNames = ''; |
||
254 | if (!empty($dependents)) { |
||
255 | foreach ($dependents as $item) { |
||
256 | $packageNames .= self::$vendorName . '/' . $item['product_code'] . ' '; |
||
257 | $pluginItem = [ |
||
258 | "product_code" => $item['product_code'], |
||
259 | "version" => $item['version'] |
||
260 | ]; |
||
261 | array_push($dependentModifier, $pluginItem); |
||
262 | } |
||
263 | } |
||
264 | $packageNames .= self::$vendorName . '/' . $pluginCode; |
||
265 | $data = array( |
||
266 | 'code' => $pluginCode, |
||
267 | 'version' => $version, |
||
268 | 'core_version' => $eccubeVersion, |
||
269 | 'php_version' => phpversion(), |
||
270 | 'db_version' => $this->systemService->getDbversion(), |
||
271 | 'os' => php_uname('s') . ' ' . php_uname('r') . ' ' . php_uname('v'), |
||
272 | 'host' => $request->getHost(), |
||
273 | 'web_server' => $request->server->get("SERVER_SOFTWARE"), |
||
274 | 'composer_version' => $this->composerService->composerVersion(), |
||
275 | 'composer_execute_mode' => $this->composerService->getMode(), |
||
276 | 'dependents' => json_encode($dependentModifier) |
||
277 | ); |
||
278 | |||
279 | try { |
||
280 | $this->composerService->execRequire($packageNames); |
||
281 | // Do report to package repo |
||
282 | $url = $this->appConfig['package_repo_url'] . '/report'; |
||
283 | $this->postRequestApi($url, $data); |
||
284 | $app->addSuccess('admin.plugin.install.complete', 'admin'); |
||
285 | |||
286 | return $app->redirect($app->url('admin_store_plugin')); |
||
287 | } catch (\Exception $exception) { |
||
288 | log_info($exception); |
||
289 | } |
||
290 | |||
291 | // Do report to package repo |
||
292 | $url = $this->appConfig['package_repo_url'] . '/report/fail'; |
||
293 | $this->postRequestApi($url, $data); |
||
294 | $app->addError('admin.plugin.install.fail', 'admin'); |
||
295 | |||
296 | return $app->redirect($app->url('admin_store_plugin_owners_search')); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Do confirm page |
||
301 | * |
||
302 | * @Route("/{_admin}/store/plugin/delete/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_delete_confirm") |
||
303 | * @Template("Store/plugin_confirm_uninstall.twig") |
||
304 | * @param Application $app |
||
305 | * @param Plugin $Plugin |
||
306 | * @return array|RedirectResponse |
||
307 | */ |
||
308 | public function deleteConfirm(Application $app, Plugin $Plugin) |
||
349 | |||
350 | /** |
||
351 | * New ways to remove plugin: using composer command |
||
352 | * |
||
353 | * @Method("DELETE") |
||
354 | * @Route("/{_admin}/store/plugin/api/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_api_uninstall") |
||
355 | * @param Application $app |
||
356 | * @param Plugin $Plugin |
||
357 | * @return RedirectResponse |
||
358 | */ |
||
359 | public function apiUninstall(Application $app, Plugin $Plugin) |
||
378 | |||
379 | /** |
||
380 | * API request processing |
||
381 | * |
||
382 | * @param string $url |
||
383 | * @return array |
||
384 | */ |
||
385 | private function getRequestApi($url) |
||
411 | |||
412 | /** |
||
413 | * API post request processing |
||
414 | * |
||
415 | * @param string $url |
||
416 | * @param array $data |
||
417 | * @return array |
||
418 | */ |
||
419 | private function postRequestApi($url, $data) |
||
435 | |||
436 | /** |
||
437 | * Get message |
||
438 | * |
||
439 | * @param $info |
||
440 | * @return string |
||
441 | */ |
||
442 | View Code Duplication | private function getResponseErrorMessage($info) |
|
455 | |||
456 | /** |
||
457 | * Do confirm update page |
||
458 | * |
||
459 | * @Route("/{_admin}/store/plugin/{id}/update/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_update_confirm") |
||
460 | * @Template("Store/plugin_confirm.twig") |
||
461 | * @param Application $app |
||
462 | * @param Plugin $plugin |
||
463 | * @return Response |
||
464 | */ |
||
465 | public function doUpdateConfirm(Application $app, Plugin $plugin) |
||
472 | } |
||
473 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.