1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Common Component. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\Common\EventListener; |
16
|
|
|
|
17
|
|
|
use FOS\RestBundle\View\View; |
18
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
19
|
|
|
use SWP\Component\Common\Factory\KnpPaginatorRepresentationFactory; |
20
|
|
|
use SWP\Component\Common\Response\ResourcesListResponseInterface; |
21
|
|
|
use SWP\Component\Common\Response\ResponseContextInterface; |
22
|
|
|
use SWP\Component\Common\Response\SingleResourceResponseInterface; |
23
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
24
|
|
|
|
25
|
|
|
final class ResourceResponseListener |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ViewHandlerInterface |
29
|
|
|
*/ |
30
|
|
|
private $viewHandler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ResourcesListResponseListener constructor. |
34
|
|
|
* |
35
|
|
|
* @param ViewHandlerInterface $viewHandler |
36
|
|
|
*/ |
37
|
50 |
|
public function __construct(ViewHandlerInterface $viewHandler) |
38
|
|
|
{ |
39
|
50 |
|
$this->viewHandler = $viewHandler; |
40
|
50 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param GetResponseForControllerResultEvent $event |
44
|
|
|
*/ |
45
|
48 |
|
public function onKernelView(GetResponseForControllerResultEvent $event) |
46
|
|
|
{ |
47
|
48 |
|
$controllerResult = $event->getControllerResult(); |
48
|
|
|
|
49
|
48 |
|
if ($controllerResult instanceof ResourcesListResponseInterface) { |
50
|
8 |
|
$responseContext = $controllerResult->getResponseContext(); |
51
|
|
|
|
52
|
8 |
View Code Duplication |
if ($responseContext->getIntention() === ResponseContextInterface::INTENTION_API) { |
|
|
|
|
53
|
8 |
|
$factory = new KnpPaginatorRepresentationFactory(); |
54
|
8 |
|
$representation = $factory->createRepresentation($controllerResult->getResources(), $event->getRequest()); |
55
|
8 |
|
$event->setResponse($this->viewHandler->handle( |
56
|
8 |
|
View::create($representation, $responseContext->getStatusCode()) |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
View Code Duplication |
} elseif ($controllerResult instanceof SingleResourceResponseInterface) { |
|
|
|
|
60
|
43 |
|
$responseContext = $controllerResult->getResponseContext(); |
61
|
43 |
|
if ($responseContext->getIntention() === ResponseContextInterface::INTENTION_API) { |
62
|
43 |
|
$event->setResponse($this->viewHandler->handle( |
63
|
43 |
|
View::create($controllerResult->getResource(), $responseContext->getStatusCode()) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
48 |
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.