__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-12-09
5
 */
6
namespace Net\Bazzline\Component\Curl\ResponseBehaviour;
7
8
use Exception;
9
use Net\Bazzline\Component\Curl\Response\Response;
10
use RuntimeException;
11
12
class ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviour implements ResponseBehaviourInterface
13
{
14
    /** @var int */
15
    private $statusCode;
16
17
    /**
18
     * @param int $firstStatusCodeThatIsOverTheLimit
19
     */
20
    public function __construct($firstStatusCodeThatIsOverTheLimit = 400)
21
    {
22
        $this->statusCode = (int) $firstStatusCodeThatIsOverTheLimit;
23
    }
24
25
    /**
26
     * Since the Response is immutable, each behaviour has to return a new
27
     *  Response instance
28
     *
29
     * @param Response $response
30
     * @return Response
31
     * @throws Exception|RuntimeException
32
     */
33
    public function behave(Response $response)
34
    {
35
        $statusCodeIsToHigh = ($response->statusCode() > $this->statusCode);
36
37
        if ($statusCodeIsToHigh) {
38
            throw new RuntimeException(
39
                'limit of status code exceeded. dumping response: ' .
40
                implode(', ', $response->convertIntoAnArray())
41
            );
42
        }
43
44
        return $response;
45
    }
46
}
47