GenFormGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 13
c 3
b 1
f 2
lcom 1
cbo 4
dl 0
loc 95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClassName() 0 4 1
A getClassPath() 0 4 1
A getFieldFormatter() 0 4 1
A getFieldsFromMetadata() 0 17 4
B generate() 0 40 5
1
<?php
2
3
namespace Kpicaza\GenBundle\Generator;
4
5
use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
use Kpicaza\GenBundle\Formatter\FieldFormatter;
7
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
10
11
class GenFormGenerator extends DoctrineFormGenerator
12
{
13
    private $className;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
    private $classPath;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param Filesystem $filesystem A Filesystem instance
20
     */
21
    public function __construct(Filesystem $filesystem)
22
    {
23
        parent::__construct($filesystem);
24
    }
25
26
    public function getClassName()
27
    {
28
        return $this->className;
29
    }
30
31
    public function getClassPath()
32
    {
33
        return $this->classPath;
34
    }
35
36
    public function getFieldFormatter()
37
    {
38
        return new FieldFormatter();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false)
45
    {
46
        $parts = explode('\\', $entity);
47
        $entityClass = array_pop($parts);
48
49
        $this->className = $entityClass.'Type';
50
        $dirPath = $bundle->getPath().'/Form';
51
        $this->classPath = $dirPath.'/'.str_replace('\\', '/', $entity).'Type.php';
52
53
        if (!$forceOverwrite && file_exists($this->classPath)) {
54
            throw new \RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath));
55
        }
56
57
        if (count($metadata->identifier) > 1) {
58
            throw new \RuntimeException('The form generator does not support entity classes with multiple primary keys.');
59
        }
60
61
        $parts = explode('\\', $entity);
62
        array_pop($parts);
63
64
        $formatter = $this->getFieldFormatter();
65
        $mappings = $formatter->formatMappings($metadata->fieldMappings);
66
67
        $this->renderFile('form/FormType.php.twig', $this->classPath, array(
68
            'fields' => $this->getFieldsFromMetadata($metadata),
69
            'fields_mapping' => $mappings,
70
            'metadata' => $metadata->fieldMappings,
71
            'namespace' => $bundle->getNamespace(),
72
            'entity_namespace' => implode('\\', $parts),
73
            'entity_class' => $entityClass,
74
            'bundle' => $bundle->getName(),
75
            'form_class' => $this->className,
76
            'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.substr($this->className, 0, -4)),
77
78
            // Add 'setDefaultOptions' method with deprecated type hint, if the new 'configureOptions' isn't available.
79
            // Required as long as Symfony 2.6 is supported.
80
            'configure_options_available' => method_exists('Symfony\Component\Form\AbstractType', 'configureOptions'),
81
            'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
82
        ));
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
89
    {
90
        $fields = (array) $metadata->fieldNames;
91
92
        // Remove the primary key field if it's not managed manually
93
        if (!$metadata->isIdentifierNatural()) {
94
            $fields = array_diff($fields, $metadata->identifier);
95
        }
96
97
        foreach ($metadata->associationMappings as $fieldName => $relation) {
98
            if ($relation['type'] !== ClassMetadataInfo::ONE_TO_MANY) {
99
                $fields[] = sprintf('%s_%s', $fieldName, 'id');
100
            }
101
        }
102
103
        return $fields;
104
    }
105
}
106