Completed
Push — master ( 2fb143...d99a08 )
by Thomas
09:36
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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 15
nc 6
nop 3
1
<?php
2
namespace keeko\tools\generator\responder;
3
4
use gossi\codegen\model\PhpClass;
5
use gossi\codegen\model\PhpMethod;
6
use keeko\framework\utils\NameUtils;
7
use Propel\Generator\Model\Table;
8
use gossi\codegen\model\PhpParameter;
9
use gossi\codegen\model\AbstractPhpStruct;
10
use keeko\framework\schema\ActionSchema;
11
12
class AbstractModelJsonResponderGenerator extends AbstractJsonResponderGenerator {
13
	
14
	protected function ensureUseStatements(AbstractPhpStruct $struct) {
15
		parent::ensureUseStatements($struct);
16
		$struct->addUseStatement('keeko\framework\domain\payload\PayloadInterface');
17
		$struct->removeUseStatement('keeko\\framework\\foundation\\AbstractResponder');
18
		$struct->addUseStatement('keeko\\framework\\foundation\\AbstractPayloadResponder');
19
	}
20
	
21
	protected function generateStruct(ActionSchema $action, $format) {
22
		$class = parent::generateStruct($action, $format);
23
		$class->setParentClassName('AbstractPayloadResponder');
24
		
25
		return $class;
26
	}
27
	
28
	protected function generateGetPayloadMethods(PhpClass $class, $body = '') {
29
		$class->setMethod(PhpMethod::create('getPayloadMethods')
30
			->setVisibility(PhpMethod::VISIBILITY_PROTECTED)
31
			->setBody($body)
32
		);
33
	}
34
	
35
	protected function generatePayloadMethod($name, $body) {
36
		return PhpMethod::create($name)
37
			->addParameter(PhpParameter::create('request')
38
				->setType('Request')
39
			)
40
			->addParameter(PhpParameter::create('payload')
41
				->setType('PayloadInterface')
42
			)
43
			->setBody($body)
44
		;
45
	}
46
	
47
	protected function generateNotValid(PhpClass $class) {
48
		$class->addUseStatement('keeko\framework\exceptions\ValidationException');
49
		$notValid = $this->generatePayloadMethod('notValid', $this->twig->render('model-notValid.twig'));
50
		$class->setMethod($notValid);
51
	}
52
	
53
	protected function generateNotFound(PhpClass $class) {
54
		$class->addUseStatement('Symfony\Component\Routing\Exception\ResourceNotFoundException');
55
		$notFound = $this->generatePayloadMethod('notFound', $this->twig->render('model-notFound.twig'));
56
		$class->setMethod($notFound);
57
	}
58
	
59
	protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) {
60
		if (in_array($model->getOriginCommonName(), $processed)) {
61
			return [];
62
		}
63
		
64
		$relationships = $this->modelService->getRelationships($model);
65
		$includes = [];
66
	
67
		foreach ($relationships['all'] as $rel) {
68
			$fk = $rel['fk'];
69
			$foreignModel = $fk->getForeignTable();
70
			$processed[] = $foreignModel->getOriginCommonName();
71
			
72
			$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...
73
			if ($rel['type'] == 'many') {
74
				$typeName = NameUtils::pluralize($typeName);
75
			}
76
			$includes[] = (!empty($root) ? $root . '.' : '') . $typeName;
77
			$includes = array_merge($includes, $this->getRelationshipIncludes($foreignModel, $typeName, $processed));
78
		}
79
	
80
		return $includes;
81
	}
82
	
83
	protected function getModelFields(Table $model, $root = '', $processed = []) {
84
		if (in_array($model->getOriginCommonName(), $processed)) {
85
			return [];
86
		}
87
		
88
		$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...
89
		$relationships = $this->modelService->getRelationships($model);
90
		$fields = [$typeName => $model];
91
92
		foreach ($relationships['all'] as $rel) {
93
			$fk = $rel['fk'];
94
			$foreignModel = $fk->getForeignTable();
95
			$processed[] = $foreignModel->getOriginCommonName();
96
			
97
			$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...
98
			if ($rel['type'] == 'many') {
99
				$typeName = NameUtils::pluralize($typeName);
100
			}
101
			$name = (!empty($root) ? $root . '.' : '') . $typeName;
102
			
103
			$fields[$name] = $foreignModel;
104
			$fields = array_merge($fields, $this->getModelFields($foreignModel, $name, $processed));
105
		}
106
		
107
		return $fields;
108
	}
109
	
110
	protected function getFieldsCode(array $fields) {
111
		$code = '';
112
		foreach ($fields as $typeName => $field) {
113
			$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName());
114
		}
115
		
116
		if (strlen($code) > 0) {
117
			$code = substr($code, 0, -2);
118
		}
119
		
120
		return sprintf("[\n%s\n]", $code);
121
	}
122
	
123
}
124