Completed
Push — master ( fceab7...09c4c4 )
by Fabian
13s
created

StopOnFirstSuccess::responseIsSuccessful()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\CallbackProxy\DispatchStrategy;
5
6
use IntegerNet\CallbackProxy\DispatchStrategy;
7
use IntegerNet\CallbackProxy\HttpClient;
8
use IntegerNet\CallbackProxy\Targets;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class StopOnFirstSuccess implements DispatchStrategy
13
{
14 6
    public function execute(
15
        HttpClient $client,
16
        Targets $targets,
17
        RequestInterface $request,
18
        ResponseInterface $response,
19
        string $action
20
    ): ResponseInterface {
21 6
        foreach ($targets as $target) {
22 6
            $response = $client->send($request, $target, $action);
23 6
            if ($this->responseIsSuccessful($response)) {
24 6
                return $response;
25
            }
26
        }
27 3
        return $response;
28
    }
29
30 6
    private function responseIsSuccessful(ResponseInterface $response): bool
31
    {
32 6
        return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
33
    }
34
}
35