Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

Admin/Setting/Shop/TaxRuleController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Setting\Shop;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\TaxRule;
19
use Eccube\Event\EccubeEvents;
20
use Eccube\Event\EventArgs;
21
use Eccube\Form\Type\Admin\TaxRuleType;
22
use Eccube\Repository\BaseInfoRepository;
23
use Eccube\Repository\TaxRuleRepository;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Routing\Annotation\Route;
27
28
/**
29
 * Class TaxRuleController
30
 */
31
class TaxRuleController extends AbstractController
32
{
33
    /**
34
     * @var BaseInfo
35
     */
36
    protected $BaseInfo;
37
38
    /**
39
     * @var TaxRuleRepository
40
     */
41
    protected $taxRuleRepository;
42
43
    /**
44
     * TaxRuleController constructor.
45
     *
46
     * @param BaseInfoRepository $baseInfoRepository
47
     * @param TaxRuleRepository $taxRuleRepository
48
     */
49
    public function __construct(BaseInfoRepository $baseInfoRepository, TaxRuleRepository $taxRuleRepository)
50
    {
51 5
        $this->BaseInfo = $baseInfoRepository->get();
52
        $this->taxRuleRepository = $taxRuleRepository;
53 5
    }
54 5
55
    /**
56
     * 税率設定の初期表示・登録
57
     *
58
     * @Route("/%eccube_admin_route%/setting/shop/tax", name="admin_setting_shop_tax")
59
     * @Route("/%eccube_admin_route%/setting/shop/tax/new", name="admin_setting_shop_tax_new")
60
     * @Template("@admin/Setting/Shop/tax_rule.twig")
61
     */
62
    public function index(Request $request)
63
    {
64 2
        $TargetTaxRule = $this->taxRuleRepository->newTaxRule();
65
        $builder = $this->formFactory
66 2
            ->createBuilder(TaxRuleType::class, $TargetTaxRule);
67 2
68 2
        $event = new EventArgs(
69
            [
70 2
                'builder' => $builder,
71
                'BaseInfo' => $this->BaseInfo,
72 2
                'TargetTaxRule' => $TargetTaxRule,
73 2
            ],
74 2
            $request
75
        );
76 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_INITIALIZE, $event);
77
78 2
        $form = $builder->getForm();
79
80 2
        $mode = $request->get('mode');
81
        if ($mode != 'edit_inline') {
82 2
            $form->handleRequest($request);
83 2 View Code Duplication
            if ($form->isSubmitted() && $form->isValid()) {
84 1
                $this->entityManager->persist($TargetTaxRule);
85 1
                $this->entityManager->flush();
86
87
                $event = new EventArgs(
88
                    [
89
                        'form' => $form,
90
                        'BaseInfo' => $this->BaseInfo,
91
                        'TargetTaxRule' => $TargetTaxRule,
92
                    ],
93
                    $request
94
                );
95
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_INDEX_COMPLETE, $event);
96
97
                $this->addSuccess('admin.common.save_complete', 'admin');
98
99
                return $this->redirectToRoute('admin_setting_shop_tax');
100
            }
101
        }
102
103
        // 共通税率一覧
104
        $TaxRules = $this->taxRuleRepository->getList();
105
106 2
        // edit tax rule form
107
        $forms = [];
108
        $errors = [];
109 2
        /** @var TaxRule $TaxRule */
110 2
        foreach ($TaxRules as $TaxRule) {
0 ignored issues
show
The expression $TaxRules of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
111
            /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
112 2
            $builder = $this->formFactory->createBuilder(TaxRuleType::class, $TaxRule);
113
            if ($TaxRule->isDefaultTaxRule()) {
114 2
                $builder->remove('apply_date');
115 2
            }
116 1
            $editTaxRuleForm = $builder->getForm();
117
            // error number
118 2
            $error = 0;
119
            if ($mode == 'edit_inline'
120 2
                && $request->getMethod() === 'POST'
121 2
                && (string) $TaxRule->getId() === $request->get('tax_rule_id')
122 2
            ) {
123 2
                $editTaxRuleForm->handleRequest($request);
124
                if ($editTaxRuleForm->isValid()) {
125 1
                    $taxRuleData = $editTaxRuleForm->getData();
126 1
127 1
                    $this->entityManager->persist($taxRuleData);
128
                    $this->entityManager->flush();
129 1
130 1
                    $this->addSuccess('admin.common.save_complete', 'admin');
131
132 1
                    return $this->redirectToRoute('admin_setting_shop_tax');
133
                }
134 1
                $error = count($editTaxRuleForm->getErrors(true));
135
            }
136
137
            $forms[$TaxRule->getId()] = $editTaxRuleForm->createView();
138
            $errors[$TaxRule->getId()] = $error;
139 1
        }
140 1
141
        return [
142
            'TargetTaxRule' => $TargetTaxRule,
143
            'TaxRules' => $TaxRules,
144 1
            'form' => $form->createView(),
145 1
            'forms' => $forms,
146 1
            'errors' => $errors,
147 1
        ];
148 1
    }
149
150
    /**
151
     * 税率設定の削除
152
     *
153
     * @Route("/%eccube_admin_route%/setting/shop/tax/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_tax_delete", methods={"DELETE"})
154
     */
155
    public function delete(Request $request, TaxRule $TaxRule)
156
    {
157
        $this->isTokenValid();
158 2
159
        if (!$TaxRule->isDefaultTaxRule()) {
160 2
            $this->taxRuleRepository->delete($TaxRule);
161
162 2
            $event = new EventArgs(
163 1
                [
164
                    'TargetTaxRule' => $TaxRule,
165 1
                ],
166
                $request
167 1
            );
168
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_TAX_RULE_DELETE_COMPLETE, $event);
169 1
170
            $this->addSuccess('admin.common.delete_complete', 'admin');
171 1
        }
172
173 1
        return $this->redirectToRoute('admin_setting_shop_tax');
174
    }
175
}
176