Completed
Push — master ( 952773...a13902 )
by Evgeny
02:39
created

LogExtension::beforeProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
/**
3
 * Copyright 2016, 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, 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 Cake\Log\LogTrait;
15
use Cake\Event\Event;
16
use Cake\Event\EventListenerInterface;
17
use CakeDC\Api\Service\Service;
18
use CakeDC\Api\Service\Action\Action;
19
20
class LogExtension extends Extension implements EventListenerInterface
21
{
22
    use LogTrait;
23
24
    /**
25
     * @var Service
26
     */
27
    protected $_service;
28
29
    /**
30
     * @var Action
31
     */
32
    protected $_action;
33
34
    /**
35
     * @var int
36
     */
37
    protected $_timer;
38
39
    /**
40
     * Returns a list of events this object is implementing. When the class is registered
41
     * in an event manager, each individual method will be associated with the respective event.
42
     *
43
     * @return array
44
     */
45
    public function implementedEvents()
46
    {
47
        return [
48
            'Service.beforeDispatch' => 'beforeProcess',
49
            'Service.afterDispatch' => 'afterProcess',
50
        ];
51
    }
52
53
    /**
54
     * before process
55
     *
56
     * @param Event $event An Event instance.
57
     * @return void
58
     */
59
    public function beforeProcess(Event $event)
60
    {
61
        $this->_service = $event->data['service'];
62
        $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...
63
    }
64
65
    /**
66
     * after process
67
     *
68
     * @param Event $event An Event instance.
69
     * @return void
70
     */
71
    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...
72
    {
73
        $duration = round((microtime(true) - $this->_timer) * 1000, 0);
74
        $url = $this->_service->baseUrl();
75
        $data = $this->_service->parser()->params();
76
        $result = $this->_service->result()->toArray();
77
        $log = [
78
            'url' => $url,
79
            'method' => env('REQUEST_METHOD'),
80
            'duration' => $duration . 'ms',
81
            'input' => $data,
82
            'result' => json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE),
83
        ];
84
        $this->log($log);
85
    }
86
}
87