EventLogger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 65
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A start() 0 4 1
A stop() 0 3 1
A log() 0 6 1
A writeHeader() 0 6 2
A getHeader() 0 8 1
A getEntry() 0 13 1
1
<?php
2
3
namespace Hubph\Internal;
4
5
use Hubph\EventLoggerInterface;
6
7
class EventLogger implements EventLoggerInterface
8
{
9
    protected $filename;
10
11
    /**
12
     * EventLogger constructor
13
     */
14
    public function __construct($filename)
15
    {
16
        $this->filename = $filename;
17
    }
18
19
    public function start()
20
    {
21
        @unlink($this->filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
22
    }
23
24
    public function stop()
25
    {
26
    }
27
28
    /**
29
     * Write an event into the event log
30
     * @param string $event_name
31
     * @param array $args
32
     * @param array $params
33
     * @param array $response
34
     */
35
    public function log($event_name, $args, $params, $response)
36
    {
37
        $this->writeHeader();
38
        $entry = $this->getEntry($event_name, $args, $params, $response);
39
        file_put_contents($this->filename, $entry, FILE_APPEND);
40
    }
41
42
    protected function writeHeader()
43
    {
44
        if (!file_exists($this->filename)) {
45
            file_put_contents($this->filename, $this->getHeader());
46
        }
47
    }
48
49
    protected function getHeader()
50
    {
51
        return <<<EOT
52
# Event log
53
# ---------
54
55
EOT;
56
    }
57
58
    protected function getEntry($event_name, $args, $params, $response)
59
    {
60
        $args_string = json_encode($args);
61
        $params_string = json_encode($params);
62
        $response_string = json_encode($response);
63
64
        return <<<EOT
65
name    : $event_name
66
args    : $args_string
67
params  : $params_string
68
response: $response_string
69
EOT;
70
    }
71
}
72