Passed
Push — master ( 5c38aa...9764b6 )
by Rougin
04:16
created

ControllerGenerator::prepareData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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