Completed
Push — master ( 2aa80c...f80030 )
by Alexandru
08:43
created

DefaultDecorator::decorateResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Arkitekto\RequestId library.
5
 *
6
 * (c) Alexandru Furculita <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 */
11
12
namespace Arki\RequestId\Integrations\Symfony\Decorators;
13
14
use Arki\RequestId\Providers\RequestIdProvider;
15
use Arki\RequestId\Providers\RequestIdProviderFactory;
16
use Arki\RequestId\RequestId;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
final class DefaultDecorator implements RequestDecorator, ResponseDecorator
21
{
22
    /**
23
     * @var RequestIdProvider
24
     */
25
    private $providerFactory;
26
    /**
27
     * @var string
28
     */
29
    private $requestHeader;
30
    /**
31
     * @var string
32
     */
33
    private $responseHeader;
34
35
    /**
36
     * @param RequestIdProviderFactory $providerFactory
37
     * @param string                   $requestHeader
38
     * @param string                   $responseHeader
39
     */
40
    public function __construct(
41
        RequestIdProviderFactory $providerFactory,
42
        $requestHeader = RequestId::HEADER_NAME,
43
        $responseHeader = RequestId::HEADER_NAME
44
    ) {
45
        $this->providerFactory = $providerFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $providerFactory of type object<Arki\RequestId\Pr...questIdProviderFactory> is incompatible with the declared type object<Arki\RequestId\Pr...ders\RequestIdProvider> of property $providerFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->requestHeader = $requestHeader;
47
        $this->responseHeader = $responseHeader;
48
    }
49
50
    /**
51
     * @param Request $request
52
     *
53
     * @return Request
54
     */
55
    public function decorateRequest(Request $request)
56
    {
57
        $provider = $this->providerFactory->create($request);
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<Arki\RequestId\Pr...ders\RequestIdProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
59
        if (null === $provider->getRequestId()) {
60
            return $request;
61
        }
62
63
        $request->headers->set($this->requestHeader, $provider->getRequestId());
64
    }
65
66
    /**
67
     * @param Response $response
68
     *
69
     * @return Response
70
     */
71
    public function decorateResponse(Response $response)
72
    {
73
        if (null === $this->providerFactory->getRequestId()) {
74
            return $response;
75
        }
76
77
        $response->headers->set($this->responseHeader, $this->providerFactory->getRequestId());
78
    }
79
}
80