Completed
Push — master ( 5d27f2...d6c5c0 )
by Thomas
08:45
created

EmberModelGenerator::generateColumns()   C

Complexity

Conditions 16
Paths 15

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 42
rs 5.0151
cc 16
eloc 30
nc 15
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace keeko\tools\generator\ember;
3
4
use keeko\framework\schema\CodegenSchema;
5
use keeko\framework\utils\NameUtils;
6
use keeko\tools\model\Relationship;
7
use phootwork\collection\Set;
8
use Propel\Generator\Model\Table;
9
10
class EmberModelGenerator extends AbstractEmberGenerator {
11
	
12
	public function generate(Table $model) {
13
		$class = new EmberClassGenerator('Model');
14
		$class->addImport('Model', 'ember-data/model');
15
		$class->addImport('attr', 'ember-data/attr');
16
		
17
		// columns
18
		$this->generateColumns($class, $model);
19
		
20
		// relationships
21
		$this->generateRelationships($class, $model);
22
		
23
		return $class->generate();
24
	}
25
	
26
	protected function generateColumns(EmberClassGenerator $class, Table $model) {
27
		$codegen = $this->getCodegen();
28
		$filter = $this->getColumnFilter($codegen, $model);
29
		foreach ($model->getColumns() as $col) {
30
			if (in_array($col, $filter)) {
31
				continue;
32
			}
33
			
34
			if ($col->isForeignKey() || $col->isPrimaryKey()) {
35
				continue;
36
			}
37
				
38
			$prop = NameUtils::toCamelCase($col->getPhpName());
39
				
40
			switch ($col->getType()) {
41
				case 'NUMERIC':
42
				case 'DECIMAL':
43
				case 'TINYINT':
44
				case 'SMALLINT':
45
				case 'INTEGER':
46
				case 'BIGINT':
47
				case 'REAL':
48
				case 'FLOAT':
49
				case 'DOUBLE':
50
					$value = 'attr(\'number\')';
51
					break;
52
						
53
				case 'BOOLEAN':
54
					$value = 'attr(\'boolean\')';
55
					break;
56
						
57
				case 'TIMESTAMP':
58
					$value = 'attr(\'date\')';
59
					break;
60
						
61
				default:
62
					$value = 'attr(\'string\')';
63
			}
64
				
65
			$class->setProperty($prop, $value);
66
		}
67
	}
68
	
69
	protected function generateRelationships(EmberClassGenerator $class, Table $model) {
70
		$relationships = $this->modelService->getRelationships($model);
71
		$imports = new Set();
72
		
73
		foreach ($relationships->getAll() as $relationship) {
74
			$prop = NameUtils::toCamelCase($relationship->getRelatedTypeName());
75
			$type = $relationship->getRelatedPluralTypeName();
76
			$slug = $this->getSlug($relationship->getForeign());
77
			
78
			// check one-to-one
79
			if ($relationship->getType() == Relationship::ONE_TO_ONE) {
80
				$value = sprintf('belongsTo(\'%s/%s\')', $slug, $type);
81
				$imports->add('belongsTo');
82
			} else {
83
				$value = sprintf('hasMany(\'%s/%s\')', $slug, $type);
84
				$imports->add('hasMany');
85
			}
86
			
87
			$class->setProperty($prop, $value);
88
		}
89
		
90
		if ($imports->size() > 0) {
91
			$import = sprintf('{ %s }', implode(', ', $imports->toArray()));
92
			$class->addImport($import, 'ember-data/relationships');
93
		}
94
	}
95
	
96
	private function getColumnFilter(CodegenSchema $codegen, Table $model) {
97
		$read = $codegen->getReadFilter($model->getOriginCommonName());
98
		$write = $codegen->getWriteFilter($model->getOriginCommonName());
99
		
100
		$merge = [];
101
		foreach ($read as $field) {
102
			if (in_array($field, $write)) {
103
				$merge[] = $field;
104
			}
105
		}
106
		
107
		return $merge;
108
	}
109
}