Completed
Push — master ( f80030...418223 )
by Alexandru
06:28
created

ExtractsRequestIdFromHeader::getRequestId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Arki\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\Providers;
13
14
use Arki\RequestId\Providers\RequestIdProvider;
15
use Arki\RequestId\RequestId;
16
use Symfony\Component\HttpFoundation\Request;
17
18 View Code Duplication
final class ExtractsRequestIdFromHeader implements RequestIdProvider
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * @var string
22
     */
23
    private $headerName;
24
    /**
25
     * @var RequestIdProvider
26
     */
27
    private $fallback;
28
    /**
29
     * @var Request
30
     */
31
    private $request;
32
33
    /**
34
     * @param Request           $request
35
     * @param string            $headerName
36
     * @param RequestIdProvider $fallback
37
     */
38
    public function __construct(
39
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
40
        RequestIdProvider $fallback,
41
        $headerName = RequestId::HEADER_NAME
42
    ) {
43
        $this->headerName = $headerName;
44
        $this->fallback = $fallback;
45
        $this->request = $request;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getRequestId()
52
    {
53
        if ($this->request->headers->has($this->headerName)) {
54
            return $this->request->headers->get($this->headerName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->request->headers->get($this->headerName); of type string|array adds the type array to the return on line 54 which is incompatible with the return type declared by the interface Arki\RequestId\Providers...dProvider::getRequestId of type string|null.
Loading history...
55
        }
56
57
        return $this->fallback->getRequestId();
58
    }
59
}
60