Test Failed
Push — master ( 02204c...466d37 )
by Maximo
03:15
created

Api::run()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 9
nop 0
dl 0
loc 16
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\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->setHeader('Access-Control-Allow-Origin', '*'); //@todo check why this fales on nginx
69
        $response->setStatusCode($httpCode, $httpMessage);
70
        $response->setContentType('application/json');
71
        $response->setJsonContent([
72
            'errors' => [
73
                'type' => $httpMessage,
74
                'identifier' => $identifier,
75
                'message' => $e->getMessage(),
76
                'trace' => strtolower($config->app->env) != Flags::PRODUCTION ? $e->getTraceAsString() : null,
77
                'data' => $data,
78
            ],
79
        ]);
80
81
        //only log when server error production is seerver error or dev
82
        if ($e instanceof ServerErrorHttpException || strtolower($config->app->env) != Flags::PRODUCTION) {
83
            $this->container->getLog()->error($e);
84
        }
85
86
        return $response;
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    public function setup()
93
    {
94
        //set the default DI
95
        $this->container = new FactoryDefault();
96
        //set all the services
97
        $this->providers = require appPath('api/config/providers.php');
98
99
        //run my parents setup
100
        parent::setup();
101
    }
102
}
103