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.
Completed
Push — master ( 55e1d8...b3923f )
by Petko
17:15 queued 03:22
created

getFieldsFromEntityMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Petkopara\CrudGeneratorBundle\Generator;
4
5
use Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use Petkopara\CrudGeneratorBundle\Generator\Guesser\MetadataGuesser;
8
use RuntimeException;
9
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator;
10
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
11
12
/**
13
 * Generates a form class based on a Doctrine entity.
14
 *
15
 */
16
class PetkoparaFormGenerator extends DoctrineFormGenerator
17
{
18
19
    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...
20
    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...
21
    private $metadataGuesser;
22
23
    /**
24
     * 
25
     * @param MetadataGuesser $guesser
26
     */
27 1
    public function __construct(MetadataGuesser $guesser)
28
    {
29 1
        $this->metadataGuesser = $guesser;
30 1
    }
31
32
    public function getClassName()
33
    {
34
        return $this->className;
35
    }
36
37
    public function getClassPath()
38
    {
39
        return $this->classPath;
40
    }
41
42
    /**
43
     * Generates the entity form class.
44
     *
45
     * @param BundleInterface   $bundle         The bundle in which to create the class
46
     * @param string            $entity         The entity relative class name
47
     * @param ClassMetadataInfo $metadata       The entity metadata class
48
     * @param bool              $forceOverwrite If true, remove any existing form class before generating it again
49
     */
50 1
    public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false)
51
    {
52 1
        $parts = explode('\\', $entity);
53 1
        $entityClass = array_pop($parts);
54
55 1
        $this->className = $entityClass . 'Type';
56 1
        $dirPath = $bundle->getPath() . '/Form';
57 1
        $this->classPath = $dirPath . '/' . str_replace('\\', '/', $entity) . 'Type.php';
58
59 1 View Code Duplication
        if (!$forceOverwrite && file_exists($this->classPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            throw new RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath));
61
        }
62
63 1
        if (count($metadata->identifier) > 1) {
64
            throw new RuntimeException('The form generator does not support entity classes with multiple primary keys.');
65
        }
66
67 1
        $parts = explode('\\', $entity);
68 1
        array_pop($parts);
69
70 1
        $this->renderFile('form/FormType.php.twig', $this->classPath, array(
71 1
            'fields' => $this->getFieldsFromEntityMetadata($metadata),
72 1
            'fields_associated' => $this->getAssociatedFields($metadata),
73 1
            'fields_mapping' => $metadata->fieldMappings,
74 1
            'namespace' => $bundle->getNamespace(),
75 1
            'entity_namespace' => implode('\\', $parts),
76 1
            'entity_class' => $entityClass,
77 1
            'bundle' => $bundle->getName(),
78 1
            'form_class' => $this->className,
79 1
            'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()) . ($parts ? '_' : '') . implode('_', $parts) . '_' . substr($this->className, 0, -4)),
80
            // Add 'setDefaultOptions' method with deprecated type hint, if the new 'configureOptions' isn't available.
81
            // Required as long as Symfony 2.6 is supported.
82 1
            'configure_options_available' => method_exists('Symfony\Component\Form\AbstractType', 'configureOptions'),
83 1
            'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
84 1
        ));
85 1
    }
86
87
    /**
88
     * Returns an array of fields. Fields can be both column fields and
89
     * association fields.
90
     *
91
     * @param ClassMetadataInfo $metadata
92
     *
93
     * @return array $fields
94
     */
95 1
    private function getFieldsFromEntityMetadata(ClassMetadataInfo $metadata)
96
    {
97 1
        $fields = (array) $metadata->fieldNames;
98
99
        // Remove the primary key field if it's not managed manually
100 1
        if (!$metadata->isIdentifierNatural()) {
101 1
            $fields = array_diff($fields, $metadata->identifier);
102 1
        }
103
104 1
        return $fields;
105
    }
106
107 1
    private function getAssociatedFields(ClassMetadataInfo $metadata)
108
    {
109 1
        $fields = array();
110
111 1
        foreach ($metadata->associationMappings as $fieldName => $relation) {
112
113 1
            switch ($relation['type']) {
114 1
                case ClassMetadataInfo::ONE_TO_ONE:
115 1
                case ClassMetadataInfo::MANY_TO_ONE:
116 1
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "MANY_TO_ONE");
117 1
                    break;
118 1
                case ClassMetadataInfo::MANY_TO_MANY:
119
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "MANY_TO_MANY");
120
                    break;
121 1
                case ClassMetadataInfo::ONE_TO_MANY:
122
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "ONE_TO_MANY");
123
                    break;
124 1
            }
125 1
        }
126
127 1
        return $fields;
128
    }
129
130
    /**
131
     * @param string $relationType
132
     */
133 1
    private function getRelationFieldData($fieldName, $relation, $relationType)
134
    {
135 1
        $field = array();
136 1
        $field['name'] = $fieldName;
137 1
        $field['widget'] = 'EntityType::class';
138 1
        $field['class'] = $relation['targetEntity'];
139 1
        $field['choice_label'] = $this->metadataGuesser->guessChoiceLabelFromClass($relation['targetEntity']);
140 1
        $field['type'] = $relationType;
141 1
        return $field;
142
    }
143
144
}
145