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

ModelGenerator::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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
use Rougin\Combustor\Common\Inflector;
7
8
/**
9
 * Model Generator
10
 *
11
 * Generates CodeIgniter-based models.
12
 * 
13
 * @package Combustor
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 */
16
class ModelGenerator extends BaseGenerator implements GeneratorInterface
17
{
18
    /**
19
     * Prepares the data before generation.
20
     * 
21
     * @param  array &$data
22
     * @return void
23
     */
24 9
    public function prepareData(array &$data)
25
    {
26 9
        $data['camel'] = [];
27 9
        $data['columns'] = [];
28 9
        $data['indexes'] = [];
29 9
        $data['primaryKeys'] = [];
30 9
        $data['underscore'] = [];
31 9
        $data['columns'] = $this->describe->getTable($data['name']);
32 9
        $data['primaryKey'] = $this->describe->getPrimaryKey($data['name']);
33 9
    }
34
35
    /**
36
     * Generates set of code based on data.
37
     * 
38
     * @return array
39
     */
40 9
    public function generate()
41
    {
42 9
        $this->prepareData($this->data);
43
44 9
        foreach ($this->data['columns'] as $column) {
45 9
            $field = strtolower($column->getField());
46
47 9
            $this->data['camel'][$field] = $this->transformField(
48 9
                $field,
49
                'camelize'
50 9
            );
51
52 9
            $this->data['underscore'][$field] = $this->transformField(
53 9
                $field,
54
                'underscore'
55 9
            );
56
57 9
            if ($column->isForeignKey()) {
58
                $field = $column->getField();
59
60
                array_push($this->data['indexes'], $field);
61
62
                $this->data['primaryKeys'][$field] = 'get_' . 
63
                    $this->describe->getPrimaryKey(
64
                        $column->getReferencedTable()
65
                    );
66
67 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...
68
                    $this->data['primaryKeys'][$field] = Inflector::camelize(
69
                        $this->data['primaryKeys'][$field]
70
                    );
71
                }
72
            }
73
74 9
            $column->setReferencedTable(
75 9
                Tools::stripTableSchema($column->getReferencedTable())
76 9
            );
77 9
        }
78
79 9
        return $this->data;
80
    }
81
}
82