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