Failed Conditions
Pull Request — master (#10)
by Maximo
03:34
created

Api   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 29.73%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 80
ccs 11
cts 37
cp 0.2973
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 4
A setup() 0 9 1
B handleException() 0 30 7
1
<?php
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
use Baka\Http\RouterCollection;
17
18
/**
19
 * Class Api
20
 *
21
 * @package Gewaer\Bootstrap
22
 *
23
 * @property Micro $application
24
 */
25
class Api extends AbstractBootstrap
26
{
27
    /**
28
     * Run the application
29
     *
30
     * @return mixed
31
     */
32 1
    public function run()
33
    {
34
        try {
35 1
            $config = $this->container->getConfig()->jwt->toArray();
36
37
            //if the router has jwt ignore url we always overwrite the app config
38 1
            $routerJwtIgnoreUrl = RouterCollection::getJwtIgnoreRoutes();
39 1
            if (!empty($routerJwtIgnoreUrl)) {
40 1
                $config['ignoreUri'] = $routerJwtIgnoreUrl;
41
            } elseif (!$this->container->getConfig()->application->jwtSecurity) {
42
                //ignore token validation if disable
43
                $config['ignoreUri'] = ['regex: *'];
44
            }
45
46
            //JWT Validation
47 1
            new AuthMicro($this->application, $config);
48
49 1
            return $this->application->handle();
50
        } catch (Throwable $e) {
51
            $this->handleException($e)->send();
52
        }
53
    }
54
55
    /**
56
     * Handle the exception we throw from our api
57
     *
58
     * @param Throwable $e
59
     * @return Response
60
     */
61
    public function handleException(Throwable $e): Response
62
    {
63
        $response = new Response();
64
        $request = new Request();
65
        $identifier = $request->getServerAddress();
66
        $config = $this->container->getConfig();
67
68
        $httpCode = (method_exists($e, 'getHttpCode')) ? $e->getHttpCode() : 400;
69
        $httpMessage = (method_exists($e, 'getHttpMessage')) ? $e->getHttpMessage() : 'Bad Request';
70
        $data = (method_exists($e, 'getData')) ? $e->getData() : [];
71
72
        $response->setHeader('Access-Control-Allow-Origin', '*'); //@todo check why this fails on nginx
73
        $response->setStatusCode($httpCode, $httpMessage);
74
        $response->setContentType('application/json');
75
        $response->setJsonContent([
76
            'errors' => [
77
                'type' => $httpMessage,
78
                'identifier' => $identifier,
79
                'message' => $e->getMessage(),
80
                'trace' => strtolower($config->app->env) != Flags::PRODUCTION ? $e->getTraceAsString() : null,
81
                'data' => $data,
82
            ],
83
        ]);
84
85
        //only log when server error production is seerver error or dev
86
        if ($e instanceof ServerErrorHttpException || strtolower($config->app->env) != Flags::PRODUCTION) {
87
            $this->container->getLog()->error($e->getTraceAsString());
88
        }
89
90
        return $response;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96 4
    public function setup()
97
    {
98
        //set the default DI
99 4
        $this->container = new FactoryDefault();
100
        //set all the services
101 4
        $this->providers = require appPath('api/config/providers.php');
102
103
        //run my parents setup
104 4
        parent::setup();
105 4
    }
106
}
107