BlockType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 26.8 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 26
loc 97
rs 10
c 0
b 0
f 0
ccs 15
cts 16
cp 0.9375
wmc 6
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A __construct() 0 4 1
A buildForm() 26 66 3
A setDefaultOptions() 0 6 1

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\Form\Type\Admin;
26
27
use Symfony\Component\Form\AbstractType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\Form\FormError;
30
use Symfony\Component\Form\FormEvents;
31
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
32
use Symfony\Component\Validator\Constraints as Assert;
33
34
class BlockType extends AbstractType
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    public $app;
37
38 286
    public function __construct(\Silex\Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40 286
        $this->app = $app;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function buildForm(FormBuilderInterface $builder, array $options)
47
    {
48 1
        $app = $this->app;
49
50
        $builder
51
            ->add('name', 'text', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
52 1
                'label' => 'ブロック名',
53 1
                'required' => true,
54
                'constraints' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
55
                    new Assert\NotBlank(),
56
                    new Assert\Length(array(
57 1
                        'max' => $app['config']['stext_len'],
58
                    ))
59 1
                )
60
            ))
61
            ->add('file_name', 'text', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
62 1
                'label' => 'ファイル名',
63 1
                'required' => true,
64
                'constraints' => array(
65
                    new Assert\NotBlank(),
66
                    new Assert\Length(array(
67 1
                        'max' => $app['config']['stext_len'],
68
                    )),
69
                    new Assert\Regex(array(
70
                        'pattern' => '/^[0-9a-zA-Z\/_]+$/',
71
                    )),
72 1
                )
73
            ))
74
            ->add('block_html', 'textarea', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
75
                'label' => 'ブロックデータ',
76
                'mapped' => false,
77
                'required' => false,
78
                'constraints' => array()
79
            ))
80
            ->add('DeviceType', 'entity', array(
81
                'class' => 'Eccube\Entity\Master\DeviceType',
82
                'property' => 'id',
83
            ))
84 1
            ->add('id', 'hidden')
85 View Code Duplication
            ->addEventListener(FormEvents::POST_SUBMIT, function($event) use ($app) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
86
                $form = $event->getForm();
87
                $file_name = $form['file_name']->getData();
88
                $DeviceType = $form['DeviceType']->getData();
89
                $block_id = $form['id']->getData();
90
91
                $qb = $app['orm.em']->createQueryBuilder();
92
                $qb->select('b')
93
                    ->from('Eccube\\Entity\\Block', 'b')
94
                    ->where('b.file_name = :file_name')
95
                    ->setParameter('file_name', $file_name)
96
                    ->andWhere('b.DeviceType = :DeviceType')
97
                    ->setParameter('DeviceType', $DeviceType);
98
                if (isset($block_id)) {
99
                    $qb
100
                        ->andWhere('b.id <> :block_id')
101
                        ->setParameter('block_id', $block_id);
102
                }
103
104
                $Block = $qb
105
                    ->getQuery()
106
                    ->getResult();
107
                if (count($Block) > 0) {
108
                    $form['file_name']->addError(new FormError('※ 同じファイル名のデータが存在しています。別のファイル名を入力してください。'));
109
                }
110
            });
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function setDefaultOptions(OptionsResolverInterface $resolver)
117
    {
118
        $resolver->setDefaults(array(
119
            'data_class' => 'Eccube\Entity\Block',
120
        ));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getName()
127 285
    {
128
        return 'block';
129 285
    }
130
}
131