Passed
Push — master ( 2ab272...5c38aa )
by Rougin
02:42
created

ControllerGenerator::prepareData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 Royce 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
    public function prepareData(array &$data)
24
    {
25
        $data['camel'] = [];
26
        $data['columns'] = [];
27
        $data['dropdowns'] = [];
28
        $data['foreignKeys'] = [];
29
        $data['models'] = [$data['name']];
30
        $data['plural'] = plural($data['name']);
31
        $data['singular'] = singular($data['name']);
32
        $data['underscore'] = [];
33
    }
34
35
    /**
36
     * Generates set of code based on data.
37
     *
38
     * @return array
39
     */
40
    public function generate()
41
    {
42
        $this->prepareData($this->data);
43
44
        $columnFields = ['name', 'description', 'label'];
45
46
        $table = $this->describe->getTable($this->data['name']);
47
48
        foreach ($table as $column) {
49
            if ($column->isAutoIncrement()) {
50
                continue;
51
            }
52
53
            $field = strtolower($column->getField());
54
            $method = 'set_' . $field;
55
56
            $this->data['camel'][$field] = lcfirst(camelize($method));
57
            $this->data['underscore'][$field] = underscore($method);
58
59
            array_push($this->data['columns'], $field);
60
61
            if ($column->isForeignKey()) {
62
                $referencedTable = Tools::stripTableSchema(
63
                    $column->getReferencedTable()
64
                );
65
66
                $this->data['foreignKeys'][$field] = $referencedTable;
67
68
                array_push($this->data['models'], $referencedTable);
69
70
                $dropdown = [
71
                    'list' => plural($referencedTable),
72
                    'table' => $referencedTable,
73
                    'field' => $field
74
                ];
75
76
                if (! in_array($field, $columnFields)) {
77
                    $field = $this->describe->getPrimaryKey($referencedTable);
78
79
                    $dropdown['field'] = $field;
80
                }
81
82
                array_push($this->data['dropdowns'], $dropdown);
83
            }
84
        }
85
86
        return $this->data;
87
    }
88
}
89