ModelGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 60
ccs 36
cts 36
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 36 3
A prepareData() 0 9 1
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
7
/**
8
 * Model Generator
9
 *
10
 * Generates CodeIgniter-based models.
11
 *
12
 * @package Combustor
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class ModelGenerator 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 9
        $data['camel'] = [];
26 9
        $data['columns'] = [];
27 9
        $data['indexes'] = [];
28 9
        $data['primaryKeys'] = [];
29 9
        $data['underscore'] = [];
30 9
        $data['columns'] = $this->describe->getTable($data['name']);
31 9
        $data['primaryKey'] = $this->describe->getPrimaryKey($data['name']);
32 9
    }
33
34
    /**
35
     * Generates set of code based on data.
36
     *
37
     * @return array
38
     */
39 9
    public function generate()
40
    {
41 9
        $this->prepareData($this->data);
42
43 9
        foreach ($this->data['columns'] as $column) {
44 9
            $field = strtolower($column->getField());
45
46 9
            $this->data['camel'][$field] = $this->transformField(
47 9
                $field,
48 3
                'camelize'
49 6
            );
50
51 9
            $this->data['underscore'][$field] = $this->transformField(
52 9
                $field,
53 3
                'underscore'
54 6
            );
55
56 9
            if ($column->isForeignKey()) {
57 9
                $field = $column->getField();
58 9
                $referencedTable = $column->getReferencedTable();
59
60 9
                array_push($this->data['indexes'], $field);
61
62 9
                $this->data = $this->getPrimaryKey(
63 9
                    $this->data,
64 9
                    $field,
65 3
                    $referencedTable
66 6
                );
67 6
            }
68
69 9
            $column->setReferencedTable(
70 9
                Tools::stripTableSchema($column->getReferencedTable())
71 6
            );
72 6
        }
73
74 9
        return $this->data;
75
    }
76
}
77