Completed
Push — master ( 98cd2e...56265b )
by Thomas
09:01
created

getRelationshipIncludes()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 15
nc 6
nop 3
1
<?php
2
namespace keeko\tools\generator\response;
3
4
use keeko\framework\utils\NameUtils;
5
use Propel\Generator\Model\Table;
6
7
class AbstractModelJsonResponseGenerator extends AbstractJsonResponseGenerator {
8
	
9
	protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) {
10
		if (in_array($model->getOriginCommonName(), $processed)) {
11
			return [];
12
		}
13
		
14
		$relationships = $this->modelService->getRelationships($model);
15
		$includes = [];
16
	
17
		foreach ($relationships['all'] as $rel) {
18
			$fk = $rel['fk'];
19
			$foreignModel = $fk->getForeignTable();
20
			$processed[] = $foreignModel->getOriginCommonName();
21
			
22
			$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName());
1 ignored issue
show
Bug introduced by
The method dasherize() cannot be called from this context as it is declared private in class keeko\framework\utils\NameUtils.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
23
			if ($rel['type'] == 'many') {
24
				$typeName = NameUtils::pluralize($typeName);
25
			}
26
			$includes[] = (!empty($root) ? $root . '.' : '') . $typeName;
27
			$includes = array_merge($includes, $this->getRelationshipIncludes($foreignModel, $typeName, $processed));
28
		}
29
	
30
		return $includes;
31
	}
32
	
33
	protected function getModelFields(Table $model, $root = '', $processed = []) {
34
		if (in_array($model->getOriginCommonName(), $processed)) {
35
			return [];
36
		}
37
		
38
		$typeName = NameUtils::dasherize($model->getOriginCommonName());
1 ignored issue
show
Bug introduced by
The method dasherize() cannot be called from this context as it is declared private in class keeko\framework\utils\NameUtils.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
39
		$relationships = $this->modelService->getRelationships($model);
40
		$fields = [$typeName => $model];
41
42
		foreach ($relationships['all'] as $rel) {
43
			$fk = $rel['fk'];
44
			$foreignModel = $fk->getForeignTable();
45
			$processed[] = $foreignModel->getOriginCommonName();
46
			
47
			$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName());
1 ignored issue
show
Bug introduced by
The method dasherize() cannot be called from this context as it is declared private in class keeko\framework\utils\NameUtils.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
48
			if ($rel['type'] == 'many') {
49
				$typeName = NameUtils::pluralize($typeName);
50
			}
51
			$name = (!empty($root) ? $root . '.' : '') . $typeName;
52
			
53
			$fields[$name] = $foreignModel;
54
			$fields = array_merge($fields, $this->getModelFields($foreignModel, $name, $processed));
55
		}
56
		
57
		return $fields;
58
	}
59
	
60
	protected function getFieldsCode(array $fields) {
61
		$code = '';
62
		foreach ($fields as $typeName => $field) {
63
			$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName());
64
		}
65
		
66
		if (strlen($code) > 0) {
67
			$code = substr($code, 0, -2);
68
		}
69
		
70
		return sprintf("[\n%s\n]", $code);
71
	}
72
	
73
}
74