ControllerGenerator::generate()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

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