Test Failed
Push — master ( c223c8...a9e334 )
by Maximo
01:54
created

Api::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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