Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

ControllerGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 64.1%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 6
c 7
b 1
f 1
lcom 1
cbo 4
dl 0
loc 74
ccs 25
cts 39
cp 0.641
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareData() 0 11 1
B generate() 0 48 5
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
use Rougin\Combustor\Common\Inflector;
7
8
/**
9
 * Controller Generator
10
 *
11
 * Generates CodeIgniter-based controllers.
12
 * 
13
 * @package Combustor
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 */
16
class ControllerGenerator extends BaseGenerator implements GeneratorInterface
17
{
18
    /**
19
     * Prepares the data before generation.
20
     * 
21
     * @param  array &$data
22
     * @return void
23
     */
24 9
    public function prepareData(array &$data)
25
    {
26 9
        $data['camel'] = [];
27 9
        $data['columns'] = [];
28 9
        $data['dropdowns'] = [];
29 9
        $data['foreignKeys'] = [];
30 9
        $data['models'] = [$data['name']];
31 9
        $data['plural'] = Inflector::plural($data['name']);
32 9
        $data['singular'] = Inflector::singular($data['name']);
33 9
        $data['underscore'] = [];
34 9
    }
35
36
    /**
37
     * Generates set of code based on data.
38
     * 
39
     * @return array
40
     */
41 9
    public function generate()
42
    {
43 9
        $this->prepareData($this->data);
44
45 9
        $columnFields = ['name', 'description', 'label'];
46
47 9
        $table = $this->describe->getTable($this->data['name']);
48
49 9
        foreach ($table as $column) {
50 9
            if ($column->isAutoIncrement()) {
51 9
                continue;
52
            }
53
54 9
            $field = strtolower($column->getField());
55 9
            $method = 'set_'.$field;
56
57 9
            $this->data['camel'][$field] = lcfirst(Inflector::camelize($method));
58 9
            $this->data['underscore'][$field] = Inflector::underscore($method);
59
60 9
            array_push($this->data['columns'], $field);
61
62 9
            if ($column->isForeignKey()) {
63
                $referencedTable = Tools::stripTableSchema(
64
                    $column->getReferencedTable()
65
                );
66
67
                $this->data['foreignKeys'][$field] = $referencedTable;
68
69
                array_push($this->data['models'], $referencedTable);
70
71
                $dropdown = [
72
                    'list' => plural($referencedTable),
73
                    'table' => $referencedTable,
74
                    'field' => $field
75
                ];
76
77
                if ( ! in_array($field, $columnFields)) {
78
                    $dropdown['field'] = $this->describe->getPrimaryKey(
79
                        $referencedTable
80
                    );
81
                }
82
83
                array_push($this->data['dropdowns'], $dropdown);
84
            }
85 9
        }
86
87 9
        return $this->data;
88
    }
89
}
90