Completed
Push — wip-platform ( 1b7118...2b724b )
by
unknown
03:32
created

ControllerGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 7
Ratio 30.43 %

Importance

Changes 0
Metric Value
dl 7
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\Generator;
14
15
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
16
use Sonata\AdminBundle\Generator\ControllerGenerator as BaseControllerGenerator;
17
18
/**
19
 * Class ControllerGenerator.
20
 */
21
class ControllerGenerator extends BaseControllerGenerator
22
{
23
    /**
24
     * @var string|null
25
     */
26
    private $class;
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...
27
28
    /**
29
     * @var string|null
30
     */
31
    private $file;
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...
32
33
    /**
34
     * @param array|string $skeletonDirectory
35
     */
36
    public function __construct($skeletonDirectory)
37
    {
38
        $this->setSkeletonDirs($skeletonDirectory);
0 ignored issues
show
Bug introduced by
It seems like $skeletonDirectory defined by parameter $skeletonDirectory on line 36 can also be of type string; however, Sensio\Bundle\GeneratorB...ator::setSkeletonDirs() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
39
    }
40
41
    /**
42
     * @param BundleInterface $bundle
43
     * @param string          $controllerClassBasename
44
     *
45
     * @throws \RuntimeException
46
     */
47
    public function generate(BundleInterface $bundle, $controllerClassBasename)
48
    {
49
        $this->class = sprintf('%s\Controller\%s', $bundle->getNamespace(), $controllerClassBasename);
50
        $this->file = sprintf(
51
            '%s/Controller/%s.php',
52
            $bundle->getPath(),
53
            str_replace('\\', '/', $controllerClassBasename)
54
        );
55
        $parts = explode('\\', $this->class);
56
57 View Code Duplication
        if (file_exists($this->file)) {
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...
58
            throw new \RuntimeException(sprintf(
59
                'Unable to generate the admin controller class "%s". The file "%s" already exists.',
60
                $this->class,
61
                realpath($this->file)
62
            ));
63
        }
64
65
        $this->renderFile('AdminController.php.twig', $this->file, array(
66
            'classBasename' => array_pop($parts),
67
            'namespace'     => implode('\\', $parts),
68
        ));
69
    }
70
71
    /**
72
     * @return string|null
73
     */
74
    public function getClass()
75
    {
76
        return $this->class;
77
    }
78
79
    /**
80
     * @return string|null
81
     */
82
    public function getFile()
83
    {
84
        return $this->file;
85
    }
86
}
87