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

StopOnFirstSuccess   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 3
A responseIsSuccessful() 0 4 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