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 DeliveryController extends AbstractController |
||
|
|||
35 | { |
||
36 | private $main_title; |
||
37 | private $sub_title; |
||
38 | |||
39 | public $form; |
||
40 | |||
41 | public function __construct() |
||
44 | |||
45 | View Code Duplication | public function index(Application $app, Request $request) |
|
65 | |||
66 | public function edit(Application $app, Request $request, $id = 0) |
||
67 | { |
||
68 | /* @var $Delivery \Eccube\Entity\Delivery */ |
||
69 | $Delivery = $app['eccube.repository.delivery'] |
||
70 | ->findOrCreate($id); |
||
71 | |||
72 | // FormType: DeliveryFeeの生成 |
||
73 | $Prefs = $app['eccube.repository.master.pref'] |
||
74 | ->findAll(); |
||
75 | |||
76 | foreach ($Prefs as $Pref) { |
||
77 | $DeliveryFee = $app['eccube.repository.delivery_fee'] |
||
78 | ->findOrCreate(array( |
||
79 | 'Delivery' => $Delivery, |
||
80 | 'Pref' => $Pref, |
||
81 | )); |
||
82 | if (!$DeliveryFee->getFee()) { |
||
83 | $Delivery->addDeliveryFee($DeliveryFee); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $DeliveryFees = $Delivery->getDeliveryFees(); |
||
88 | $DeliveryFeesIndex = array(); |
||
89 | foreach ($DeliveryFees as $DeliveryFee) { |
||
90 | $Delivery->removeDeliveryFee($DeliveryFee); |
||
91 | $DeliveryFeesIndex[$DeliveryFee->getPref()->getId()] = $DeliveryFee; |
||
92 | } |
||
93 | ksort($DeliveryFeesIndex); |
||
94 | foreach ($DeliveryFeesIndex as $timeId => $DeliveryFee) { |
||
95 | $Delivery->addDeliveryFee($DeliveryFee); |
||
96 | } |
||
97 | |||
98 | // FormType: DeliveryTimeの生成 |
||
99 | $DeliveryTimes = $Delivery->getDeliveryTimes(); |
||
100 | $loop = 16 - count($DeliveryTimes); |
||
101 | for ($i = 1; $i <= $loop; $i++) { |
||
102 | $DeliveryTime = new \Eccube\Entity\DeliveryTime(); |
||
103 | $DeliveryTime->setDelivery($Delivery); |
||
104 | $Delivery->addDeliveryTime($DeliveryTime); |
||
105 | } |
||
106 | |||
107 | $builder = $app['form.factory'] |
||
108 | ->createBuilder('delivery', $Delivery); |
||
109 | |||
110 | $event = new EventArgs( |
||
111 | array( |
||
112 | 'builder' => $builder, |
||
113 | 'Delivery' => $Delivery, |
||
114 | 'Prefs' => $Prefs, |
||
115 | 'DeliveryFees' => $DeliveryFees, |
||
116 | ), |
||
117 | $request |
||
118 | ); |
||
119 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_INITIALIZE, $event); |
||
120 | |||
121 | $form = $builder->getForm(); |
||
122 | |||
123 | // 支払方法をセット |
||
124 | $Payments = array(); |
||
125 | foreach ($Delivery->getPaymentOptions() as $PaymentOption) { |
||
126 | $Payments[] = $PaymentOption->getPayment(); |
||
127 | } |
||
128 | |||
129 | $form['delivery_times']->setData($Delivery->getDeliveryTimes()); |
||
130 | $form['payments']->setData($Payments); |
||
131 | |||
132 | // 登録ボタン押下 |
||
133 | if ($app['request']->getMethod() === 'POST') { |
||
134 | $form->handleRequest($app['request']); |
||
135 | |||
136 | if ($form->isValid()) { |
||
137 | $DeliveryData = $form->getData(); |
||
138 | |||
139 | // 配送時間の登録 |
||
140 | $DeliveryTimes = $form['delivery_times']->getData(); |
||
141 | foreach ($DeliveryTimes as $DeliveryTime) { |
||
142 | if (is_null($DeliveryTime->getDeliveryTime())) { |
||
143 | $Delivery->removeDeliveryTime($DeliveryTime); |
||
144 | $app['orm.em']->remove($DeliveryTime); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | // お支払いの登録 |
||
149 | $PaymentOptions = $app['eccube.repository.payment_option'] |
||
150 | ->findBy(array('delivery_id' => $id)); |
||
151 | // 消す |
||
152 | foreach ($PaymentOptions as $PaymentOption) { |
||
153 | $DeliveryData->removePaymentOption($PaymentOption); |
||
154 | $app['orm.em']->remove($PaymentOption); |
||
155 | } |
||
156 | $app['orm.em']->persist($DeliveryData); |
||
157 | $app['orm.em']->flush(); |
||
158 | |||
159 | // いれる |
||
160 | $PaymentsData = $form->get('payments')->getData(); |
||
161 | foreach ($PaymentsData as $PaymentData) { |
||
162 | $PaymentOption = new \Eccube\Entity\PaymentOption(); |
||
163 | $PaymentOption |
||
164 | ->setPaymentId($PaymentData->getId()) |
||
165 | ->setPayment($PaymentData) |
||
166 | ->setDeliveryId($DeliveryData->getId()) |
||
167 | ->setDelivery($DeliveryData); |
||
168 | $DeliveryData->addPaymentOption($PaymentOption); |
||
169 | $app['orm.em']->persist($DeliveryData); |
||
170 | } |
||
171 | |||
172 | $app['orm.em']->persist($DeliveryData); |
||
173 | |||
174 | $app['orm.em']->flush(); |
||
175 | |||
176 | $event = new EventArgs( |
||
177 | array( |
||
178 | 'form' => $form, |
||
179 | 'Delivery' => $Delivery, |
||
180 | 'Prefs' => $Prefs, |
||
181 | 'DeliveryFees' => $DeliveryFees, |
||
182 | ), |
||
183 | $request |
||
184 | ); |
||
185 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_COMPLETE, $event); |
||
186 | |||
187 | $app->addSuccess('admin.register.complete', 'admin'); |
||
188 | |||
189 | return $app->redirect($app->url('admin_setting_shop_delivery')); |
||
190 | } |
||
191 | } |
||
192 | return $app->render('Setting/Shop/delivery_edit.twig', array( |
||
193 | 'form' => $form->createView(), |
||
194 | 'delivery_id' => $id, |
||
195 | )); |
||
196 | } |
||
197 | |||
198 | public function delete(Application $app, Request $request, $id) |
||
243 | |||
244 | View Code Duplication | public function moveRank(Application $app, Request $request) |
|
259 | } |
||
260 |