RestControllerGenerator::setEntityName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Tpg\ExtjsBundle\Generator;
3
4
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
5
use Sensio\Bundle\GeneratorBundle\Generator\ControllerGenerator;
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
use Symfony\Component\Yaml\Yaml;
8
9
class RestControllerGenerator extends ControllerGenerator {
10
11
    protected $entityName;
12
    protected $trait = false;
13
    protected $mongo = false;
14
    protected $template = 'Controller.php';
15
16
    /**
17
     * @var BundleInterface $entityBundle
18
     */
19
    protected $entityBundle;
20
21 2
    public function setEntityName($name) {
22 2
        $this->entityName = $name;
23 2
    }
24 2
    public function setEntityBundle(BundleInterface $bundle) {
25 2
        $this->entityBundle = $bundle;
26 2
    }
27
28
    public function setUseTrait($flag) {
29
        $this->trait = $flag;
30
    }
31
32
    public function setTemplateFile($file) {
33
        $this->template = $file;
34
    }
35
36 1
    public function setMongo($mongo) {
37 1
        $this->mongo = $mongo;
38 1
    }
39
40 2
    public function generate(BundleInterface $bundle, $controller, $routeFormat, $templateFormat, array $actions = array())
41
    {
42 2
        $dir = $bundle->getPath();
43 2
        $controllerFile = $dir.'/Controller/';
44 2
        if ($this->trait) {
45
            $controllerFile .= 'Generated/';
46
            @mkdir($controllerFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
47
        }
48 2
        $controllerFile .= $controller.'Controller.php';
49
50 2
        if (file_exists($controllerFile) && !$this->trait) {
51
            throw new \RuntimeException(sprintf('Controller "%s" already exists', $controllerFile));
52
        }
53
54 2
        if ($this->mongo) {
55 1
            $entityClass = $this->entityBundle->getNamespace().'\\Document\\'.$this->entityName;
56 1
        } else {
57 1
            $entityClass = $this->entityBundle->getNamespace().'\\Entity\\'.$this->entityName;
58
        }
59
60 2
        $tmpEntity = explode('/', $this->entityName);
61
62
        $parameters = array(
63 2
            'mongo'      => $this->mongo,
64 2
            'trait'      => $this->trait,
65 2
            'namespace'  => $bundle->getNamespace(),
66 2
            'bundle'     => $bundle->getName(),
67 2
            "manager"    => ($this->mongo===true)?"doctrine_mongodb.odm.default_document_manager":"doctrine.orm.default_entity_manager",
68 2
            'serializer' => ($this->mongo===true)?"tpg_extjs.odm_serializer":"tpg_extjs.orm_serializer",
69 2
            'controller'        => $controller,
70 2
            'entity_class'      => $entityClass,
71 2
            'entity_name'       => str_replace(array("/","\\"), "_", $this->entityName),
72 2
            'entity_bundle'     => $this->entityBundle->getName(),
73 2
            'entity'            => array_pop($tmpEntity),
74 2
            'route_name_prefix' => strtolower(preg_replace('/([A-Z])/', '_\\1', $bundle->getName().'_api_'))
75 2
        );
76
77 2
        $this->generateRestRouting($bundle, $controller);
78
79 2
        $this->renderFile('controller/'.$this->template, $controllerFile, $parameters);
80 2
    }
81
82 2
    public function generateRestRouting(BundleInterface $bundle, $controller)
83
    {
84 2
        $file = $bundle->getPath().'/Resources/config/routing.rest.yml';
85 2
        if (file_exists($file)) {
86
            $content = file_get_contents($file);
87 2
        } elseif (!is_dir($dir = $bundle->getPath().'/Resources/config')) {
88 1
            mkdir($dir);
89 1
        }
90
91 2
        $resource = $bundle->getNamespace()."\\Controller\\".$controller.'Controller';
92 2
        $name = strtolower(preg_replace('/([A-Z])/', '_\\1', $bundle->getName().$controller.'_rest'));
93 2
        $name_prefix = strtolower(preg_replace('/([A-Z])/', '_\\1', $bundle->getName().'_api_'));
94
95
96 2
        if (!isset($content)) {
97 2
            $content = '';
98 2
        } else {
99
            $yml = new Yaml();
100
            $route = $yml->parse($content);
101
            if (isset($route[$name])) {
102
                return false;
103
            }
104
        }
105
106 2
        $content .= sprintf(
107 2
            "\n%s:\n    type: rest\n    resource: %s\n    name_prefix: %s\n",
108 2
            $name,
109 2
            $resource,
110
            $name_prefix
111 2
        );
112
113 2
        $flink = fopen($file, 'w');
114 2
        if ($flink) {
115 2
            $write = fwrite($flink, $content);
116
117 2
            if ($write) {
118 2
                fclose($flink);
119 2
            } else {
120
                throw new \RunTimeException(sprintf('We cannot write into file "%s", has that file the correct access level?', $file));
121
            }
122 2
        } else {
123
            throw new \RunTimeException(sprintf('Problems with generating file "%s", did you gave write access to that directory?', $file));
124
        }
125
    }
126
}