Test Failed
Push — master ( 3b9b23...4cf4fc )
by Maximo
02:05
created

Api   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 77
ccs 10
cts 35
cp 0.2857
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 9 1
B handleException() 0 31 7
A run() 0 16 3
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 1
    public function run()
32
    {
33
        try {
34 1
            $config = $this->container->getConfig()->jwt->toArray();
35
36
            //ignore token validation if disable
37 1
            if (!$this->container->getConfig()->application->jwtSecurity) {
38 1
                $config['ignoreUri'] = ['regex: *'];
39
            }
40
41
            //JWT Validation
42 1
            $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 1
            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 4
    public function setup()
93
    {
94
        //set the default DI
95 4
        $this->container = new FactoryDefault();
96
        //set all the services
97 4
        $this->providers = require appPath('api/config/providers.php');
98
99
        //run my parents setup
100 4
        parent::setup();
101 4
    }
102
}
103