Completed
Push — master ( bbff6f...c51acf )
by Rafał
19:14 queued 09:31
created

ResourceResponseListener   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 22.92 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 22
loc 96
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onKernelView() 0 34 6
A setHeaders() 11 11 3
A clearCookies() 11 11 3
A setSerializationGroups() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Context\Context;
18
use FOS\RestBundle\View\View;
19
use FOS\RestBundle\View\ViewHandlerInterface;
20
use SWP\Component\Common\Factory\KnpPaginatorRepresentationFactory;
21
use SWP\Component\Common\Response\ResourcesListResponseInterface;
22
use SWP\Component\Common\Response\ResponseContext;
23
use SWP\Component\Common\Response\ResponseContextInterface;
24
use SWP\Component\Common\Response\SingleResourceResponseInterface;
25
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
26
27
final class ResourceResponseListener
28
{
29
    /**
30
     * @var ViewHandlerInterface
31
     */
32
    private $viewHandler;
33
34
    /**
35
     * ResourcesListResponseListener constructor.
36
     *
37 79
     * @param ViewHandlerInterface $viewHandler
38
     */
39 79
    public function __construct(ViewHandlerInterface $viewHandler)
40 79
    {
41
        $this->viewHandler = $viewHandler;
42
    }
43
44
    /**
45 78
     * @param GetResponseForControllerResultEvent $event
46
     */
47 78
    public function onKernelView(GetResponseForControllerResultEvent $event)
48
    {
49 78
        $controllerResult = $event->getControllerResult();
50 20
        if (null === $controllerResult) {
51
            return;
52 20
        }
53 20
54 20
        /** @var ResponseContext $responseContext */
55 20
        $responseContext = $controllerResult->getResponseContext();
56 20
        if ($controllerResult instanceof ResourcesListResponseInterface) {
57
            if (ResponseContextInterface::INTENTION_API === $responseContext->getIntention()) {
58
                $factory = new KnpPaginatorRepresentationFactory();
59
                $representation = $factory->createRepresentation($controllerResult->getResources(), $event->getRequest());
60 72
61 72
                $view = View::create($representation, $responseContext->getStatusCode());
62 72
                $view = $this->setSerializationGroups($view);
63 72
                $event->setResponse($this->viewHandler->handle(
64
                    $view
65
                ));
66
            }
67 78
        } elseif ($controllerResult instanceof SingleResourceResponseInterface) {
68
            if (ResponseContextInterface::INTENTION_API === $responseContext->getIntention()) {
69
                $view = View::create($controllerResult->getResource(), $responseContext->getStatusCode());
70
                $view = $this->setSerializationGroups($view);
71
72
                $event->setResponse($this->viewHandler->handle(
73
                    $view
74
                ));
75
            }
76
        }
77
78
        $this->setHeaders($event, $responseContext);
79
        $this->clearCookies($event, $responseContext);
80
    }
81
82
    /**
83
     * @param GetResponseForControllerResultEvent $event
84
     * @param ResponseContext                     $responseContext
85
     */
86 View Code Duplication
    private function setHeaders(GetResponseForControllerResultEvent $event, ResponseContext $responseContext)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
    {
88
        if (count($responseContext->getHeaders()) > 0) {
89
            $response = $event->getResponse();
90
            foreach ($responseContext->getHeaders() as $key => $value) {
91
                $response->headers->set($key, $value);
92
            }
93
94
            $event->setResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $event->getResponse() on line 89 can be null; however, Symfony\Component\HttpKe...nseEvent::setResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
95
        }
96
    }
97
98
    /**
99
     * @param GetResponseForControllerResultEvent $event
100
     * @param ResponseContext                     $responseContext
101
     */
102 View Code Duplication
    private function clearCookies(GetResponseForControllerResultEvent $event, ResponseContext $responseContext)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
103
    {
104
        if (count($responseContext->getClearedCookies()) > 0) {
105
            $response = $event->getResponse();
106
            foreach ($responseContext->getClearedCookies() as $key) {
107
                $response->headers->clearCookie($key);
108
            }
109
110
            $event->setResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $event->getResponse() on line 105 can be null; however, Symfony\Component\HttpKe...nseEvent::setResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
111
        }
112
    }
113
114
    private function setSerializationGroups(View $view): View
115
    {
116
        $context = new Context();
117
        $context->setGroups(['Default', 'api']);
118
        $view->setContext($context);
119
120
        return $view;
121
    }
122
}
123