TemplateController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 13
c 5
b 1
f 3
lcom 1
cbo 3
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B generateAction() 0 30 3
A getTemplatePath() 0 13 4
B templateApply() 0 34 6
1
<?php
2
namespace Newage\Generators\Controller;
3
4
use Zend\Mvc\Controller\AbstractActionController;
5
use Zend\Console\ColorInterface as Color;
6
use Zend\Console\Prompt;
7
8
class TemplateController extends AbstractActionController
9
{
10
    public function generateAction()
11
    {
12
        $request = $this->getRequest();
13
14
        $templateName = $request->getParam('template');
15
        $destination = getcwd() . $request->getParam('destination');
16
        $variables['NAMESPACE'] = ucfirst($request->getParam('namespace'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$variables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $variables = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
17
        $variables['NAME'] = ucfirst($request->getParam('name'));
18
19
        $config = $this->getServiceLocator()->get('config');
20
        $console = $this->getServiceLocator()->get('console');
21
        if (!isset($config['generators']['templates'][$templateName])) {
22
            $console->writeLine('Template not exists: '.$templateName, color::RED);
23
            return;
24
        }
25
26
        if (!is_dir($destination)) {
27
            $console->writeLine('Directory not exists: '.$destination, color::RED);
28
            return;
29
        }
30
31
        $templates = $config['generators']['templates'];
32
        $this->getTemplatePath(
33
            $templateName,
34
            $templates,
35
            $console,
36
            $destination,
37
            $variables
38
        );
39
    }
40
41
    protected function getTemplatePath($templateName, $templates, $console, $destination, $variables)
42
    {
43
        if (array_key_exists($templateName, $templates)) {
44
            $templateName = $templates[$templateName];
45
        }
46
        if (is_array($templateName)) {
47
            foreach($templateName as $oneTemplate) {
48
                $this->getTemplatePath($oneTemplate, $templates, $console, $destination, $variables);
49
            }
50
        } else {
51
            $this->templateApply($templateName, $console, $destination, $variables);
52
        }
53
    }
54
55
    /**
56
     * @param $templateFromConfig
57
     * @param $console
58
     * @param $destination
59
     * @param $variables
60
     */
61
    protected function templateApply($templateFromConfig, $console, $destination, $variables)
62
    {
63
        $config = $this->getServiceLocator()->get('config');
64
65
        $templatePath = getcwd() . $config['generators']['path'] . $templateFromConfig;
66
        if (!file_exists($templatePath)) {
67
            $console->writeLine('Template file not exists: ' . $templatePath, color::RED);
68
            return;
69
        }
70
71
        $template = file_get_contents($templatePath);
72
        $destination .= '/' . substr($templateFromConfig, 0, -4) . 'php';
73
        foreach ($variables as $variableName => $variableValue) {
74
            $template = str_replace('$' . $variableName . '$', $variableValue, $template);
75
            //lower case variable
76
            $template = str_replace('&' . $variableName . '&', lcfirst($variableValue), $template);
77
            $destination = str_replace('$' . $variableName . '$', $variableValue, $destination);
78
        }
79
80
        $destinationDir = dirname($destination);
81
        if (!is_dir($destinationDir)) {
82
            mkdir($destinationDir, 0777, true);
83
        }
84
85
        $rewrite = 'y';
86
        if (file_exists($destination)) {
87
            $rewrite = Prompt\Confirm::prompt('File is exists. Are you want to rewrite file? [y/n]', 'y', 'n');
88
        }
89
90
        if ($rewrite == 'y') {
91
            file_put_contents($destination, $template);
92
            $console->writeLine('The class generated: ' . $destination, color::GREEN);
93
        }
94
    }
95
}
96