Completed
Pull Request — master (#1)
by Woody
27:07 queued 01:07
created

AbstractFormatter::status()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.2222
cc 7
eloc 9
nc 4
nop 1
crap 7
1
<?php
2
namespace Equip\Formatter;
3
4
use Equip\Adr\PayloadInterface;
5
6
abstract class AbstractFormatter
7
{
8
    /**
9
     * Get the content types this formatter can satisfy.
10
     *
11
     * @return array
12
     */
13 1
    public static function accepts()
14
    {
15 1
        throw new \RuntimeException(sprintf(
16 1
            '%s::%s() must be defined to declare accepted content types',
17 1
            static::class,
18 1
            __FUNCTION__
19
        ));
20
    }
21
22
    /**
23
     * Get the content type of the response body.
24
     *
25
     * @return string
26
     */
27
    abstract protected function type();
28
29
    /**
30
     * Get the response body from the payload.
31
     *
32
     * @param PayloadInterface $payload
33
     *
34
     * @return string
35
     */
36
    abstract protected function body(PayloadInterface $payload);
37
38
    /**
39
     * Get the response status from the payload.
40
     *
41
     * @param PayloadInterface $payload
42
     *
43
     * @return integer
44
     */
45 6
    public function status(PayloadInterface $payload)
46
    {
47 6
        $status = $payload->getStatus();
48
49 6
        if ($status >= PayloadInterface::OK && $status < PayloadInterface::ERROR) {
50 3
            return 200;
51
        }
52
53 3
        if ($status >= PayloadInterface::ERROR && $status < PayloadInterface::INVALID) {
54 1
            return 500;
55
        }
56
57 2
        if ($status >= PayloadInterface::INVALID && $status < PayloadInterface::UNKNOWN) {
58 1
            return 400;
59
        }
60
61 1
        return 520;
62
    }
63
}
64