Failed Conditions
Push — master ( 385b2f...f2f4ce )
by Rafael
16s queued 13s
created

SwooleResponse::handleException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * thanks to https://github.com/limingxinleo.
5
 */
6
7
namespace Canvas\Http;
8
9
use Exception;
10
use Phalcon\Http\Cookie;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Cookie was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Phalcon\Http\Response as PhResponse;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use swoole_http_response;
0 ignored issues
show
Bug introduced by
The type swoole_http_response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Throwable;
14
15
class SwooleResponse extends Response
16
{
17
    protected $response;
18
19
    /**
20
     * Set the swoole response object.
21
     *
22
     * @param swoole_http_response $response
23
     *
24
     * @return void
25
     */
26
    public function init(swoole_http_response $response) : void
27
    {
28
        $this->response = $response;
29
        $this->_sent = false;
0 ignored issues
show
Bug Best Practice introduced by
The property _sent does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->_content = null;
0 ignored issues
show
Bug Best Practice introduced by
The property _content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->setStatusCode(200);
32
    }
33
34
    /**
35
     * Send the response.
36
     *
37
     * @return PhResponse
38
     */
39
    public function send() : PhResponse
40
    {
41
        if ($this->_sent) {
42
            throw new Exception('Response was already sent');
43
        }
44
45
        $this->_sent = true;
0 ignored issues
show
Bug Best Practice introduced by
The property _sent does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        // get phalcon headers
47
        $headers = $this->getHeaders();
48
49
        foreach ($headers->toArray() as $key => $val) {
50
            //if the key has spaces this breaks postman, so we remove this headers
51
            //example: HTTP/1.1 200 OK || HTTP/1.1 401 Unauthorized
52
            if (!preg_match('/\s/', $key)) {
53
                $this->response->header($key, $val);
54
            }
55
        }
56
57
        /** @var Cookies $cookies */
58
        $cookies = $this->getCookies();
59
        if ($cookies) {
0 ignored issues
show
introduced by
$cookies is of type Canvas\Http\Cookies, thus it always evaluated to true.
Loading history...
60
            /** @var Cookie $cookie */
61
            foreach ($cookies->getCookies() as $cookie) {
62
                $this->response->cookie(
63
                    $cookie->getName(),
64
                    $cookie->getValue(),
65
                    $cookie->getExpiration(),
66
                    $cookie->getPath(),
67
                    $cookie->getDomain(),
68
                    $cookie->getSecure(),
69
                    $cookie->getHttpOnly()
70
                );
71
            }
72
        }
73
74
        //set swoole response
75
        $this->response->status($this->getStatusCode());
76
        $this->response->end($this->_content);
77
78
        //reset di
79
        $this->resetDi();
80
81
        return $this;
82
    }
83
84
    /**
85
     * Handle the exception we throw from our api.
86
     *
87
     * @param Throwable $e
88
     *
89
     * @return Response
90
     */
91
    public function handleException(Throwable $e) : Response
92
    {
93
        //reset di
94
        $response = parent::handleException($e);
95
        $this->resetDi();
96
97
        return $response;
98
    }
99
100
    /**
101
     * Given Swoole behavior we need to reset the DI and Close the DB connection
102
     * What happens if you don't do this? You will cache the DI request and always get the same info ;).
103
     *
104
     * @return void
105
     */
106
    protected function resetDi() : void
107
    {
108
        $this->_sent = false;
0 ignored issues
show
Bug Best Practice introduced by
The property _sent does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
        $this->getDi()->get('db')->close();
110
        $this->getDi()->reset();
111
    }
112
}
113