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

AbstractFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A accepts() 0 8 1
type() 0 1 ?
body() 0 1 ?
B status() 0 18 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