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

Api::handleException()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 16
nop 1
dl 0
loc 30
rs 8.6666
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\Bootstrap;
6
7
use function Gewaer\Core\appPath;
8
use Phalcon\Di\FactoryDefault;
9
use Phalcon\Mvc\Micro;
10
use Gewaer\Http\Response;
11
use Phalcon\Http\Request;
12
use Throwable;
13
use Dmkit\Phalcon\Auth\Middleware\Micro as AuthMicro;
14
use Gewaer\Exception\ServerErrorHttpException;
15
use Gewaer\Constants\Flags;
16
17
/**
18
 * Class Api
19
 *
20
 * @package Gewaer\Bootstrap
21
 *
22
 * @property Micro $application
23
 */
24
class Api extends AbstractBootstrap
25
{
26
    /**
27
     * Run the application
28
     *
29
     * @return mixed
30
     */
31
    public function run()
32
    {
33
        try {
34
            $config = $this->container->getConfig()->jwt->toArray();
35
36
            //ignore token validation if disable
37
            if (!$this->container->getConfig()->application->jwtSecurity) {
38
                $config['ignoreUri'] = ['regex: *'];
39
            }
40
41
            //JWT Validation
42
            $auth = new AuthMicro($this->application, $config);
0 ignored issues
show
Unused Code introduced by
The assignment to $auth is dead and can be removed.
Loading history...
43
44
            return $this->application->handle();
45
        } catch (Throwable $e) {
46
            $this->handleException($e)->send();
47
        }
48
    }
49
50
    /**
51
     * Handle the exception we throw from our api
52
     *
53
     * @param Throwable $e
54
     * @return Response
55
     */
56
    public function handleException(Throwable $e): Response
57
    {
58
        $response = new Response();
59
        $request = new Request();
60
        $identifier = $request->getServerAddress();
61
        $config = $this->container->getConfig();
62
63
        $httpCode = (method_exists($e, 'getHttpCode')) ? $e->getHttpCode() : 400;
64
        $httpMessage = (method_exists($e, 'getHttpMessage')) ? $e->getHttpMessage() : 'Bad Request';
65
        $data = (method_exists($e, 'getData')) ? $e->getData() : [];
66
67
        $message = $e->getMessage();
0 ignored issues
show
Unused Code introduced by
The assignment to $message is dead and can be removed.
Loading history...
68
        $response->setStatusCode($httpCode, $httpMessage);
69
        $response->setContentType('application/json');
70
        $response->setJsonContent([
71
            'errors' => [
72
                'type' => $httpMessage,
73
                'identifier' => $identifier,
74
                'message' => $e->getMessage(),
75
                'trace' => strtolower($config->app->env) != Flags::PRODUCTION ? $e->getTraceAsString() : null,
76
                'data' => $data,
77
            ],
78
        ]);
79
80
        //only log when server error production is seerver error or dev
81
        if ($e instanceof ServerErrorHttpException || strtolower($config->app->env) != Flags::PRODUCTION) {
82
            $this->container->getLog()->error($e);
83
        }
84
85
        return $response;
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91
    public function setup()
92
    {
93
        //set the default DI
94
        $this->container = new FactoryDefault();
95
        //set all the services
96
        $this->providers = require appPath('api/config/providers.php');
97
98
        //run my parents setup
99
        parent::setup();
100
    }
101
}
102