Completed
Push — master ( 74e326...044046 )
by Fabian
12s
created

Dispatcher::resetHeaderOriginalKeys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace IntegerNet\CallbackProxy;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Slim\Http\Headers;
7
use Slim\Http\Request as SlimRequest;
8
9
/**
10
 * Dispatches a request to multiple targets, returns first successful response (HTTP 200) or last response if none are
11
 * successful
12
 */
13
class Dispatcher
14
{
15
    /**
16
     * @var HttpClient
17
     */
18
    private $client;
19
    /**
20
     * @var Targets
21
     */
22
    private $targets;
23
    /**
24
     * @var DispatchStrategy
25
     */
26
    private $strategy;
27
28 48
    public function __construct(HttpClient $client, Targets $targets, DispatchStrategy $strategy)
29
    {
30 48
        $this->client = $client;
31 48
        $this->targets = $targets;
32 48
        $this->strategy = $strategy;
33 48
    }
34
35 48
    public function dispatch(SlimRequest $request, ResponseInterface $response, string $action): ResponseInterface
36
    {
37 48
        $this->resetHeaderOriginalKeys($request);
38 48
        return $this->strategy->execute($this->client, $this->targets, $request, $response, $action);
39
    }
40
41
    /**
42
     * Workaround for https://github.com/slimphp/Slim-Psr7/issues/11
43
     */
44 48
    private function resetHeaderOriginalKeys(SlimRequest $request): void
45
    {
46 48
        $headersProperty = new \ReflectionProperty(SlimRequest::class, 'headers');
47 48
        $headersProperty->setAccessible(true);
48
        /** @var Headers $headers */
49 48
        $headers = $headersProperty->getValue($request);
50 48
        foreach ($headers as $key => $header) {
51 48
            $headers->set($key, $header['value']);
52
        }
53 48
        $headersProperty->setAccessible(false);
54 48
    }
55
}
56