Completed
Push — master ( ba7b2b...21cd4a )
by Woody
04:45
created

StatusResponder::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 2
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 = $response->withStatus($this->status($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 status from the payload.
55
     *
56
     * @param PayloadInterface $payload
57
     *
58
     * @return integer
1 ignored issue
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60 2
    private function status(PayloadInterface $payload)
61
    {
62 2
        $status = $payload->getStatus();
63
64 2
        return $this->http_status->getStatusCode($status);
65
    }
66
}
67