Failed Conditions
Push — experimental/3.1 ( 2823d3...342757 )
by Yangsin
85:33
created

DoctrineOrmExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
rs 10
wmc 9
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C buildForm() 0 35 7
A getExtendedType() 0 4 1
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 Doctrine\ORM\EntityManager;
26
use Eccube\Application;
27
use Symfony\Component\Form\AbstractTypeExtension;
28
use Symfony\Component\Form\Extension\Core\Type\FormType;
29
use Symfony\Component\Form\FormBuilderInterface;
30
use Symfony\Component\Form\FormEvent;
31
use Symfony\Component\Form\FormEvents;
32
33
class DoctrineOrmExtension extends AbstractTypeExtension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
34
{
35
    /**
36
     * @var EntityManager
37
     */
38
    protected $em;
39
40
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
introduced by
Missing function doc comment
Loading history...
41
    {
42
        $this->em = $em;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function buildForm(FormBuilderInterface $builder, array $options)
49
    {
50
        $builder->addEventListener(
51
            FormEvents::PRE_SET_DATA,
52
            function (FormEvent $event) {
53
                $form = $event->getForm();
54
                $config = $form->getConfig();
55
                // data_classオプションが必要
56
                $class = $config->getDataClass();
57
                if (is_null($class)) {
58
                    return;
59
                }
60
                // メタデータの取得
61
                try {
62
                    $meta = $this->em->getClassMetadata($class);
63
                } catch (\Exception $e) {
64
                    return;
65
                }
66
                // フィールドからフォームへ定義
67
                $names = $meta->getFieldNames();
68
                foreach ($names as $name) {
69
                    $mapping = $meta->getFieldMapping($name);
70
                    // eccube_form_renderがtrueのときに実行
71
                    // -> @ORM\Column(..., options={"eccube_form_render": true})
72
                    if (isset($mapping['options']['eccube_form_render']) && $mapping['options']['eccube_form_render']) {
73
                        $fieldName = $mapping['fieldName'];
74
                        if (!isset($form[$fieldName])) {
75
                            $form->add($mapping['fieldName']);
76
                        }
77
                    }
78
                }
79
                // TODO Assosiationも対応する
80
            }
81
        );
82
    }
83
84
    public function getExtendedType()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
85
    {
86
        return FormType::class;
87
    }
88
}
89