LogExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
ccs 0
cts 21
cp 0
wmc 3
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A implementedEvents() 0 7 1
A beforeProcess() 0 5 1
A afterProcess() 0 15 1
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Extension;
13
14
use CakeDC\Api\Service\Action\Action;
15
use CakeDC\Api\Service\Service;
16
use Cake\Event\Event;
17
use Cake\Event\EventListenerInterface;
18
use Cake\Log\LogTrait;
19
use Psr\Log\LogLevel;
20
21
class LogExtension extends Extension implements EventListenerInterface
22
{
23
    use LogTrait;
24
25
    /**
26
     * @var Service
27
     */
28
    protected $_service;
29
30
    /**
31
     * @var Action
32
     */
33
    protected $_action;
34
35
    /**
36
     * @var int
37
     */
38
    protected $_timer;
39
40
    /**
41
     * Returns a list of events this object is implementing. When the class is registered
42
     * in an event manager, each individual method will be associated with the respective event.
43
     *
44
     * @return array
45
     */
46
    public function implementedEvents()
47
    {
48
        return [
49
            'Service.beforeDispatch' => 'beforeProcess',
50
            'Service.afterDispatch' => 'afterProcess',
51
        ];
52
    }
53
54
    /**
55
     * before process
56
     *
57
     * @param Event $event An Event instance.
58
     * @return void
59
     */
60
    public function beforeProcess(Event $event)
61
    {
62
        $this->_service = $event->getData('service');
63
        $this->_timer = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
The property $_timer was declared of type integer, but microtime(true) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
64
    }
65
66
    /**
67
     * after process
68
     *
69
     * @param Event $event An Event instance.
70
     * @return void
71
     */
72
    public function afterProcess(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        $duration = round((microtime(true) - $this->_timer) * 1000, 0);
75
        $url = $this->_service->getBaseUrl();
76
        $data = $this->_service->getParser()->getParams();
77
        $result = $this->_service->getResult()->toArray();
78
        $log = [
79
            'url' => $url,
80
            'method' => env('REQUEST_METHOD'),
81
            'duration' => $duration . 'ms',
82
            'input' => $data,
83
            'result' => json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE),
84
        ];
85
        $this->log($log, LogLevel::INFO, ['api']);
86
    }
87
}
88