Completed
Push — master ( 118e9a...c92c25 )
by Matthew
02:01
created

AbstractFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Equip\Formatter;
3
4
use Equip\Adr\PayloadInterface;
5
use Lukasoppermann\Httpstatus\Httpstatus;
6
7
abstract class AbstractFormatter
8
{
9
    /**
10
     * @var Httpstatus
11
     */
12
    private $http_status;
13
14
    /**
15
     * @param Httpstatus $http_status
16
     */
17 25
    public function __construct(Httpstatus $http_status)
18
    {
19 25
        $this->http_status = $http_status;
20 25
    }
21
22
    /**
23
     * Get the content types this formatter can satisfy.
24
     *
25
     * @return array
26
     */
27 1
    public static function accepts()
28
    {
29 1
        throw new \RuntimeException(sprintf(
30 1
            '%s::%s() must be defined to declare accepted content types',
31 1
            static::class,
32
            __FUNCTION__
33 1
        ));
34
    }
35
36
    /**
37
     * Get the content type of the response body.
38
     *
39
     * @return string
40
     */
41
    abstract protected function type();
42
43
    /**
44
     * Get the response body from the payload.
45
     *
46
     * @param PayloadInterface $payload
47
     *
48
     * @return string
49
     */
50
    abstract protected function body(PayloadInterface $payload);
51
52
    /**
53
     * Get the response status from the payload.
54
     *
55
     * @param PayloadInterface $payload
56
     *
57
     * @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...
58
     */
59 17
    public function status(PayloadInterface $payload)
60
    {
61 17
        $status = $payload->getStatus();
62
63
        // Legacy logic
64
        // @todo Remove this in 2.0
65 17
        if (is_int($status)) {
66 6
            if ($status >= PayloadInterface::OK && $status < PayloadInterface::ERROR) {
2 ignored issues
show
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::OK has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::ERROR has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
67 3
                return 200;
68
            }
69 3
            if ($status >= PayloadInterface::ERROR && $status < PayloadInterface::INVALID) {
2 ignored issues
show
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::ERROR has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::INVALID has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
70 1
                return 500;
71
            }
72 2
            if ($status >= PayloadInterface::INVALID && $status < PayloadInterface::UNKNOWN) {
2 ignored issues
show
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::INVALID has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant Equip\Adr\PayloadInterface::UNKNOWN has been deprecated with message: 1.1.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
73 1
                return 400;
74
            }
75 1
            return 520;
76
        }
77
78 11
        return $this->http_status->getStatusCode($status);
79
    }
80
}
81