Completed
Push — master ( d6c5c0...118f52 )
by Thomas
04:57
created

AbstractModelJsonResponderGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getRelationshipIncludes() 0 28 5
B getModelFields() 0 22 4
A getFieldsCode() 0 12 3
1
<?php
2
namespace keeko\tools\generator\responder;
3
4
use keeko\framework\utils\NameUtils;
5
use Propel\Generator\Model\Table;
6
use keeko\tools\model\Relationship;
7
8
class AbstractModelJsonResponderGenerator extends AbstractJsonResponderGenerator {
9
	
10
	use PayloadGeneratorTrait;
11
	
12
	protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) {
13
		if (in_array($model->getOriginCommonName(), $processed)) {
14
			return [];
15
		}
16
		
17
		$processed[] = $model->getOriginCommonName();
18
		
19
		$relationships = $this->modelService->getRelationships($model);
20
		$includes = [];
21
	
22
		foreach ($relationships->getAll() as $rel) {
23
			$typeName = $rel->getRelatedTypeName();
24
			if ($rel->getType() != Relationship::ONE_TO_ONE) {
25
				$typeName = NameUtils::pluralize($typeName);
26
			}
27
			$includeName = (!empty($root) ? $root . '.' : '') . $typeName;
28
			$includes[] = $includeName;
29
			
30
// 			$foreign = $rel->getForeign();
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...
31
// 			$includes = array_merge($includes, $this->getRelationshipIncludes($foreign, $includeName, $processed));
32
		}
33
		
34
		// load additional includes from codegen
35
		$codegen = $this->codegenService->getCodegen();
36
		$includes = array_merge($includes, $codegen->getIncludes($model->getOriginCommonName())->toArray());
0 ignored issues
show
Bug introduced by
The method getIncludes() does not seem to exist on object<keeko\framework\schema\CodegenSchema>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
		return $includes;
39
	}
40
	
41
	protected function getModelFields(Table $model, $root = '', $processed = []) {
42
		if (in_array($model->getOriginCommonName(), $processed)) {
43
			return [];
44
		}
45
		
46
		$typeName = NameUtils::dasherize($model->getOriginCommonName());
47
		$relationships = $this->modelService->getRelationships($model);
48
		$fields = [$typeName => $model];
49
50
		foreach ($relationships->getAll() as $rel) {
51
			$foreign = $rel->getForeign();
52
			$processed[] = $foreign->getOriginCommonName();
53
			
54
			$typeName = $rel->getRelatedTypeName();
55
			$name = (!empty($root) ? $root . '.' : '') . $typeName;
56
			
57
			$fields[$name] = $foreign;
58
			$fields = array_merge($fields, $this->getModelFields($foreign, $name, $processed));
59
		}
60
		
61
		return $fields;
62
	}
63
	
64
	protected function getFieldsCode(array $fields) {
65
		$code = '';
66
		foreach ($fields as $typeName => $field) {
67
			$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName());
68
		}
69
		
70
		if (strlen($code) > 0) {
71
			$code = substr($code, 0, -2);
72
		}
73
		
74
		return sprintf("[\n%s\n]", $code);
75
	}
76
	
77
}
78