StatusResponder::status()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Equip\Responder;
4
5
use Equip\Adr\PayloadInterface;
6
use Equip\Adr\ResponderInterface;
7
use Lukasoppermann\Httpstatus\Httpstatus;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class StatusResponder implements ResponderInterface
12
{
13
    /**
14
     * @var Httpstatus
15
     */
16
    private $http_status;
17
18
    /**
19
     * @param Httpstatus $http_status
20
     */
21 5
    public function __construct(Httpstatus $http_status)
22
    {
23 5
        $this->http_status = $http_status;
24 5
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29 5
    public function __invoke(
30
        ServerRequestInterface $request,
31
        ResponseInterface $response,
32
        PayloadInterface $payload
33
    ) {
34 5
        if ($this->hasStatus($payload)) {
35 2
            $response = $this->status($response, $payload);
36 2
        }
37
38 5
        return $response;
39
    }
40
41
    /**
42
     * Determine if the payload has a status.
43
     *
44
     * @param PayloadInterface $payload
45
     *
46
     * @return boolean
47
     */
48 5
    private function hasStatus(PayloadInterface $payload)
49
    {
50 5
        return (bool) $payload->getStatus();
51
    }
52
53
    /**
54
     * Get the response with the status code from the payload.
55
     *
56
     * @param ResponseInterface $response
57
     * @param PayloadInterface $payload
58
     *
59
     * @return ResponseInterface
60
     */
61 2
    private function status(
62
        ResponseInterface $response,
63
        PayloadInterface $payload
64
    ) {
65 2
        $status = $payload->getStatus();
66 2
        $code = $this->http_status->getStatusCode($status);
67
68 2
        return $response->withStatus($code);
69
    }
70
}
71