Completed
Pull Request — master (#16)
by Akihito
04:55
created

Log::write()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 23
nc 4
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Ackintosh\Snidel;
3
4
use Psr\Log\LoggerInterface;
5
6
date_default_timezone_set('UTC');
7
8
class Log
9
{
10
    /** @var int */
11
    private $ownerPid;
12
13
    /** @var int */
14
    private $masterPid;
15
16
    /** @var  \Psr\Log\LoggerInterface */
17
    private $logger;
18
19
    /**
20
     * @param   int                     $ownerPid
21
     * @param   LoggerInterface | null  $logger
22
     */
23
    public function __construct($ownerPid, $logger)
24
    {
25
        $this->ownerPid = $ownerPid;
26
        $this->logger = $logger;
27
    }
28
29
    /**
30
     * @param int $pid
31
     * @return void
32
     */
33
    public function setMasterPid($pid)
34
    {
35
        $this->masterPid = $pid;
36
    }
37
38
    /**
39
     * creates context
40
     *
41
     * @return array
42
     */
43
    private function context()
44
    {
45
        $pid  = getmypid();
46
        switch ($pid) {
47
            case $this->ownerPid:
48
                $role = 'owner';
49
                break;
50
            case $this->masterPid:
51
                $role = 'master';
52
                break;
53
            default:
54
                $role = 'worker';
55
                break;
56
        }
57
58
        return [
59
            'role' => $role,
60
            'pid'  => $pid,
61
        ];
62
    }
63
64
    /**
65
     * info
66
     *
67
     * @param   string  $message
68
     * @return  void
69
     */
70
    public function info($message)
71
    {
72
        if ($this->logger === null) {
73
            return;
74
        }
75
76
        $this->logger->debug($message, $this->context());
77
    }
78
79
    /**
80
     * error
81
     *
82
     * @param   string  $message
83
     * @return  void
84
     */
85
    public function error($message)
86
    {
87
        if ($this->logger === null) {
88
            return;
89
        }
90
91
        $this->logger->error($message, $this->context());
92
    }
93
}
94