Completed
Push — master ( f3af33...267f53 )
by Rougin
03:52
created

ViewGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 67.74%

Importance

Changes 11
Bugs 2 Features 2
Metric Value
wmc 8
c 11
b 2
f 2
lcom 1
cbo 3
dl 0
loc 105
ccs 42
cts 62
cp 0.6774
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareData() 0 50 5
B generate() 0 40 3
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
7
/**
8
 * View Generator
9
 *
10
 * Generates CodeIgniter-based views.
11
 * 
12
 * @package Combustor
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class ViewGenerator extends BaseGenerator implements GeneratorInterface
16
{
17
    /**
18
     * Prepares the data before generation.
19
     * 
20
     * @param  array $data
21
     * @return void
22
     */
23 6
    public function prepareData(array &$data)
24
    {
25
        $bootstrap = [
26 6
            'button' => 'btn btn-default',
27 6
            'buttonPrimary' => 'btn btn-primary',
28 6
            'formControl' => 'form-control',
29 6
            'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12',
30 6
            'label' => 'control-label',
31 6
            'table' => 'table table table-striped table-hover',
32
            'textRight' => 'text-right'
33 6
        ];
34
35 6
        if ($data['isBootstrap']) {
36 6
            $data['bootstrap'] = $bootstrap;
37 6
        }
38
39 6
        $data['camel'] = [];
40 6
        $data['underscore'] = [];
41 6
        $data['foreignKeys'] = [];
42 6
        $data['primaryKeys'] = [];
43
44 6
        $data['plural'] = plural($data['name']);
45 6
        $data['singular'] = singular($data['name']);
46
47 6
        $data['primaryKey'] = 'get_' . $this->describe->getPrimaryKey(
48 6
            $data['name']
49 6
        );
50
51
        // Workaround...
52 6
        if ($data['primaryKey'] == 'get_') {
53
            $data['primaryKey'] = 'get_' . $this->describe->getPrimaryKey(
54
                singular($data['name'])
55
            );
56
        }
57
58 6
        if ($this->data['isCamel']) {
59 3
            $data['primaryKey'] = camelize($data['primaryKey']);
60 3
        }
61
62 6
        $data['columns'] = $this->describe->getTable(
63 6
            $data['name']
64 6
        );
65
66
        // Workaround...
67 6
        if (empty($data['columns'])) {
68
            $data['columns'] = $this->describe->getTable(
69
                singular($data['name'])
70
            );
71
        }
72 6
    }
73
74
    /**
75
     * Generates set of code based on data.
76
     * 
77
     * @return array
78
     */
79 6
    public function generate()
80
    {
81 6
        $this->prepareData($this->data);
82
83 6
        foreach ($this->data['columns'] as $column) {
84 6
            $field = strtolower($column->getField());
85
86 6
            $this->data['camel'][$field] = $this->transformField(
87 6
                $field,
88
                'camelize'
89 6
            );
90
91 6
            $this->data['underscore'][$field] = $this->transformField(
92 6
                $field,
93
                'underscore'
94 6
            );
95
96 6
            if ($column->isForeignKey()) {
97
                $referencedTable = Tools::stripTableSchema(
98
                    $column->getReferencedTable()
99
                );
100
101
                $this->data['foreignKeys'][$field] = plural($referencedTable);
102
103
                $singular = $field . '_singular';
104
105
                $this->data['foreignKeys'][$singular] = singular(
106
                    $referencedTable
107
                );
108
109
                $this->data = $this->getPrimaryKey(
110
                    $this->data,
111
                    $field,
112
                    $referencedTable
113
                );
114
            }
115 6
        }
116
117 6
        return $this->data;
118
    }
119
}
120