Completed
Pull Request — experimental/3.1 (#2484)
by Kentaro
57:08 queued 35:11
created

TaxRuleController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 215
Duplicated Lines 8.84 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
dl 19
loc 215
ccs 84
cts 87
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 19 65 6
A delete() 0 20 2
B editParameter() 0 39 3
B isValid() 0 35 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Setting\Shop;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Entity\BaseInfo;
33
use Eccube\Entity\TaxRule;
34
use Eccube\Event\EccubeEvents;
35
use Eccube\Event\EventArgs;
36
use Eccube\Form\Type\Admin\TaxRuleType;
37
use Eccube\Repository\TaxRuleRepository;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
41
use Symfony\Component\EventDispatcher\EventDispatcher;
42
use Symfony\Component\Form\Form;
43
use Symfony\Component\Form\FormError;
44
use Symfony\Component\Form\FormFactory;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
47
48
/**
49
 * @Component
50
 * @Route(service=TaxRuleController::class)
51
 */
52
class TaxRuleController extends AbstractController
53
{
54
    /**
55
     * @Inject("orm.em")
56
     * @var EntityManager
57
     */
58
    protected $entityManager;
59
60
    /**
61
     * @Inject("eccube.event.dispatcher")
62
     * @var EventDispatcher
63
     */
64
    protected $eventDispatcher;
65
66
    /**
67
     * @Inject("form.factory")
68
     * @var FormFactory
69
     */
70
    protected $formFactory;
71
72
    /**
73
     * @Inject(BaseInfo::class)
74
     * @var BaseInfo
75
     */
76
    protected $BaseInfo;
77
78
    /**
79
     * @Inject(TaxRuleRepository::class)
80
     * @var TaxRuleRepository
81
     */
82
    protected $taxRuleRepository;
83
84
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
85
     * 税率設定の初期表示・登録
86
     *
87
     * @Route("/{_admin}/setting/shop/tax", name="admin_setting_shop_tax")
88
     * @Route("/{_admin}/setting/shop/tax/new", name="admin_setting_shop_tax_new")
89
     * @Route("/{_admin}/setting/shop/tax/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_tax_edit")
90
     * @Template("Setting/Shop/tax_rule.twig")
91
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
92 4
    public function index(Application $app, Request $request, $id = null)
93
    {
94 4
        if (is_null($id)) {
95 1
            $TargetTaxRule = $this->taxRuleRepository->newTaxRule();
96
        } else {
97 3
            $TargetTaxRule = $this->taxRuleRepository->find($id);
98 3
            if (is_null($TargetTaxRule)) {
99 1
                throw new NotFoundHttpException();
100
            }
101
        }
102
103 3
        $builder = $this->formFactory
104 3
            ->createBuilder(TaxRuleType::class, $TargetTaxRule);
105
106
        $builder
107 3
            ->get('option_product_tax_rule')
108 3
            ->setData($this->BaseInfo->getOptionProductTaxRule());
109
110 3
        if ($TargetTaxRule->isDefaultTaxRule()) {
111
            // 基本税率設定は適用日時の変更は行わない
112 1
            $builder = $builder->remove('apply_date');
113
        }
114
115 3
        $event = new EventArgs(
116
            array(
117 3
                'builder' => $builder,
118 3
                'BaseInfo' => $this->BaseInfo,
119 3
                'TargetTaxRule' => $TargetTaxRule,
120
            ),
121 3
            $request
122
        );
123 3
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_INITIALIZE, $event);
124
125 3
        $form = $builder->getForm();
126 3
        $form->handleRequest($request);
127
128 3 View Code Duplication
        if ($form->isSubmitted() && $this->isValid($form)) {
0 ignored issues
show
Compatibility introduced by
$form of type object<Symfony\Component\Form\FormInterface> is not a sub-type of object<Symfony\Component\Form\Form>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
129
130 1
            $this->entityManager->persist($TargetTaxRule);
131 1
            $this->entityManager->flush();
132
133 1
            $event = new EventArgs(
134
                array(
135 1
                    'form' => $form,
136 1
                    'BaseInfo' => $this->BaseInfo,
137 1
                    'TargetTaxRule' => $TargetTaxRule,
138
                ),
139 1
                $request
140
            );
141 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_COMPLETE, $event);
142
143 1
            $app->addSuccess('admin.shop.tax.save.complete', 'admin');
144
145 1
            return $app->redirect($app->url('admin_setting_shop_tax'));
146
        }
147
148
        // 共通税率一覧
149 2
        $TaxRules = $this->taxRuleRepository->getList();
150
151
        return [
152 2
            'TargetTaxRule' => $TargetTaxRule,
153 2
            'TaxRules' => $TaxRules,
154 2
            'form' => $form->createView(),
155
        ];
156
    }
157
158
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$TaxRule" missing
Loading history...
159
     * 税率設定の削除
160
     *
161
     * @Method("DELETE")
162
     * @Route("/{_admin}/setting/shop/tax/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_tax_delete")
163
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
164 2
    public function delete(Application $app, Request $request, TaxRule $TaxRule)
165
    {
166 2
        $this->isTokenValid($app);
167
168 2
        if (!$TaxRule->isDefaultTaxRule()) {
169 1
            $this->taxRuleRepository->delete($TaxRule);
170
171 1
            $event = new EventArgs(
172
                array(
173 1
                    'TargetTaxRule' => $TaxRule,
174
                ),
175 1
                $request
176
            );
177 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_DELETE_COMPLETE, $event);
178
179 1
            $app->addSuccess('admin.shop.tax.delete.complete', 'admin');
180
        }
181
182 2
        return $app->redirect($app->url('admin_setting_shop_tax'));
183
    }
184
185
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
186
     * 軽減税率の有効/無効設定
187
     *
188
     * @Method("POST")
189
     * @Route("/{_admin}/setting/shop/tax/edit_param", name="admin_setting_shop_tax_edit_param")
190
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
191 3
    public function editParameter(Application $app, Request $request)
192
    {
193 3
        $builder = $this->formFactory
194 3
            ->createBuilder(TaxRuleType::class);
195
196 3
        $event = new EventArgs(
197
            array(
198 3
                'builder' => $builder,
199
            ),
200 3
            $request
201
        );
202 3
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_EDIT_PARAMETER_INITIALIZE, $event);
203
204 3
        $form = $builder->getForm();
205 3
        $form->handleRequest($request);
206
207
        // 軽減税率設定の項目のみ処理する
208 3
        if ($form->isSubmitted() && $form['option_product_tax_rule']->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
209
210 1
            $this->BaseInfo->setOptionProductTaxRule($form['option_product_tax_rule']->getData());
211 1
            $this->entityManager->flush($this->BaseInfo);
212
213 1
            $event = new EventArgs(
214
                array(
215 1
                    'form' => $form,
216 1
                    'BaseInfo' => $this->BaseInfo,
217
                ),
218 1
                $request
219
            );
220 1
            $this->eventDispatcher->dispatch(
221 1
                EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_EDIT_PARAMETER_COMPLETE,
222 1
                $event
223
            );
224
225 1
            $app->addSuccess('admin.shop.tax.save.complete', 'admin');
226
        }
227
228 3
        return $app->redirect($app->url('admin_setting_shop_tax'));
229
    }
230
231 1
    protected function isValid(Form $form)
232
    {
233 1
        if (!$form->isValid()) {
234
            return false;
235
        }
236
        /**
237
         * 同一日時のエラーチェック.
238
         */
239
        /** @var $TargetTaxRule \Eccube\Entity\TaxRule */
240 1
        $TargetTaxRule = $form->getData();
241 1
        $parameters = array();
242 1
        $parameters['apply_date'] = $TargetTaxRule->getApplyDate();
243 1
        $qb = $this->entityManager
244 1
            ->getRepository('Eccube\Entity\TaxRule')
245 1
            ->createQueryBuilder('t')
246 1
            ->select('count(t.id)')
247 1
            ->where('t.apply_date = :apply_date');
248
        // 編集時は, 編集対象をのぞいて検索.
249 1
        if ($TargetTaxRule->getId()) {
250 1
            $qb->andWhere('t.id <> :id');
251 1
            $parameters['id'] = $TargetTaxRule->getId();
252
        }
253 1
        $qb->setParameters($parameters);
254
        $count = $qb
255 1
            ->getQuery()
256 1
            ->getSingleScalarResult();
257
        // 同じ適用日時の登録データがあればエラーとする.
258 1
        if ($count > 0) {
259
            $form['apply_date']->addError(new FormError('既に同じ適用日時で登録されています。'));
260
261
            return false;
262
        }
263
264 1
        return true;
265
    }
266
}
267