TransitionMiddleware::transformRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Transitions;
4
5
use Closure;
6
use Generator;
7
use Illuminate\Http\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class TransitionMiddleware
11
{
12
13
    /**
14
     * @var Config
15
     */
16
    private $config;
17
    /**
18
     * @var TransitionFactory
19
     */
20
    private $factory;
21
22 12
    public function __construct(Config $config, TransitionFactory $factory)
23
    {
24
25 12
        $this->config = $config;
26 12
        $this->factory = $factory;
27 12
    }
28
29 12
    public function handle(Request $request, Closure $next) : Response
30
    {
31
32 12
        $request = $this->transformRequest($request);
33 12
        return $this->transformResponse($request, $next($request));
34
    }
35
36 12
    private function transformRequest(Request $request) : Request
37
    {
38
39 12
        foreach ($this->transitions($request->header($this->config->headerKey())) as $version) {
40 9
            $request = $version->transformRequest($request);
41
        }
42 12
        return $request;
43
    }
44
45 12
    private function transformResponse(Request $request, Response $response) : Response
46
    {
47
48 12
        foreach ($this->transitions($request->header($this->config->headerKey())) as $transition) {
0 ignored issues
show
Bug introduced by
It seems like $request->header($this->config->headerKey()) targeting Illuminate\Http\Concerns...actsWithInput::header() can also be of type array or null; however, Transitions\TransitionMiddleware::transitions() does only seem to accept string|integer, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49 9
            $response = $transition->withRequest($request)->transformResponse($response);
50
        }
51 12
        return $response;
52
    }
53
54
    /**
55
     * @param string|int $version
56
     * @return Generator|Transition[]
57
     */
58 12
    private function transitions($version) : Generator
59
    {
60
61 12
        foreach ($this->config->transitionsForVersion($version) as $transition) {
62 9
            yield $this->factory->create($transition);
63
        }
64 12
    }
65
}
66