AddCartType::validate()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 18
Ratio 64.29 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 5
nop 2
dl 18
loc 28
rs 8.5806
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
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\Form\Type;
26
27
use Symfony\Component\Form\AbstractType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\Form\FormEvent;
30
use Symfony\Component\Form\FormEvents;
31
use Symfony\Component\Form\FormInterface;
32
use Symfony\Component\Form\FormView;
33
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
34
use Symfony\Component\Validator\Constraints as Assert;
35
use Symfony\Component\Validator\Context\ExecutionContext;
36
37
class AddCartType extends AbstractType
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39
40
    public $config;
41
    public $security;
42
    public $customerFavoriteProductRepository;
43
    public $Product = null;
0 ignored issues
show
Coding Style introduced by
Member variable "Product" is not in valid camel caps format
Loading history...
44
45 286
    public function __construct(
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
        $config,
47
        \Symfony\Component\Security\Core\SecurityContext $security,
48
        \Eccube\Repository\CustomerFavoriteProductRepository $customerFavoriteProductRepository
49
    ) {
50 286
        $this->config = $config;
51 286
        $this->security = $security;
52 286
        $this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function buildForm(FormBuilderInterface $builder, array $options)
59
    {
60
        /* @var $Product \Eccube\Entity\Product */
61 2
        $Product = $options['product'];
62 2
        $this->Product = $Product;
63
        $ProductClasses = $Product->getProductClasses();
64
65
        $builder
66
            ->add('mode', 'hidden', array(
67
                'data' => 'add_cart',
68
            ))
69
            ->add('product_id', 'hidden', array(
70 2
                'data' => $Product->getId(),
71
                'constraints' => array(
72
                    new Assert\NotBlank(),
73
                    new Assert\Regex(array('pattern' => '/^\d+$/')),
74 2
                ),
75
            ))
76
            ->add('product_class_id', 'hidden', array(
77
                'data' => count($ProductClasses) === 1 ? $ProductClasses[0]->getId() : '',
78
                'constraints' => array(
79
                    new Assert\Regex(array('pattern' => '/^\d+$/')),
80 2
                ),
81
            ));
82
83
        if ($Product->getStockFind()) {
84
            $builder
85
                ->add('quantity', 'integer', array(
86
                    'data' => 1,
87 2
                    'attr' => array(
88
                        'min' => 1,
89 2
                        'maxlength' => $this->config['int_len'],
90 2
                    ),
91 2
                    'constraints' => array(
92
                        new Assert\NotBlank(),
93
                        new Assert\GreaterThanOrEqual(array(
94
                            'value' => 1,
95
                        )),
96
                        new Assert\Regex(array('pattern' => '/^\d+$/')),
97
                    ),
98 2
                ))
99
            ;
100 2
            if ($Product && $Product->getProductClasses()) {
101
                if (!is_null($Product->getClassName1())) {
102
                    $builder->add('classcategory_id1', 'choice', array(
103
                        'label' => $Product->getClassName1(),
104 3
                        'choices'   => array('__unselected' => '選択してください') + $Product->getClassCategories1(),
105
                    ));
106
                }
107
                if (!is_null($Product->getClassName2())) {
108
                    $builder->add('classcategory_id2', 'choice', array(
109
                        'label' => $Product->getClassName2(),
110 3
                        'choices' => array('__unselected' => '選択してください'),
111 3
                    ));
112
                }
113
            }
114
115
            $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) {
116
                $data = $event->getData();
117
                $form = $event->getForm();
118
                if (!is_null($Product->getClassName2())) {
119
                    if ($data['classcategory_id1']) {
120
                        $form->add('classcategory_id2', 'choice', array(
121
                            'label' => $Product->getClassName2(),
122
                            'choices' => array('__unselected' => '選択してください') + $Product->getClassCategories2($data['classcategory_id1']),
123
                        ));
124
                    }
125
                }
126
            });
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function setDefaultOptions(OptionsResolverInterface $resolver)
134 3
    {
135
        $resolver->setRequired('product');
0 ignored issues
show
Documentation introduced by
'product' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
        $resolver->setDefaults(array(
137
            'id_add_product_id' => true,
138 3
            'constraints' => array(
139
                new Assert\Callback(array($this, 'validate')),
140
            ),
141 3
        ));
142
    }
143
144
    /*
145
     * {@inheritdoc}
146
     */
147
    public function finishView(FormView $view, FormInterface $form, array $options)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
148 2
    {
149
        if ($options['id_add_product_id']) {
150 2
            foreach ($view->vars['form']->children as $child) {
151
                $child->vars['id'] .= $options['product']->getId();
152
            }
153
        }
154
155
        if ($view->vars['form']->children['mode']->vars['value'] === 'add_cart') {
156 2
            $view->vars['form']->children['mode']->vars['value'] = '';
157 2
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getName()
164 283
    {
165
        return 'add_cart';
166 283
    }
167
168
    /**
169
     * validate
170
     *
171
     * @param type             $data
172
     * @param ExecutionContext $context
173
     */
174
    public function validate($data, ExecutionContext $context)
175
    {
176
        if ($data['mode'] !== 'add_favorite') {
177
            $context->validateValue($data['product_class_id'], array(
178
                new Assert\NotBlank(),
179
            ), '[product_class_id]');
180 View Code Duplication
            if ($this->Product->getClassName1()) {
181
                $context->validateValue($data['classcategory_id1'], array(
182
                    new Assert\NotBlank(),
183
                    new Assert\NotEqualTo(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
184
                        'value' => '__unselected',
185
                        'message' => 'form.type.select.notselect'
186
                    )),
187
                ), '[classcategory_id1]');
188
            }
189
            //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
190 View Code Duplication
            if ($this->Product->getClassName2()) {
191
                $context->validateValue($data['classcategory_id2'], array(
192
                    new Assert\NotBlank(),
193
                    new Assert\NotEqualTo(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
194
                        'value' => '__unselected',
195
                        'message' => 'form.type.select.notselect'
196
                    )),
197
                ), '[classcategory_id2]');
198
            }
199
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
200
        }
201
    }
202
}
203