Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

ViewGenerator::prepareData()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5.2526

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                    $this->data['primaryKeys'][$field] = Inflector::camelize(
117
                        $this->data['primaryKeys'][$field]
118
                    );
119
                }
120
            }
121 6
        }
122
123 6
        return $this->data;
124
    }
125
}
126