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 |
||
34 | class MypageController extends AbstractController |
||
|
|||
35 | { |
||
36 | 2 | public function login(Application $app, Request $request) |
|
37 | { |
||
38 | if ($app->isGranted('IS_AUTHENTICATED_FULLY')) { |
||
39 | return $app->redirect($app->url('mypage')); |
||
40 | } |
||
41 | |||
42 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
43 | $builder = $app['form.factory'] |
||
44 | ->createNamedBuilder('', 'customer_login'); |
||
45 | |||
46 | View Code Duplication | if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
|
47 | $Customer = $app->user(); |
||
48 | if ($Customer) { |
||
49 | $builder->get('login_email')->setData($Customer->getEmail()); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $form = $builder->getForm(); |
||
54 | |||
55 | 1 | return $app->render('Mypage/login.twig', array( |
|
56 | 'error' => $app['security.last_error']($request), |
||
57 | 1 | 'form' => $form->createView(), |
|
58 | )); |
||
59 | 2 | } |
|
60 | |||
61 | /** |
||
62 | * @param Application $app |
||
63 | * @param Request $request |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 1 | public function index(Application $app, Request $request) |
|
68 | { |
||
69 | $Customer = $app['user']; |
||
70 | |||
71 | $app['orm.em'] |
||
72 | ->getFilters() |
||
73 | ->enable('incomplete_order_status_hidden'); |
||
74 | |||
75 | // paginator |
||
76 | $qb = $app['eccube.repository.order']->getQueryBuilderByCustomer($Customer); |
||
77 | $pagination = $app['paginator']()->paginate( |
||
78 | $qb, |
||
79 | 1 | $request->get('pageno', 1), |
|
80 | 1 | $app['config']['search_pmax'] |
|
81 | ); |
||
82 | |||
83 | 1 | return $app->render('Mypage/index.twig', array( |
|
84 | 'pagination' => $pagination, |
||
85 | )); |
||
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * @param Application $app |
||
90 | * @param Request $request |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | 2 | public function history(Application $app, Request $request, $id) |
|
95 | { |
||
96 | |||
97 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
98 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
99 | $softDeleteFilter->setExcludes(array( |
||
100 | 'Eccube\Entity\ProductClass', |
||
101 | )); |
||
102 | |||
103 | $Order = $app['eccube.repository.order']->findOneBy(array( |
||
104 | 'id' => $id, |
||
105 | 2 | 'Customer' => $app->user(), |
|
106 | )); |
||
107 | 2 | if (!$Order) { |
|
108 | throw new NotFoundHttpException(); |
||
109 | } |
||
110 | |||
111 | 1 | return $app->render('Mypage/history.twig', array( |
|
112 | 'Order' => $Order, |
||
113 | )); |
||
114 | 2 | } |
|
115 | |||
116 | /** |
||
117 | * @param Application $app |
||
118 | * @param Request $request |
||
119 | * @param id $id |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 1 | public function order(Application $app, Request $request, $id) |
|
124 | { |
||
125 | |||
126 | $this->isTokenValid($app); |
||
127 | |||
128 | $Customer = $app->user(); |
||
129 | |||
130 | /* @var $Order \Eccube\Entity\Order */ |
||
131 | $Order = $app['eccube.repository.order']->findOneBy(array( |
||
132 | 'id' => $id, |
||
133 | 'Customer' => $Customer, |
||
134 | )); |
||
135 | 1 | if (!$Order) { |
|
136 | throw new NotFoundHttpException(); |
||
137 | } |
||
138 | |||
139 | 1 | foreach ($Order->getOrderDetails() as $OrderDetail) { |
|
140 | try { |
||
141 | if ($OrderDetail->getProduct()) { |
||
142 | $app['eccube.service.cart']->addProduct($OrderDetail->getProductClass()->getId(), $OrderDetail->getQuantity())->save(); |
||
143 | } else { |
||
144 | $app->addRequestError('cart.product.delete'); |
||
145 | 1 | } |
|
146 | } catch (CartException $e) { |
||
147 | $app->addRequestError($e->getMessage()); |
||
148 | 1 | } |
|
149 | } |
||
150 | |||
151 | return $app->redirect($app->url('cart')); |
||
152 | 1 | } |
|
153 | |||
154 | /** |
||
155 | * @param Application $app |
||
156 | * @param Request $request |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 1 | public function favorite(Application $app, Request $request) |
|
161 | { |
||
162 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
163 | |||
164 | if ($BaseInfo->getOptionFavoriteProduct() == Constant::ENABLED) { |
||
165 | $Customer = $app->user(); |
||
166 | |||
167 | // paginator |
||
168 | $qb = $app['eccube.repository.product']->getFavoriteProductQueryBuilderByCustomer($Customer); |
||
169 | $pagination = $app['paginator']()->paginate( |
||
170 | $qb, |
||
171 | 1 | $request->get('pageno', 1), |
|
172 | 1 | $app['config']['search_pmax'] |
|
173 | ); |
||
174 | |||
175 | 1 | return $app->render('Mypage/favorite.twig', array( |
|
176 | 'pagination' => $pagination, |
||
177 | )); |
||
178 | } else { |
||
179 | throw new NotFoundHttpException(); |
||
180 | } |
||
181 | 1 | } |
|
182 | |||
183 | /** |
||
184 | * @param Application $app |
||
185 | * @param Request $request |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 1 | public function delete(Application $app, $id) |
|
202 | } |
||
203 |