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 |
||
43 | class AdminController extends AbstractController |
||
|
|||
44 | { |
||
45 | 3 | public function login(Application $app, Request $request) |
|
46 | { |
||
47 | 3 | if ($app->isGranted('ROLE_ADMIN')) { |
|
48 | return $app->redirect($app->url('admin_homepage')); |
||
49 | } |
||
50 | |||
51 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
52 | 3 | $builder = $app['form.factory'] |
|
53 | 3 | ->createNamedBuilder('', LoginType::class); |
|
54 | |||
55 | 3 | $event = new EventArgs( |
|
56 | array( |
||
57 | 3 | 'builder' => $builder, |
|
58 | ), |
||
59 | 3 | $request |
|
60 | ); |
||
61 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_LOGIN_INITIALIZE, $event); |
|
62 | |||
63 | 3 | $form = $builder->getForm(); |
|
64 | |||
65 | 3 | return $app->render('login.twig', array( |
|
66 | 3 | 'error' => $app['security.last_error']($request), |
|
67 | 3 | 'form' => $form->createView(), |
|
68 | )); |
||
69 | } |
||
70 | |||
71 | 4 | public function index(Application $app, Request $request) |
|
72 | { |
||
73 | // install.phpのチェック. |
||
74 | 4 | if (isset($app['config']['eccube_install']) && $app['config']['eccube_install'] == 1) { |
|
75 | 4 | $file = $app['config']['root_dir'] . '/html/install.php'; |
|
76 | 4 | View Code Duplication | if (file_exists($file)) { |
77 | $message = $app->trans('admin.install.warning', array('installphpPath' => 'html/install.php')); |
||
78 | $app->addWarning($message, 'admin'); |
||
79 | } |
||
80 | 4 | $fileOnRoot = $app['config']['root_dir'] . '/install.php'; |
|
81 | 4 | View Code Duplication | if (file_exists($fileOnRoot)) { |
82 | 4 | $message = $app->trans('admin.install.warning', array('installphpPath' => 'install.php')); |
|
83 | 4 | $app->addWarning($message, 'admin'); |
|
84 | } |
||
85 | } |
||
86 | |||
87 | // 受注マスター検索用フォーム |
||
88 | 4 | $searchOrderBuilder = $app['form.factory'] |
|
89 | 4 | ->createBuilder(SearchOrderType::class); |
|
90 | // 商品マスター検索用フォーム |
||
91 | 4 | $searchProductBuilder = $app['form.factory'] |
|
92 | 4 | ->createBuilder(SearchProductType::class); |
|
93 | // 会員マスター検索用フォーム |
||
94 | 4 | $searchCustomerBuilder = $app['form.factory'] |
|
95 | 4 | ->createBuilder(SearchCustomerType::class); |
|
96 | |||
97 | 4 | $event = new EventArgs( |
|
98 | array( |
||
99 | 4 | 'searchOrderBuilder' => $searchOrderBuilder, |
|
100 | 4 | 'searchProductBuilder' => $searchProductBuilder, |
|
101 | 4 | 'searchCustomerBuilder' => $searchCustomerBuilder, |
|
102 | ), |
||
103 | 4 | $request |
|
104 | ); |
||
105 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_INITIALIZE, $event); |
|
106 | |||
107 | // 受注マスター検索用フォーム |
||
108 | 4 | $searchOrderForm = $searchOrderBuilder->getForm(); |
|
109 | |||
110 | // 商品マスター検索用フォーム |
||
111 | 4 | $searchProductForm = $searchProductBuilder->getForm(); |
|
112 | |||
113 | // 会員マスター検索用フォーム |
||
114 | 4 | $searchCustomerForm = $searchCustomerBuilder->getForm(); |
|
115 | |||
116 | /** |
||
117 | * 受注状況. |
||
118 | */ |
||
119 | 4 | $excludes = array(); |
|
120 | 4 | $excludes[] = $app['config']['order_pending']; |
|
121 | 4 | $excludes[] = $app['config']['order_processing']; |
|
122 | 4 | $excludes[] = $app['config']['order_cancel']; |
|
123 | 4 | $excludes[] = $app['config']['order_deliv']; |
|
124 | |||
125 | 4 | $event = new EventArgs( |
|
126 | array( |
||
127 | 4 | 'excludes' => $excludes, |
|
128 | ), |
||
129 | 4 | $request |
|
130 | ); |
||
131 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_ORDER, $event); |
|
132 | 4 | $excludes = $event->getArgument('excludes'); |
|
133 | |||
134 | // 受注ステータスごとの受注件数. |
||
135 | 4 | $Orders = $this->getOrderEachStatus($app['orm.em'], $excludes); |
|
136 | // 受注ステータスの一覧. |
||
137 | 4 | $OrderStatuses = $this->findOrderStatus($app['orm.em'], $excludes); |
|
138 | |||
139 | /** |
||
140 | * 売り上げ状況 |
||
141 | */ |
||
142 | 4 | $excludes = array(); |
|
143 | 4 | $excludes[] = $app['config']['order_processing']; |
|
144 | 4 | $excludes[] = $app['config']['order_cancel']; |
|
145 | 4 | $excludes[] = $app['config']['order_pending']; |
|
146 | |||
147 | 4 | $event = new EventArgs( |
|
148 | array( |
||
149 | 4 | 'excludes' => $excludes, |
|
150 | ), |
||
151 | 4 | $request |
|
152 | ); |
||
153 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_SALES, $event); |
|
154 | 4 | $excludes = $event->getArgument('excludes'); |
|
155 | |||
156 | // 今日の売上/件数 |
||
157 | 4 | $salesToday = $this->getSalesByDay($app['orm.em'], new \DateTime(), $excludes); |
|
158 | // 昨日の売上/件数 |
||
159 | 4 | $salesYesterday = $this->getSalesByDay($app['orm.em'], new \DateTime('-1 day'), $excludes); |
|
160 | // 今月の売上/件数 |
||
161 | 4 | $salesThisMonth = $this->getSalesByMonth($app['orm.em'], new \DateTime(), $excludes); |
|
162 | |||
163 | /** |
||
164 | * ショップ状況 |
||
165 | */ |
||
166 | // 在庫切れ商品数 |
||
167 | 4 | $countNonStockProducts = $this->countNonStockProducts($app['orm.em']); |
|
168 | // 本会員数 |
||
169 | 4 | $countCustomers = $this->countCustomers($app['orm.em']); |
|
170 | |||
171 | 4 | $event = new EventArgs( |
|
172 | array( |
||
173 | 4 | 'Orders' => $Orders, |
|
174 | 4 | 'OrderStatuses' => $OrderStatuses, |
|
175 | 4 | 'salesThisMonth' => $salesThisMonth, |
|
176 | 4 | 'salesToday' => $salesToday, |
|
177 | 4 | 'salesYesterday' => $salesYesterday, |
|
178 | 4 | 'countNonStockProducts' => $countNonStockProducts, |
|
179 | 4 | 'countCustomers' => $countCustomers, |
|
180 | ), |
||
181 | 4 | $request |
|
182 | ); |
||
183 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_COMPLETE, $event); |
|
184 | |||
185 | 4 | return $app->render('index.twig', array( |
|
186 | 4 | 'searchOrderForm' => $searchOrderForm->createView(), |
|
187 | 4 | 'searchProductForm' => $searchProductForm->createView(), |
|
188 | 4 | 'searchCustomerForm' => $searchCustomerForm->createView(), |
|
189 | 4 | 'Orders' => $Orders, |
|
190 | 4 | 'OrderStatuses' => $OrderStatuses, |
|
191 | 4 | 'salesThisMonth' => $salesThisMonth, |
|
192 | 4 | 'salesToday' => $salesToday, |
|
193 | 4 | 'salesYesterday' => $salesYesterday, |
|
194 | 4 | 'countNonStockProducts' => $countNonStockProducts, |
|
195 | 4 | 'countCustomers' => $countCustomers, |
|
196 | )); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * パスワード変更画面 |
||
201 | * |
||
202 | * @param Application $app |
||
203 | * @param Request $request |
||
204 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
205 | */ |
||
206 | 3 | public function changePassword(Application $app, Request $request) |
|
207 | { |
||
208 | 3 | $builder = $app['form.factory'] |
|
209 | 3 | ->createBuilder(ChangePasswordType::class); |
|
210 | |||
211 | 3 | $event = new EventArgs( |
|
212 | array( |
||
213 | 3 | 'builder' => $builder, |
|
214 | ), |
||
215 | 3 | $request |
|
216 | ); |
||
217 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_CHANGE_PASSWORD_INITIALIZE, $event); |
|
218 | |||
219 | 3 | $form = $builder->getForm(); |
|
220 | 3 | $form->handleRequest($request); |
|
221 | |||
222 | 3 | if ($form->isSubmitted() && $form->isValid()) { |
|
223 | 1 | $password = $form->get('change_password')->getData(); |
|
224 | |||
225 | 1 | $Member = $app->user(); |
|
226 | |||
227 | 1 | $dummyMember = clone $Member; |
|
228 | 1 | $dummyMember->setPassword($password); |
|
229 | 1 | $salt = $dummyMember->getSalt(); |
|
230 | 1 | if (!isset($salt)) { |
|
231 | $salt = $app['eccube.repository.member']->createSalt(5); |
||
232 | $dummyMember->setSalt($salt); |
||
233 | } |
||
234 | |||
235 | 1 | $encryptPassword = $app['eccube.repository.member']->encryptPassword($dummyMember); |
|
236 | |||
237 | $Member |
||
238 | 1 | ->setPassword($encryptPassword) |
|
239 | 1 | ->setSalt($salt); |
|
240 | |||
241 | 1 | $status = $app['eccube.repository.member']->save($Member); |
|
242 | 1 | if ($status) { |
|
243 | 1 | $event = new EventArgs( |
|
244 | array( |
||
245 | 1 | 'form' => $form, |
|
246 | ), |
||
247 | 1 | $request |
|
248 | ); |
||
249 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIN_CHANGE_PASSWORD_COMPLETE, $event); |
|
250 | |||
251 | 1 | $app->addSuccess('admin.change_password.save.complete', 'admin'); |
|
252 | |||
253 | 1 | return $app->redirect($app->url('admin_change_password')); |
|
254 | } |
||
255 | |||
256 | $app->addError('admin.change_password.save.error', 'admin'); |
||
257 | } |
||
258 | |||
259 | 2 | return $app->render('change_password.twig', array( |
|
260 | 2 | 'form' => $form->createView(), |
|
261 | )); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * 在庫なし商品の検索結果を表示する. |
||
266 | * |
||
267 | * @param Application $app |
||
268 | * @param Request $request |
||
269 | * @return \Symfony\Component\HttpFoundation\Response |
||
270 | */ |
||
271 | 2 | public function searchNonStockProducts(Application $app, Request $request) |
|
272 | { |
||
273 | // 商品マスター検索用フォーム |
||
274 | /* @var Form $form */ |
||
275 | 2 | $form = $app['form.factory'] |
|
276 | 2 | ->createBuilder(SearchProductType::class) |
|
277 | 2 | ->getForm(); |
|
278 | |||
279 | 2 | $form->handleRequest($request); |
|
280 | 2 | if ($form->isSubmitted() && $form->isValid()) { |
|
281 | // 在庫なし商品の検索条件をセッションに付与し, 商品マスタへリダイレクトする. |
||
282 | 1 | $searchData = array(); |
|
283 | 1 | $searchData['stock_status'] = Constant::DISABLED; |
|
284 | 1 | $session = $request->getSession(); |
|
285 | 1 | $session->set('eccube.admin.product.search', $searchData); |
|
286 | |||
287 | 1 | return $app->redirect($app->url('admin_product_page', array( |
|
288 | 1 | 'page_no' => 1, |
|
289 | 1 | 'status' => $app['config']['admin_product_stock_status']))); |
|
290 | } |
||
291 | |||
292 | 1 | return $app->redirect($app->url('admin_homepage')); |
|
293 | } |
||
294 | |||
295 | 4 | protected function findOrderStatus($em, array $excludes) |
|
296 | { |
||
297 | /* @var $qb QueryBuilder */ |
||
298 | $qb = $em |
||
299 | 4 | ->getRepository('Eccube\Entity\Master\OrderStatus') |
|
300 | 4 | ->createQueryBuilder('os'); |
|
301 | |||
302 | return $qb |
||
303 | 4 | ->where($qb->expr()->notIn('os.id', $excludes)) |
|
304 | 4 | ->orderBy('os.rank', 'ASC') |
|
305 | 4 | ->getQuery() |
|
306 | 4 | ->getResult(); |
|
307 | } |
||
308 | |||
309 | 4 | protected function getOrderEachStatus($em, array $excludes) |
|
336 | |||
337 | 4 | View Code Duplication | protected function getSalesByMonth($em, $dateTime, array $excludes) |
367 | |||
368 | 4 | View Code Duplication | protected function getSalesByDay($em, $dateTime, array $excludes) |
398 | |||
399 | 4 | protected function countNonStockProducts($em) |
|
400 | { |
||
401 | /** @var $qb \Doctrine\ORM\QueryBuilder */ |
||
402 | 4 | $qb = $em->getRepository('Eccube\Entity\Product') |
|
403 | 4 | ->createQueryBuilder('p') |
|
413 | |||
414 | 4 | protected function countCustomers($em) |
|
431 | } |
||
432 |