ResponseFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 16.84 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 16
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A respond() 8 8 1
A respondJson() 8 8 1
A forward() 0 27 2
A redirectToUrl() 0 4 1
A redirectToRoute() 0 6 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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Web\Infrastructure\Response\ZendDiactoros;
16
17
use Acme\App\Core\Port\Router\UrlGeneratorInterface;
18
use Acme\App\Presentation\Web\Core\Port\Response\ResponseFactoryInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
22
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\HttpFoundation\RedirectResponse;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\HttpKernelInterface;
27
28
final class ResponseFactory implements ResponseFactoryInterface
29
{
30
    /**
31
     * @var HttpKernelInterface
32
     */
33
    private $httpKernel;
34
35
    /**
36
     * @var UrlGeneratorInterface
37
     */
38
    private $urlGenerator;
39
40
    /**
41
     * @var HttpFoundationFactoryInterface
42
     */
43
    private $symfonyResponseFactory;
44
45
    /**
46
     * @var HttpMessageFactoryInterface
47
     */
48
    private $psrResponseFactory;
49
50
    public function __construct(
51
        HttpKernelInterface $httpKernel,
52
        UrlGeneratorInterface $urlGenerator,
53
        HttpFoundationFactoryInterface $symfonyResponseFactory,
54
        HttpMessageFactoryInterface $psrResponseFactory
55
    ) {
56
        $this->httpKernel = $httpKernel;
57
        $this->urlGenerator = $urlGenerator;
58
        $this->symfonyResponseFactory = $symfonyResponseFactory;
59
        $this->psrResponseFactory = $psrResponseFactory;
60
    }
61
62 View Code Duplication
    public function respond($content = '', int $status = 200, array $headers = []): ResponseInterface
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...
63
    {
64
        $response = $this->psrResponseFactory->createResponse(new Response($content, $status, $headers));
65
66
        $response->getBody()->rewind();
67
68
        return $response;
69
    }
70
71 View Code Duplication
    public function respondJson($data = null, int $status = 200, array $headers = []): ResponseInterface
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...
72
    {
73
        $response = $this->psrResponseFactory->createResponse(new JsonResponse($data, $status, $headers));
74
75
        $response->getBody()->rewind();
76
77
        return $response;
78
    }
79
80
    /**
81
     * @throws \Exception
82
     */
83
    public function forward(
84
        ServerRequestInterface $currentRequest,
85
        string $controller,
86
        array $attributes = null,
87
        array $queryParameters = null,
88
        array $postParameters = null
89
    ): ResponseInterface {
90
        $symfonyRequest = $this->symfonyResponseFactory->createRequest($currentRequest);
91
92
        $attributes['_forwarded'] = $symfonyRequest->attributes;
93
        $attributes['_controller'] = $controller;
94
        $subRequest = $symfonyRequest->duplicate($queryParameters, $postParameters, $attributes);
95
96
        $response = $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
97
98
        if ($response instanceof ResponseInterface) {
99
            $response->getBody()->rewind();
100
101
            return $response;
102
        }
103
104
        $response = $this->psrResponseFactory->createResponse($response);
105
106
        $response->getBody()->rewind();
107
108
        return $response;
109
    }
110
111
    public function redirectToUrl(string $url, int $status = 302): ResponseInterface
112
    {
113
        return $this->psrResponseFactory->createResponse(new RedirectResponse($url, $status));
114
    }
115
116
    public function redirectToRoute(string $route, array $parameters = [], int $status = 302): ResponseInterface
117
    {
118
        return $this->psrResponseFactory->createResponse(
119
            new RedirectResponse($this->urlGenerator->generateUrl($route, $parameters), $status)
120
        );
121
    }
122
}
123