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

ViewGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 88
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

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