Failed Conditions
Pull Request — experimental/3.1 (#2268)
by chihiro
68:29 queued 15:11
created

DoctrineOrmExtension::buildForm()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 1
nop 2
dl 0
loc 39
rs 6.7272
c 0
b 0
f 0
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
namespace Eccube\Form\Extension;
24
25
use Eccube\Application;
26
use Symfony\Component\Form\AbstractTypeExtension;
27
use Symfony\Component\Form\Extension\Core\Type\FormType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\Form\FormEvent;
30
use Symfony\Component\Form\FormEvents;
31
32
class DoctrineOrmExtension extends AbstractTypeExtension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function buildForm(FormBuilderInterface $builder, array $options)
38
    {
39
        $builder->addEventListener(
40
            FormEvents::PRE_SET_DATA,
41
            function (FormEvent $event) {
42
                $form = $event->getForm();
43
                $config = $form->getConfig();
44
                // data_classオプションが必要
45
                $class = $config->getDataClass();
46
                if (is_null($class)) {
47
                    return;
48
                }
49
                // Entityクラスのみ対象とする
50
                $ref = new \ReflectionClass($class);
51
                if ("Eccube\Entity\AbstractEntity" !== $ref->getParentClass()->getName()) {
52
                    return;
53
                }
54
                // TODO コンストラクタへ移動
55
                $app = Application::getInstance();
56
                $em = $app['orm.em'];
57
                // メタデータの取得
58
                $meta = $em->getClassMetadata($class);
59
                // フィールドからフォームへ定義
60
                $names = $meta->getFieldNames();
61
                foreach ($names as $name) {
62
                    $mapping = $meta->getFieldMapping($name);
63
                    // eccube_form_renderがtrueのときに実行
64
                    // -> @ORM\Column(..., options={"eccube_form_render": true})
65
                    if (isset($mapping['options']['eccube_form_render']) && $mapping['options']['eccube_form_render']) {
66
                        $fieldName = $mapping['fieldName'];
67
                        if (!isset($form[$fieldName])) {
68
                            $form->add($mapping['fieldName']);
69
                        }
70
                    }
71
                }
72
                // TODO Assosiationも対応する
73
            }
74
        );
75
    }
76
77
    public function getExtendedType()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
78
    {
79
        return FormType::class;
80
    }
81
}
82