Test Failed
Pull Request — master (#80)
by Maximo
05:41
created

Api   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 66
ccs 6
cts 30
cp 0.2
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

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