Completed
Push — master ( ba6b8c...fbc3ca )
by Thomas
04:38
created

CountryListJsonResponder   B

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 2
c 4
b 0
f 0
lcom 0
cbo 16
dl 0
loc 41
rs 8.4614

2 Methods

Rating   Name   Duplication   Size   Complexity  
B found() 0 26 1
A getPayloadMethods() 0 5 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\core\model\Country;
8
use keeko\core\model\Continent;
9
use keeko\core\model\Currency;
10
use keeko\core\model\RegionType;
11
use keeko\framework\domain\payload\Found;
12
use Tobscure\JsonApi\Document;
13
use Tobscure\JsonApi\Collection;
14
use Tobscure\JsonApi\Parameters;
15
16
/**
17
 * Automatically generated JsonResponder for List all countries
18
 * 
19
 * @author gossi
20
 */
21
class CountryListJsonResponder extends AbstractPayloadResponder {
22
23
	/**
24
	 * @param Request $request
25
	 * @param Found $payload
26
	 */
27
	public function found(Request $request, Found $payload) {
28
		$params = new Parameters($request->query->all());
29
		$data = $payload->getModel();
30
		$serializer = Country::getSerializer();
31
		$resource = new Collection($data, $serializer);
32
		$resource = $resource->with($params->getInclude(['continent', 'currency', 'region-type', 'region-type', 'country']));
33
		$resource = $resource->fields($params->getFields([
34
			'country' => Country::getSerializer()->getFields(),
35
			'continent' => Continent::getSerializer()->getFields(),
36
			'currency' => Currency::getSerializer()->getFields(),
37
			'region-type' => RegionType::getSerializer()->getFields()
38
		]));
39
		$document = new Document($resource);
40
41
		// meta
42
		$document->setMeta([
43
			'total' => $data->getNbResults(),
44
			'first' => $data->getFirstPage(),
45
			'next' => $data->getNextPage(),
46
			'previous' => $data->getPreviousPage(),
47
			'last' => $data->getLastPage()
48
		]);
49
50
		// return response
51
		return new JsonResponse($document->toArray());
52
	}
53
54
	/**
55
	 */
56
	protected function getPayloadMethods() {
57
		return [
58
			'keeko\framework\domain\payload\Found' => 'found'
59
		];
60
	}
61
}
62