GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PetkoparaFormGenerator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.16%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 126
ccs 55
cts 61
cp 0.9016
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClassName() 0 4 1
A getClassPath() 0 4 1
B generate() 0 36 5
A getFieldsFromEntityMetadata() 0 11 2
B getAssociatedFields() 0 19 6
A getRelationFieldData() 0 10 1
1
<?php
2
3
namespace Petkopara\CrudGeneratorBundle\Generator;
4
5
use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
use Petkopara\CrudGeneratorBundle\Generator\Guesser\MetadataGuesser;
7
use RuntimeException;
8
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator;
9
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
10
11
/**
12
 * Generates a form class based on a Doctrine entity.
13
 *
14
 */
15
class PetkoparaFormGenerator extends DoctrineFormGenerator
16
{
17
18
    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...
19
    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...
20
    private $metadataGuesser;
21
22
    /**
23
     * 
24
     * @param MetadataGuesser $guesser
25
     */
26 1
    public function __construct(MetadataGuesser $guesser)
27
    {
28 1
        $this->metadataGuesser = $guesser;
29 1
    }
30
31
    public function getClassName()
32
    {
33
        return $this->className;
34
    }
35
36
    public function getClassPath()
37
    {
38
        return $this->classPath;
39
    }
40
41
    /**
42
     * Generates the entity form class.
43
     *
44
     * @param BundleInterface   $bundle         The bundle in which to create the class
45
     * @param string            $entity         The entity relative class name
46
     * @param ClassMetadataInfo $metadata       The entity metadata class
47
     * @param bool              $forceOverwrite If true, remove any existing form class before generating it again
48
     */
49 1
    public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false)
50
    {
51 1
        $parts = explode('\\', $entity);
52 1
        $entityClass = array_pop($parts);
53
54 1
        $this->className = $entityClass . 'Type';
55 1
        $dirPath = $bundle->getPath() . '/Form';
56 1
        $this->classPath = $dirPath . '/' . str_replace('\\', '/', $entity) . 'Type.php';
57
58 1
        if (!$forceOverwrite && file_exists($this->classPath)) {
59
            throw new RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath));
60
        }
61
62 1
        if (count($metadata->identifier) > 1) {
63
            throw new RuntimeException('The form generator does not support entity classes with multiple primary keys.');
64
        }
65
66 1
        $parts = explode('\\', $entity);
67 1
        array_pop($parts);
68
69 1
        $this->renderFile('form/FormType.php.twig', $this->classPath, array(
70 1
            'fields' => $this->getFieldsFromEntityMetadata($metadata),
71 1
            'fields_associated' => $this->getAssociatedFields($metadata),
72 1
            'fields_mapping' => $metadata->fieldMappings,
73 1
            'namespace' => $bundle->getNamespace(),
74 1
            'entity_namespace' => implode('\\', $parts),
75 1
            'entity_class' => $entityClass,
76 1
            'bundle' => $bundle->getName(),
77 1
            'form_class' => $this->className,
78 1
            'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()) . ($parts ? '_' : '') . implode('_', $parts) . '_' . substr($this->className, 0, -4)),
79
            // Add 'setDefaultOptions' method with deprecated type hint, if the new 'configureOptions' isn't available.
80
            // Required as long as Symfony 2.6 is supported.
81 1
            'configure_options_available' => method_exists('Symfony\Component\Form\AbstractType', 'configureOptions'),
82 1
            'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
83 1
        ));
84 1
    }
85
86
    /**
87
     * Returns an array of fields. Fields can be both column fields and
88
     * association fields.
89
     *
90
     * @param ClassMetadataInfo $metadata
91
     *
92
     * @return array $fields
93
     */
94 1
    private function getFieldsFromEntityMetadata(ClassMetadataInfo $metadata)
95
    {
96 1
        $fields = (array) $metadata->fieldNames;
97
98
        // Remove the primary key field if it's not managed manually
99 1
        if (!$metadata->isIdentifierNatural()) {
100 1
            $fields = array_diff($fields, $metadata->identifier);
101 1
        }
102
103 1
        return $fields;
104
    }
105
106 1
    private function getAssociatedFields(ClassMetadataInfo $metadata)
107
    {
108 1
        $fields = array();
109
110 1
        foreach ($metadata->associationMappings as $fieldName => $relation) {
111
112 1
            switch ($relation['type']) {
113 1
                case ClassMetadataInfo::ONE_TO_ONE:
114 1
                case ClassMetadataInfo::MANY_TO_ONE:
115 1
                case ClassMetadataInfo::MANY_TO_MANY:
116 1
                    if ($relation['isOwningSide']) {
117 1
                        $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, $relation['type']);
118 1
                    }
119 1
                    break;
120 1
            }
121 1
        }
122
123 1
        return $fields;
124
    }
125
126
    /**
127
     * @param string $relationType
128
     */
129 1
    private function getRelationFieldData($fieldName, $relation, $relationType)
130
    {
131 1
        $field = array();
132 1
        $field['name'] = $fieldName;
133 1
        $field['widget'] = 'EntityType::class';
134 1
        $field['class'] = $relation['targetEntity'];
135 1
        $field['choice_label'] = $this->metadataGuesser->guessChoiceLabelFromClass($relation['targetEntity']);
136 1
        $field['type'] = $relationType;
137 1
        return $field;
138
    }
139
140
}
141