Completed
Push — master ( 0f0d6a...e070b7 )
by Rougin
05:03
created

ViewGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 13
Bugs 2 Features 2
Metric Value
wmc 6
c 13
b 2
f 2
lcom 1
cbo 3
dl 0
loc 90
ccs 51
cts 51
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareData() 0 35 3
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 9
    public function prepareData(array &$data)
24
    {
25
        $bootstrap = [
26 9
            'button' => 'btn btn-default',
27 9
            'buttonPrimary' => 'btn btn-primary',
28 9
            'formControl' => 'form-control',
29 9
            'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12',
30 9
            'label' => 'control-label',
31 9
            'table' => 'table table table-striped table-hover',
32
            'textRight' => 'text-right'
33 9
        ];
34
35 9
        if ($data['isBootstrap']) {
36 9
            $data['bootstrap'] = $bootstrap;
37 9
        }
38
39 9
        $data['camel'] = [];
40 9
        $data['underscore'] = [];
41 9
        $data['foreignKeys'] = [];
42 9
        $data['primaryKeys'] = [];
43
44 9
        $data['plural'] = plural($data['name']);
45 9
        $data['singular'] = singular($data['name']);
46
47 9
        $primaryKey = 'get_' . $this->describe->getPrimaryKey($data['name']);
48 9
        $data['primaryKey'] = $primaryKey;
49
50 9
        if ($this->data['isCamel']) {
51 6
            $data['primaryKey'] = camelize($data['primaryKey']);
52 6
        }
53
54 9
        $data['columns'] = $this->describe->getTable(
55 9
            $data['name']
56 9
        );
57 9
    }
58
59
    /**
60
     * Generates set of code based on data.
61
     * 
62
     * @return array
63
     */
64 9
    public function generate()
65
    {
66 9
        $this->prepareData($this->data);
67
68 9
        foreach ($this->data['columns'] as $column) {
69 9
            $field = strtolower($column->getField());
70
71 9
            $this->data['camel'][$field] = $this->transformField(
72 9
                $field,
73
                'camelize'
74 9
            );
75
76 9
            $this->data['underscore'][$field] = $this->transformField(
77 9
                $field,
78
                'underscore'
79 9
            );
80
81 9
            if ($column->isForeignKey()) {
82 9
                $referencedTable = Tools::stripTableSchema(
83 9
                    $column->getReferencedTable()
84 9
                );
85
86 9
                $this->data['foreignKeys'][$field] = plural($referencedTable);
87
88 9
                $singular = $field . '_singular';
89
90 9
                $this->data['foreignKeys'][$singular] = singular(
91
                    $referencedTable
92 9
                );
93
94 9
                $this->data = $this->getPrimaryKey(
95 9
                    $this->data,
96 9
                    $field,
97
                    $referencedTable
98 9
                );
99 9
            }
100 9
        }
101
102 9
        return $this->data;
103
    }
104
}
105