Completed
Push — master ( 89cd87...82e012 )
by Tomas
02:29
created

ApiPresenter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 14
c 5
b 0
f 0
lcom 2
cbo 8
dl 10
loc 116
ccs 0
cts 81
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A startup() 0 5 1
B renderDefault() 0 38 5
A getHandler() 0 9 1
A checkAuth() 5 9 2
A processParams() 5 10 2
A logRequest() 0 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tomaj\NetteApi\Presenters;
4
5
use Nette\Application\Responses\JsonResponse;
6
use Nette\Application\UI\Presenter;
7
use Nette\Http\Response;
8
use Tomaj\NetteApi\ApiDecider;
9
use Tomaj\NetteApi\Misc\IpDetectorInterface;
10
use Tomaj\NetteApi\Params\ParamsProcessor;
11
use Exception;
12
use Tomaj\NetteApi\Response\JsonApiResponse;
13
14
/**
15
 * @property-read \Nette\DI\Container $context
16
 */
17
class ApiPresenter extends Presenter
18
{
19
    /**
20
     * @var  ApiDecider @inject
21
     */
22
    public $apiDecider;
23
24
    /**
25
     * @var  IpDetectorInterface @inject
26
     */
27
    public $ipDetector;
28
29
    public function startup()
30
    {
31
        parent::startup();
32
        $this->autoCanonicalize = false;
33
    }
34
35
    /**
36
     * Nette render default method
37
     *
38
     * @return void
39
     */
40
    public function renderDefault()
41
    {
42
        $start = microtime(true);
43
44
        $this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*');
45
46
        $hand = $this->getHandler();
47
        $handler = $hand['handler'];
48
        $authorization = $hand['authorization'];
49
50
        if ($this->checkAuth($authorization) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
51
            return;
52
        }
53
54
        $params = $this->processParams($handler);
55
        if ($params == false) {
56
            return;
57
        }
58
59
        // process handler
60
        try {
61
            $response = $handler->handle($params);
62
            $code = $response->getCode();
63
        } catch (Exception $exception) {
64
            $response = new JsonApiResponse(500, ['status' => 'error', 'message' => 'Internal server error']);
65
            $code = $response->getCode();
66
        }
67
68
        $end = microtime(true);
69
70
        if ($this->context->hasService('apiLogger')) {
71
            $this->logRequest($this->context->getService('apiLogger'), $code, $end-$start);
72
        }
73
74
        // output to nette
75
        $this->getHttpResponse()->setCode($code);
76
        $this->sendResponse($response);
77
    }
78
79
    private function getHandler()
80
    {
81
        return $this->apiDecider->getApiHandler(
82
            $this->getRequest()->getMethod(),
83
            $this->params['version'],
84
            $this->params['package'],
85
            $this->params['apiAction']
86
        );
87
    }
88
89
    private function checkAuth($authorization)
90
    {
91 View Code Duplication
        if (!$authorization->authorized()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            $this->getHttpResponse()->setCode(Response::S403_FORBIDDEN);
93
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]));
94
            return false;
95
        }
96
        return true;
97
    }
98
99
    private function processParams($handler)
100
    {
101
        $paramsProcessor = new ParamsProcessor($handler->params());
102 View Code Duplication
        if ($paramsProcessor->isError()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR);
104
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input']));
105
            return false;
106
        }
107
        return $paramsProcessor->getValues();
108
    }
109
110
    private function logRequest($logger, $code, $elapsed)
111
    {
112
        $headers = [];
113
        if (function_exists('getallheaders')) {
114
            $headers = getallheaders();
115
        }
116
117
        $requestHeaders = '';
118
        foreach ($headers as $key => $value) {
119
            $requestHeaders .= "$key: $value\n";
120
        }
121
122
        $logger->log(
123
            $code,
124
            $this->getRequest()->getMethod(),
125
            $requestHeaders,
126
            filter_input(INPUT_SERVER, 'REQUEST_URI'),
127
            $this->ipDetector->getRequestIp(),
128
            filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'),
129
            ($elapsed) * 1000
130
        );
131
    }
132
}
133