Completed
Push — master ( daab79...ba6b8c )
by Thomas
04:22
created

CountryReadJsonResponder   B

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 17

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
c 4
b 0
f 0
lcom 0
cbo 17
dl 0
loc 39
rs 7.8571

3 Methods

Rating   Name   Duplication   Size   Complexity  
A found() 0 15 1
A notFound() 0 3 1
A getPayloadMethods() 0 6 1
1
<?php
2
namespace keeko\core\responder;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use keeko\framework\foundation\AbstractPayloadResponder;
7
use keeko\framework\domain\payload\NotFound;
8
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
9
use keeko\core\model\Country;
10
use keeko\core\model\Continent;
11
use keeko\core\model\Currency;
12
use keeko\core\model\RegionType;
13
use keeko\framework\domain\payload\Found;
14
use Tobscure\JsonApi\Document;
15
use Tobscure\JsonApi\Resource;
16
use Tobscure\JsonApi\Parameters;
17
18
/**
19
 * Automatically generated JsonResponder for Reads a country
20
 * 
21
 * @author gossi
22
 */
23
class CountryReadJsonResponder extends AbstractPayloadResponder {
24
25
	/**
26
	 * @param Request $request
27
	 * @param Found $payload
28
	 */
29
	public function found(Request $request, Found $payload) {
30
		$params = new Parameters($request->query->all());
31
		$serializer = Country::getSerializer();
32
		$resource = new Resource($payload->getModel(), $serializer);
33
		$resource = $resource->with($params->getInclude(['continent', 'currency', 'region-type', 'region-type', 'country']));
34
		$resource = $resource->fields($params->getFields([
35
			'country' => Country::getSerializer()->getFields(),
36
			'continent' => Continent::getSerializer()->getFields(),
37
			'currency' => Currency::getSerializer()->getFields(),
38
			'region-type' => RegionType::getSerializer()->getFields()
39
		]));
40
		$document = new Document($resource);
41
42
		return new JsonResponse($document->toArray(), 200);
43
	}
44
45
	/**
46
	 * @param Request $request
47
	 * @param NotFound $payload
48
	 */
49
	public function notFound(Request $request, NotFound $payload) {
50
		throw new ResourceNotFoundException($payload->getMessage());
51
	}
52
53
	/**
54
	 */
55
	protected function getPayloadMethods() {
56
		return [
57
			'keeko\framework\domain\payload\Found' => 'found',
58
			'keeko\framework\domain\payload\NotFound' => 'notFound'
59
		];
60
	}
61
}
62