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.
Passed
Pull Request — master (#49)
by
unknown
02:09
created

PetkoparaCrudGenerator::getTwigEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Petkopara\CrudGeneratorBundle\Generator;
4
5
use Doctrine\Common\Util\Inflector;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use Petkopara\CrudGeneratorBundle\Configuration\Configuration;
8
use Petkopara\CrudGeneratorBundle\Twig\CrudTemplateExtension;
9
use RuntimeException;
10
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
11
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
12
use Twig_Loader_Filesystem;
13
14
class PetkoparaCrudGenerator extends DoctrineCrudGenerator
15
{
16
17
    protected $formFilterGenerator;
18
    protected $config;
19
20
    /**
21
     * 
22
     * @param BundleInterface $bundle
23
     * @param string $entity
24
     * @param ClassMetadataInfo $metadata
25
     * @param Configuration $config
26
     * @throws RuntimeException
27
     */
28 6
    public function generateCrud(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, Configuration $config)
29
    {
30 6
        $this->config = $config;
31 6
        $this->routeNamePrefix = self::getRouteNamePrefix($config->getRoutePrefix());
32 6
        $this->actions = $config->getCrudActions();
33
34 6
        if (count($metadata->identifier) != 1) {
35
            throw new RuntimeException('The CRUD generator does not support entity classes with multiple or no primary keys.');
36
        }
37
38 6
        $this->entity = $entity;
39 6
        $this->entitySingularized = lcfirst(Inflector::singularize($entity));
40 6
        $this->entityPluralized = lcfirst(Inflector::pluralize($entity));
41 6
        $this->bundle = $bundle;
42 6
        $this->metadata = $metadata;
43
44 6
        $this->setFormat($config->getFormat());
45
46
47
        //define where to save the view files
48 6
        if (!$config->getBundleViews()) { //save in root Resources
49 6
            $dir = sprintf('%s/Resources/views/%s', $this->rootDir, str_replace('\\', '/', strtolower($this->entity)));
50 6
        } else { //save in bundle Resources
51
            $dir = sprintf('%s/Resources/views/%s', $bundle->getPath(), str_replace('\\', '/', $this->entity));
52
        }
53
54 6
        $this->generateCrudControllerClass();
55
56 6
        if (!file_exists($dir)) {
57 6
            $this->filesystem->mkdir($dir, 0777);
58 6
        }
59
60 6
        $this->generateIndexView($dir);
61
62 6
        if (in_array('show', $this->actions)) {
63 6
            $this->generateShowView($dir);
64 6
        }
65
66 6
        if (in_array('new', $this->actions)) {
67 6
            $this->generateNewView($dir);
68 6
        }
69
70 6
        if (in_array('edit', $this->actions)) {
71 6
            $this->generateEditView($dir);
72 6
        }
73
74 6
        $this->generateTestClass();
75 6
        $this->generateConfiguration();
76 6
    }
77
78
    /**
79
     * Generates the index.html.twig template in the final bundle.
80
     *
81
     * @param string $dir The path to the folder that hosts templates in the bundle
82
     */
83 6
    protected function generateIndexView($dir)
84
    {
85 6
        $this->renderFile('crud/views/index.html.twig.twig', $dir . '/index.html.twig', array(
86 6
            'bundle' => $this->bundle->getName(),
87 6
            'entity' => $this->entity,
88 6
            'entity_pluralized' => $this->entityPluralized,
89 6
            'entity_singularized' => $this->entitySingularized,
90 6
            'identifier' => $this->metadata->identifier[0],
91 6
            'fields' => $this->metadata->fieldMappings,
92 6
            'actions' => $this->actions,
93 6
            'record_actions' => $this->getRecordActions(),
94 6
            'route_prefix' => $this->config->getRoutePrefix(),
95 6
            'route_name_prefix' => $this->routeNamePrefix,
96 6
            'base_template' => $this->config->getBaseTemplate(),
97 6
            'without_bulk_action' => $this->config->getWithoutBulk(),
98 6
            'without_sorting' => $this->config->getWithoutSorting(),
99 6
            'without_page_size' => $this->config->getWithoutPageSize(),
100 6
            'filter_type' => $this->config->getFilterType(),
101 6
        ));
102 6
    }
103
104
    /**
105
     * Generates the show.html.twig template in the final bundle.
106
     *
107
     * @param string $dir The path to the folder that hosts templates in the bundle
108
     */
109 6 View Code Duplication
    protected function generateShowView($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
110
    {
111 6
        $this->renderFile('crud/views/show.html.twig.twig', $dir . '/show.html.twig', array(
112 6
            'bundle' => $this->bundle->getName(),
113 6
            'entity' => $this->entity,
114 6
            'entity_singularized' => $this->entitySingularized,
115 6
            'identifier' => $this->metadata->identifier[0],
116 6
            'fields' => $this->metadata->fieldMappings,
117 6
            'actions' => $this->actions,
118 6
            'route_prefix' => $this->config->getRoutePrefix(),
119 6
            'route_name_prefix' => $this->routeNamePrefix,
120 6
            'base_template' => $this->config->getBaseTemplate(),
121 6
        ));
122 6
    }
123
124
    /**
125
     * Generates the new.html.twig template in the final bundle.
126
     *
127
     * @param string $dir The path to the folder that hosts templates in the bundle
128
     */
129 6
    protected function generateNewView($dir)
130
    {
131 6
        $this->renderFile('crud/views/new.html.twig.twig', $dir . '/new.html.twig', array(
132 6
            'bundle' => $this->bundle->getName(),
133 6
            'entity' => $this->entity,
134 6
            'entity_singularized' => $this->entitySingularized,
135 6
            'route_prefix' => $this->config->getRoutePrefix(),
136 6
            'route_name_prefix' => $this->routeNamePrefix,
137 6
            'actions' => $this->actions,
138 6
            'fields' => $this->metadata->fieldMappings,
139 6
            'base_template' => $this->config->getBaseTemplate(),
140 6
        ));
141 6
    }
142
143
    /**
144
     * Generates the edit.html.twig template in the final bundle.
145
     *
146
     * @param string $dir The path to the folder that hosts templates in the bundle
147
     */
148 6 View Code Duplication
    protected function generateEditView($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
149
    {
150 6
        $this->renderFile('crud/views/edit.html.twig.twig', $dir . '/edit.html.twig', array(
151 6
            'route_prefix' => $this->config->getRoutePrefix(),
152 6
            'route_name_prefix' => $this->routeNamePrefix,
153 6
            'identifier' => $this->metadata->identifier[0],
154 6
            'entity' => $this->entity,
155 6
            'entity_singularized' => $this->entitySingularized,
156 6
            'fields' => $this->metadata->fieldMappings,
157 6
            'bundle' => $this->bundle->getName(),
158 6
            'actions' => $this->actions,
159 6
            'base_template' => $this->config->getBaseTemplate(),
160 6
        ));
161 6
        }
162
163
        /**
164
         * Generates the controller class only.
165
         */
166 6
        protected function generateCrudControllerClass() {
167 6
        $dir = $this->bundle->getPath();
168
169 6
        $parts = explode('\\', $this->entity);
170 6
        $entityClass = array_pop($parts);
171 6
        $entityNamespace = implode('\\', $parts);
172
173 6
        $target = sprintf(
174 6
                '%s/Controller/%s/%sController.php', $dir, str_replace('\\', '/', $entityNamespace), $entityClass
175 6
        );
176
177 6
        if (!$this->config->getOverwrite() && file_exists($target)) {
178
            throw new \RuntimeException('Unable to generate the controller as it already exists.');
179
        }
180
181 6
        $this->renderFile('crud/controller.php.twig', $target, array(
182 6
            'actions' => $this->actions,
183 6
            'route_prefix' => $this->config->getRoutePrefix(),
184 6
            'route_name_prefix' => $this->routeNamePrefix,
185 6
            'bundle' => $this->bundle->getName(),
186 6
            'entity' => $this->entity,
187 6
            'entity_singularized' => $this->entitySingularized,
188 6
            'entity_pluralized' => $this->entityPluralized,
189 6
            'entity_class' => $entityClass,
190 6
            'namespace' => $this->bundle->getNamespace(),
191 6
            'entity_namespace' => $entityNamespace,
192 6
            'format' => $this->config->getFormat(),
193 6
            'bundle_views' => $this->config->getBundleViews(),
194 6
            'filter_type' => $this->config->getFilterTYpe(),
195 6
            'without_sorting' => $this->config->getWithoutSorting(),
196 6
            'without_page_size' => $this->config->getWithoutPageSize(),
197 6
            'identifier' => $this->metadata->identifier[0],
198
199 6
        ));
200 6
    }
201
202 6
    protected function getTwigEnvironment()
203
    {
204 6
        $twig = parent::getTwigEnvironment();
205 6
        $twig->addExtension(new CrudTemplateExtension());
206
        /** @var Twig_Loader_Filesystem $loader */
207 6
        $loader = $twig->getLoader();
208 6
        $loader->addPath(dirname(__DIR__) . '/Resources/views');
209 6
        return $twig;
210
    }
211
212
213
}
214