Test Failed
Push — master ( baab8c...6507f4 )
by Maximo
01:50
created

Response::setPayloadError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Http;
6
7
use Phalcon\Http\Response as PhResponse;
8
use Phalcon\Mvc\Model\MessageInterface as ModelMessage;
9
use Phalcon\Validation\Message\Group as ValidationMessage;
10
11
class Response extends PhResponse
12
{
13
    const OK = 200;
14
    const CREATED = 201;
15
    const ACCEPTED = 202;
16
    const MOVED_PERMANENTLY = 301;
17
    const FOUND = 302;
18
    const TEMPORARY_REDIRECT = 307;
19
    const PERMANENTLY_REDIRECT = 308;
20
    const BAD_REQUEST = 400;
21
    const UNAUTHORIZED = 401;
22
    const FORBIDDEN = 403;
23
    const NOT_FOUND = 404;
24
    const NOT_ACCEPTABLE = 406;
25
    const INTERNAL_SERVER_ERROR = 500;
26
    const NOT_IMPLEMENTED = 501;
27
    const BAD_GATEWAY = 502;
28
29
    private $codes = [
30
        200 => 'OK',
31
        301 => 'Moved Permanently',
32
        302 => 'Found',
33
        307 => 'Temporary Redirect',
34
        308 => 'Permanent Redirect',
35
        400 => 'Bad Request',
36
        401 => 'Unauthorized',
37
        403 => 'Forbidden',
38
        404 => 'Not Found',
39
        500 => 'Internal Server Error',
40
        501 => 'Not Implemented',
41
        502 => 'Bad Gateway',
42
    ];
43
44
    /**
45
     * Returns the http code description or if not found the code itself
46
     * @param int $code
47
     *
48
     * @return int|string
49
     */
50
    public function getHttpCodeDescription(int $code)
51
    {
52
        if (true === isset($this->codes[$code])) {
53
            return sprintf('%d (%s)', $code, $this->codes[$code]);
54
        }
55
56
        return $code;
57
    }
58
59
    /**
60
     * Send the response back
61
     *
62
     * @return PhResponse
63
     */
64
    public function send(): PhResponse
65
    {
66
        $content = $this->getContent();
67
        $timestamp = date('c');
68
        $hash = sha1($timestamp . $content);
69
        $eTag = sha1($content);
70
71
        /** @var array $content */
72
        $content = json_decode($this->getContent(), true);
73
74
        $jsonapi = [
75
            'jsonapi' => [
76
                'version' => '1.0',
77
            ],
78
        ];
79
        $meta = [
80
            'meta' => [
81
                'timestamp' => $timestamp,
82
                'hash' => $hash,
83
            ]
84
        ];
85
86
        /**
87
         * Join the array again
88
         */
89
        $data = $jsonapi + $content + $meta;
90
        $this
91
            ->setHeader('E-Tag', $eTag)
92
            ->setJsonContent($data);
93
94
        return parent::send();
95
    }
96
97
    /**
98
     * Sets the payload code as Error
99
     *
100
     * @param string $detail
101
     *
102
     * @return Response
103
     */
104
    public function setPayloadError(string $detail = ''): Response
105
    {
106
        $this->setJsonContent(['errors' => [$detail]]);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Traverses the errors collection and sets the errors in the payload
113
     *
114
     * @param ModelMessage[]|ValidationMessage $errors
115
     *
116
     * @return Response
117
     */
118
    public function setPayloadErrors($errors): Response
119
    {
120
        $data = [];
121
        foreach ($errors as $error) {
122
            $data[] = $error->getMessage();
123
        }
124
125
        $this->setJsonContent(['errors' => $data]);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Sets the payload code as Success
132
     *
133
     * @param null|string|array $content The content
134
     *
135
     * @return Response
136
     */
137
    public function setPayloadSuccess($content = []): Response
138
    {
139
        $data = (true === is_array($content)) ? $content : ['data' => $content];
140
        $data = (true === isset($data['data'])) ? $data : ['data' => $data];
141
142
        $this->setJsonContent($data);
143
144
        return $this;
145
    }
146
}
147