Completed
Push — master ( 77f972...7eeee3 )
by Mauro
02:23
created

BaseController::logResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace App\Controller;
4
5
/**
6
 * Base Controller.
7
 */
8
abstract class BaseController
9
{
10
    const API_VERSION = '17.05';
11
12
    protected $logger;
13
14
    protected $database;
15
16
    protected $request;
17
18
    protected $response;
19
20
    protected $args;
21
22
    /**
23
     * @param Request $request
24
     * @param Response $response
25
     * @param array $args
26
     */
27
    protected function setParams($request, $response, $args)
28
    {
29
        $this->request = $request;
30
        $this->response = $response;
31
        $this->args = $args;
32
        $this->logRequest();
33
    }
34
35
    /**
36
     * Send response with json as standard format.
37
     *
38
     * @param string $status
39
     * @param mixed $message
40
     * @param int $code
41
     * @return array $response
42
     */
43
    protected function jsonResponse($status, $message, $code)
44
    {
45
        $result = [
46
            'code' => $code,
47
            'status' => $status,
48
            'message' => $message,
49
        ];
50
        $this->logResponse($result);
51
52
        return $this->response->withJson($result, $code, JSON_PRETTY_PRINT);
53
    }
54
55
    /**
56
     * Log each request.
57
     */
58
    protected function logRequest()
59
    {
60
        $routeInfo = $this->request->getAttribute('routeInfo');
61
        $route = $this->request->getAttribute('route');
62
        $this->logger->info('************');
63
        $this->logger->info('* REQUEST: ' . $route->getCallable()[1]);
64
        $this->logger->info('* ' . $route->getMethods()[0] . ' ' . $routeInfo['request'][1]);
65
        $this->logger->info('* BODY: ' . json_encode($this->request->getParsedBody()));
66
//        $this->logger->info('* ARGS: ' . json_encode($this->args));
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
    }
68
69
    /**
70
     * Log each response.
71
     *
72
     * @param array $response
73
     */
74
    protected function logResponse($response)
75
    {
76
        $this->logger->info('* RESPONSE: ' . json_encode($response));
77
        $this->logger->info('************');
78
    }
79
}
80