GeneratorDefinitionService::getWriteFields()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
namespace keeko\tools\services;
3
4
use Propel\Generator\Model\Table;
5
6
class GeneratorDefinitionService extends AbstractService {
7
8
	/**
9
	 * Returns code for hydrating a propel model
10
	 *
11
	 * @param string $modelName
12
	 * @return string
13
	 */
14
	public function getWriteFields($modelName) {
15
		$generatorDefinition = $this->project->getGeneratorDefinition();
16
		$filter = $generatorDefinition->getWriteFilter($modelName);
17
		$model = $this->modelService->getModel($modelName);
18
		$computed = $this->getComputedFields($model);
19
		$filter = array_merge($filter, $computed);
20
21
		$fields = [];
22
		$cols = $model->getColumns();
23
		foreach ($cols as $col) {
24
			$prop = $col->getName();
25
26
			if (!in_array($prop, $filter)) {
27
				$fields[] = $prop;
28
			}
29
		}
30
31
		return $fields;
32
	}
33
34
	/**
35
	 * Returns the fields for a model
36
	 *
37
	 * @param string $modelName
38
	 * @return array
39
	 */
40
	public function getReadFields($modelName) {
41
		$generatorDefinition = $this->project->getGeneratorDefinition();
42
		$model = $this->modelService->getModel($modelName);
43
// 		$computed = $this->getComputedFields($model);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
		$filter = $generatorDefinition->getReadFilter($modelName);
45
// 		$filter = array_merge($filter, $computed);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
47
		$fields = [];
48
		$cols = $model->getColumns();
49
		foreach ($cols as $col) {
50
			$prop = $col->getName();
51
52
			if (!in_array($prop, $filter) && !$col->isForeignKey() && !$col->isPrimaryKey()) {
53
				$fields[] = $prop;
54
			}
55
		}
56
57
		return $fields;
58
	}
59
60
	/**
61
	 * Returns computed model fields
62
	 *
63
	 * @param Table $table
64
	 * @return array<string>
65
	 */
66
	public function getComputedFields(Table $table) {
67
		$fields = [];
68
69
		// iterate over behaviors to get their respective columns
70
		foreach ($table->getBehaviors() as $behavior) {
71
			switch ($behavior->getName()) {
72
				case 'timestampable':
73
					$fields[] = $behavior->getParameter('create_column');
74
					$fields[] = $behavior->getParameter('update_column');
75
					break;
76
77
				case 'aggregate_column':
78
					$fields[] = $behavior->getParameter('name');
79
					break;
80
			}
81
		}
82
83
		return $fields;
84
	}
85
86
	/**
87
	 * Returns all attributes that aren't written onto a model (because manually filter or computed)
88
	 *
89
	 * @param Table $model
90
	 * @return array
91
	 */
92
	public function getWriteFilter(Table $model) {
93
		$modelName = $model->getOriginCommonName();
94
		$generatorDefinition = $this->project->getGeneratorDefinition();
95
		$filter = $generatorDefinition->getWriteFilter($modelName);
96
		$computed = $this->getComputedFields($model);
97
		return array_merge($filter, $computed);
98
	}
99
}