Completed
Push — master ( 851875...5c8cb5 )
by Kentaro
20s
created

TaxRuleController::index()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 73
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 43
nc 13
nop 3
dl 0
loc 73
ccs 37
cts 37
cp 1
crap 6
rs 8.5021
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Application;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Symfony\Component\Form\Form;
33
use Symfony\Component\Form\FormError;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
37
class TaxRuleController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39
40
    /**
41
     * 税率設定の初期表示・登録
42
     *
43
     * @param Application $app
44
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
45
     * @param null $id
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
46
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
47
     */
48 6
    public function index(Application $app, Request $request, $id = null)
49
    {
50 6
        $TargetTaxRule = null;
0 ignored issues
show
Unused Code introduced by
$TargetTaxRule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52 6
        if ($id == null) {
53 2
            $TargetTaxRule = $app['eccube.repository.tax_rule']->newTaxRule();
54
        } else {
55 4
            $TargetTaxRule = $app['eccube.repository.tax_rule']->find($id);
56 4
            if (is_null($TargetTaxRule)) {
57 1
                throw new NotFoundHttpException();
58
            }
59
        }
60
61
        /** @var  $BaseInfo \Eccube\Entity\BaseInfo */
62 5
        $BaseInfo = $app['eccube.repository.base_info']->get();
63
64 5
        $builder = $app['form.factory']
65 5
            ->createBuilder('tax_rule', $TargetTaxRule);
66
67
        $builder
68 5
            ->get('option_product_tax_rule')
69 5
            ->setData($BaseInfo->getOptionProductTaxRule());
70
71 5
        if ($TargetTaxRule->isDefaultTaxRule()) {
72
            // 基本税率設定は適用日時の変更は行わない
73 2
            $builder = $builder->remove('apply_date');
74
        }
75
76 5
        $event = new EventArgs(
77
            array(
78 5
                'builder' => $builder,
79 5
                'BaseInfo' => $BaseInfo,
80 5
                'TargetTaxRule' => $TargetTaxRule,
81
            ),
82
            $request
83
        );
84 5
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_INITIALIZE, $event);
85
86
        /* @var $form \Symfony\Component\Form\FormInterface */
87 5
        $form = $builder->getForm();
88
89 5
        if ('POST' === $request->getMethod()) {
90 1
            $form->handleRequest($request);
91 1
            if ($this->isValid($app['orm.em'], $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...
92 1
                $app['orm.em']->persist($TargetTaxRule);
93
94 1
                $app['orm.em']->flush();
95
96 1
                $event = new EventArgs(
97
                    array(
98 1
                        'form' => $form,
99 1
                        'BaseInfo' => $BaseInfo,
100 1
                        'TargetTaxRule' => $TargetTaxRule,
101
                    ),
102
                    $request
103
                );
104 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_COMPLETE, $event);
105
106 1
                $app->addSuccess('admin.shop.tax.save.complete', 'admin');
107
108 1
                return $app->redirect($app->url('admin_setting_shop_tax'));
109
            }
110
        }
111
112
        // 共通税率一覧
113 4
        $TaxRules = $app['eccube.repository.tax_rule']->getList();
114
115 4
        return $app->render('Setting/Shop/tax_rule.twig', array(
116 4
            'TargetTaxRule' => $TargetTaxRule,
117 4
            'TaxRules' => $TaxRules,
118 4
            'form' => $form->createView(),
119
        ));
120
    }
121
122
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
123
     * 税率設定の削除
124
     *
125
     * @param Application $app
126
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
127
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
128
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
129
     */
130 4
    public function delete(Application $app, Request $request, $id)
131
    {
132 4
        $this->isTokenValid($app);
133
134 4
        $TargetTaxRule = $app['eccube.repository.tax_rule']->find($id);
135 4
        if (!$TargetTaxRule) {
136 1
            $app->deleteMessage();
137 1
            return $app->redirect($app->url('admin_setting_shop_tax'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
138
        }
139
140 3
        if (!$TargetTaxRule->isDefaultTaxRule()) {
141 1
            $app['eccube.repository.tax_rule']->delete($TargetTaxRule);
142
143 1
            $event = new EventArgs(
144
                array(
145 1
                    'TargetTaxRule' => $TargetTaxRule,
146
                ),
147
                $request
148
            );
149 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_DELETE_COMPLETE, $event);
150
151 1
            $app->addSuccess('admin.shop.tax.delete.complete', 'admin');
152
        }
153
154 3
        return $app->redirect($app->url('admin_setting_shop_tax'));
155
    }
156
157
    /**
158
     * 軽減税率の有効/無効設定
159
     *
160
     * @param Application $app
161
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
162
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
163
     */
164 4
    public function editParameter(Application $app, Request $request)
165
    {
166 4
        $builder = $app['form.factory']
167 4
            ->createBuilder('tax_rule');
168
169 4
        $event = new EventArgs(
170
            array(
171 4
                'builder' => $builder,
172
            ),
173
            $request
174
        );
175 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_EDIT_PARAMETER_INITIALIZE, $event);
176
177 4
        $form = $builder->getForm();
178
179 4 View Code Duplication
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180 3
            $form->handleRequest($request);
181
182
            // 軽減税率設定の項目のみ処理する
183 3
            $optionForm = $form->get('option_product_tax_rule');
184 3
            if ($optionForm->isValid()) {
185
                /** @var  $BaseInfo \Eccube\Entity\BaseInfo */
186 1
                $BaseInfo = $app['eccube.repository.base_info']->get();
187 1
                $BaseInfo->setOptionProductTaxRule($optionForm->getData());
188
189 1
                $app['orm.em']->flush();
190
191 1
                $event = new EventArgs(
192
                    array(
193 1
                        'form' => $form,
194 1
                        'BaseInfo' => $BaseInfo,
195
                    ),
196
                    $request
197
                );
198 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_EDIT_PARAMETER_COMPLETE, $event);
199
200 1
                $app->addSuccess('admin.shop.tax.save.complete', 'admin');
201
            }
202
        }
203
204 4
        return $app->redirect($app->url('admin_setting_shop_tax'));
205
    }
206
207 1
    protected function isValid(EntityManager $em, Form $form)
208
    {
209 1
        if (!$form->isValid()) {
210
            return false;
211
        }
212
        /**
213
         * 同一日時のエラーチェック.
214
         */
215
        /** @var $TargetTaxRule \Eccube\Entity\TaxRule */
216 1
        $TargetTaxRule = $form->getData();
217 1
        $parameters = array();
218 1
        $parameters['apply_date'] = $TargetTaxRule->getApplyDate();
219
        $qb = $em
220 1
            ->getRepository('Eccube\Entity\TaxRule')
221 1
            ->createQueryBuilder('t')
222 1
            ->select('count(t.id)')
223 1
            ->where('t.apply_date = :apply_date');
224
        // 編集時は, 編集対象をのぞいて検索.
225 1
        if ($TargetTaxRule->getId()) {
226 1
            $qb->andWhere('t.id <> :id');
227 1
            $parameters['id'] = $TargetTaxRule->getId();
228
        }
229 1
        $qb->setParameters($parameters);
230
        $count = $qb
231 1
            ->getQuery()
232 1
            ->getSingleScalarResult();
233
        // 同じ適用日時の登録データがあればエラーとする.
234 1
        if ($count > 0) {
235
            $form['apply_date']->addError(new FormError('既に同じ適用日時で登録されています。'));
236
237
            return false;
238
        }
239
240 1
        return true;
241
    }
242
}
243