Completed
Push — master ( 8aa0e6...89168a )
by Thomas
05:06
created

PayloadGeneratorTrait::generateNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace keeko\tools\generator\responder;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use keeko\framework\schema\ActionSchema;
6
use gossi\codegen\model\PhpClass;
7
use gossi\codegen\model\PhpMethod;
8
use gossi\codegen\model\PhpParameter;
9
10
trait PayloadGeneratorTrait {
11
	
12
	protected function ensureUseStatements(AbstractPhpStruct $struct) {
13
		parent::ensureUseStatements($struct);
14
		$struct->removeUseStatement('keeko\\framework\\domain\\payload\\PayloadInterface');
15
		$struct->removeUseStatement('keeko\\framework\\foundation\\AbstractResponder');
16
		$struct->addUseStatement('keeko\\framework\\foundation\\AbstractPayloadResponder');
17
	}
18
	
19
	protected function generateStruct(ActionSchema $action, $format) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
		$class = parent::generateStruct($action, $format);
21
		$class->setParentClassName('AbstractPayloadResponder');
22
	
23
		return $class;
24
	}
25
	
26
	protected function generateGetPayloadMethods(PhpClass $class, $body = '') {
27
		$class->setMethod(PhpMethod::create('getPayloadMethods')
28
			->setVisibility(PhpMethod::VISIBILITY_PROTECTED)
29
			->setBody($body)
30
		);
31
	}
32
	
33
	protected function generatePayloadMethod($name, $body, $type = 'PayloadInterface') {
34
		return PhpMethod::create($name)
35
			->addParameter(PhpParameter::create('request')
36
				->setType('Request')
37
			)
38
			->addParameter(PhpParameter::create('payload')
39
				->setType($type)
40
			)
41
			->setBody($body)
42
		;
43
	}
44
	
45
	protected function generateNotValid(PhpClass $class) {
46
		$class->addUseStatement('keeko\\framework\\domain\\payload\\NotValid');
47
		$class->addUseStatement('keeko\framework\exceptions\ValidationException');
48
		$notValid = $this->generatePayloadMethod('notValid', $this->twig->render('payload/notValid.twig'),
0 ignored issues
show
Bug introduced by
The property twig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
			'NotValid');
50
		$class->setMethod($notValid);
51
	}
52
	
53
	protected function generateNotFound(PhpClass $class) {
54
		$class->addUseStatement('keeko\\framework\\domain\\payload\\NotFound');
55
		$class->addUseStatement('Symfony\Component\Routing\Exception\ResourceNotFoundException');
56
		$notFound = $this->generatePayloadMethod('notFound', $this->twig->render('payload/notFound.twig'),
57
			'NotFound');
58
		$class->setMethod($notFound);
59
	}
60
}