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 |
||
36 | class OrderController extends AbstractController |
||
|
|||
37 | { |
||
38 | |||
39 | 9 | public function index(Application $app, Request $request, $page_no = null) |
|
40 | { |
||
41 | |||
42 | 9 | $session = $request->getSession(); |
|
43 | |||
44 | 9 | $builder = $app['form.factory'] |
|
45 | 9 | ->createBuilder('admin_search_order'); |
|
46 | |||
47 | 9 | $event = new EventArgs( |
|
48 | array( |
||
49 | 9 | 'builder' => $builder, |
|
50 | ), |
||
51 | $request |
||
52 | ); |
||
53 | 9 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event); |
|
54 | |||
55 | 9 | $searchForm = $builder->getForm(); |
|
56 | |||
57 | 9 | $pagination = array(); |
|
58 | |||
59 | 9 | $disps = $app['eccube.repository.master.disp']->findAll(); |
|
60 | 9 | $pageMaxis = $app['eccube.repository.master.page_max']->findAll(); |
|
61 | 9 | $page_count = $app['config']['default_page_count']; |
|
62 | 9 | $page_status = null; |
|
63 | 9 | $active = false; |
|
64 | |||
65 | 9 | if ('POST' === $request->getMethod()) { |
|
66 | |||
67 | 7 | $searchForm->handleRequest($request); |
|
68 | |||
69 | 7 | View Code Duplication | if ($searchForm->isValid()) { |
70 | 7 | $searchData = $searchForm->getData(); |
|
71 | |||
72 | // paginator |
||
73 | 7 | $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData); |
|
74 | |||
75 | 7 | $event = new EventArgs( |
|
76 | array( |
||
77 | 7 | 'form' => $searchForm, |
|
78 | 7 | 'qb' => $qb, |
|
79 | ), |
||
80 | $request |
||
81 | ); |
||
82 | 7 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event); |
|
83 | |||
84 | 7 | $page_no = 1; |
|
85 | 7 | $pagination = $app['paginator']()->paginate( |
|
86 | $qb, |
||
87 | $page_no, |
||
88 | $page_count |
||
89 | ); |
||
90 | |||
91 | // sessionのデータ保持 |
||
92 | 7 | $session->set('eccube.admin.order.search', $searchData); |
|
93 | 7 | $session->set('eccube.admin.order.search.page_no', $page_no); |
|
94 | } |
||
95 | } else { |
||
96 | 4 | if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) { |
|
97 | // sessionを削除 |
||
98 | 2 | $session->remove('eccube.admin.order.search'); |
|
99 | 2 | $session->remove('eccube.admin.order.search.page_no'); |
|
100 | } else { |
||
101 | // pagingなどの処理 |
||
102 | 2 | $searchData = $session->get('eccube.admin.order.search'); |
|
103 | 2 | if (is_null($page_no)) { |
|
104 | $page_no = intval($session->get('eccube.admin.order.search.page_no')); |
||
105 | } else { |
||
106 | 2 | $session->set('eccube.admin.order.search.page_no', $page_no); |
|
107 | } |
||
108 | |||
109 | 2 | if (!is_null($searchData)) { |
|
110 | |||
111 | // 公開ステータス |
||
112 | 2 | $status = $request->get('status'); |
|
113 | 2 | if (!empty($status)) { |
|
114 | if ($status != $app['config']['admin_product_stock_status']) { |
||
115 | $searchData['status']->clear(); |
||
116 | $searchData['status']->add($status); |
||
117 | } else { |
||
118 | $searchData['stock_status'] = $app['config']['disabled']; |
||
119 | } |
||
120 | $page_status = $status; |
||
121 | } |
||
122 | // 表示件数 |
||
123 | 2 | $pcount = $request->get('page_count'); |
|
124 | |||
125 | 2 | $page_count = empty($pcount) ? $page_count : $pcount; |
|
126 | |||
127 | 2 | $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData); |
|
128 | |||
129 | 2 | $event = new EventArgs( |
|
130 | array( |
||
131 | 2 | 'form' => $searchForm, |
|
132 | 2 | 'qb' => $qb, |
|
133 | ), |
||
134 | $request |
||
135 | ); |
||
136 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event); |
|
137 | |||
138 | 2 | $pagination = $app['paginator']()->paginate( |
|
139 | $qb, |
||
140 | $page_no, |
||
141 | $page_count |
||
142 | ); |
||
143 | |||
144 | // セッションから検索条件を復元 |
||
145 | 2 | if (!empty($searchData['status'])) { |
|
146 | 2 | $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']); |
|
147 | } |
||
148 | 2 | View Code Duplication | if (count($searchData['multi_status']) > 0) { |
149 | $statusIds = array(); |
||
150 | foreach ($searchData['multi_status'] as $Status) { |
||
151 | $statusIds[] = $Status->getId(); |
||
152 | } |
||
153 | $searchData['multi_status'] = $app['eccube.repository.master.order_status']->findBy(array('id' => $statusIds)); |
||
154 | } |
||
155 | 2 | View Code Duplication | if (count($searchData['sex']) > 0) { |
156 | 2 | $sex_ids = array(); |
|
157 | 2 | foreach ($searchData['sex'] as $Sex) { |
|
158 | 2 | $sex_ids[] = $Sex->getId(); |
|
159 | } |
||
160 | 2 | $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids)); |
|
161 | } |
||
162 | 2 | View Code Duplication | if (count($searchData['payment']) > 0) { |
163 | 2 | $payment_ids = array(); |
|
164 | 2 | foreach ($searchData['payment'] as $Payment) { |
|
165 | 2 | $payment_ids[] = $Payment->getId(); |
|
166 | } |
||
167 | 2 | $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids)); |
|
168 | } |
||
169 | 2 | $searchForm->setData($searchData); |
|
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | 9 | return $app->render('Order/index.twig', array( |
|
175 | 9 | 'searchForm' => $searchForm->createView(), |
|
176 | 9 | 'pagination' => $pagination, |
|
177 | 9 | 'disps' => $disps, |
|
178 | 9 | 'pageMaxis' => $pageMaxis, |
|
179 | 9 | 'page_no' => $page_no, |
|
180 | 9 | 'page_status' => $page_status, |
|
181 | 9 | 'page_count' => $page_count, |
|
182 | 9 | 'active' => $active, |
|
183 | )); |
||
184 | |||
185 | } |
||
186 | |||
187 | 2 | public function delete(Application $app, Request $request, $id) |
|
230 | |||
231 | |||
232 | /** |
||
233 | * 受注CSVの出力. |
||
234 | * |
||
235 | * @param Application $app |
||
236 | * @param Request $request |
||
237 | * @return StreamedResponse |
||
238 | */ |
||
239 | public function exportOrder(Application $app, Request $request) |
||
240 | { |
||
241 | |||
242 | // タイムアウトを無効にする. |
||
243 | set_time_limit(0); |
||
244 | |||
245 | // sql loggerを無効にする. |
||
246 | $em = $app['orm.em']; |
||
247 | $em->getConfiguration()->setSQLLogger(null); |
||
248 | |||
249 | $response = new StreamedResponse(); |
||
250 | $response->setCallback(function () use ($app, $request) { |
||
251 | |||
252 | // CSV種別を元に初期化. |
||
253 | $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_ORDER); |
||
254 | |||
255 | // ヘッダ行の出力. |
||
256 | $app['eccube.service.csv.export']->exportHeader(); |
||
257 | |||
258 | // 受注データ検索用のクエリビルダを取得. |
||
259 | $qb = $app['eccube.service.csv.export'] |
||
260 | ->getOrderQueryBuilder($request); |
||
261 | |||
262 | // データ行の出力. |
||
263 | $app['eccube.service.csv.export']->setExportQueryBuilder($qb); |
||
264 | $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) use ($app, $request) { |
||
265 | |||
266 | $Csvs = $csvService->getCsvs(); |
||
267 | |||
268 | $Order = $entity; |
||
269 | $OrderDetails = $Order->getOrderDetails(); |
||
270 | |||
271 | foreach ($OrderDetails as $OrderDetail) { |
||
272 | $ExportCsvRow = new \Eccube\Entity\ExportCsvRow(); |
||
273 | |||
274 | // CSV出力項目と合致するデータを取得. |
||
275 | foreach ($Csvs as $Csv) { |
||
276 | // 受注データを検索. |
||
277 | $ExportCsvRow->setData($csvService->getData($Csv, $Order)); |
||
278 | if ($ExportCsvRow->isDataNull()) { |
||
279 | // 受注データにない場合は, 受注明細を検索. |
||
280 | $ExportCsvRow->setData($csvService->getData($Csv, $OrderDetail)); |
||
281 | } |
||
282 | |||
283 | $event = new EventArgs( |
||
284 | array( |
||
285 | 'csvService' => $csvService, |
||
286 | 'Csv' => $Csv, |
||
287 | 'OrderDetail' => $OrderDetail, |
||
288 | 'ExportCsvRow' => $ExportCsvRow, |
||
289 | ), |
||
290 | $request |
||
291 | ); |
||
292 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER, $event); |
||
293 | |||
294 | $ExportCsvRow->pushData(); |
||
295 | } |
||
296 | |||
297 | //$row[] = number_format(memory_get_usage(true)); |
||
298 | // 出力. |
||
299 | $csvService->fputcsv($ExportCsvRow->getRow()); |
||
300 | } |
||
301 | }); |
||
302 | }); |
||
303 | |||
304 | $now = new \DateTime(); |
||
305 | $filename = 'order_' . $now->format('YmdHis') . '.csv'; |
||
306 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
307 | $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename); |
||
308 | $response->send(); |
||
309 | |||
310 | log_info('受注CSV出力ファイル名', array($filename)); |
||
311 | |||
312 | return $response; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * 配送CSVの出力. |
||
317 | * |
||
318 | * @param Application $app |
||
319 | * @param Request $request |
||
320 | * @return StreamedResponse |
||
321 | */ |
||
322 | public function exportShipping(Application $app, Request $request) |
||
405 | } |
||
406 |