Completed
Push — master ( 526d6e...2376dc )
by Akihito
03:04
created

Snidel_Log   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Log::__construct() 0 4 1
A Log::setDestination() 0 4 1
A Log::write() 0 18 3
A Log::info() 0 4 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
class Log
5
{
6
    /** @var int */
7
    private $ownerPid;
8
9
    /** @var resource */
10
    private $destination;
11
12
    /**
13
     * @param   int     $ownerPid
14
     */
15
    public function __construct($ownerPid)
16
    {
17
        $this->ownerPid = $ownerPid;
18
    }
19
20
    /**
21
     * sets the resource for the log.
22
     *
23
     * @param   resource    $resource
24
     * @return  void
25
     */
26
    public function setDestination($resource)
27
    {
28
        $this->destination = $resource;
29
    }
30
31
    /**
32
     * writes log
33
     *
34
     * @param   string  $type
35
     * @param   string  $message
36
     * @return  void
37
     */
38
    private function write($type, $message)
39
    {
40
        if ($this->destination === null) {
41
            return;
42
        }
43
        $pid = getmypid();
44
        fputs(
45
            $this->destination,
46
            sprintf(
47
                '[%s][%s][%d(%s)] %s',
48
                date('Y-m-d H:i:s'),
49
                $type,
50
                $pid,
51
                ($this->ownerPid === $pid) ? 'p' : 'c',
52
                $message . PHP_EOL
53
            )
54
        );
55
    }
56
57
    /**
58
     * writes log
59
     *
60
     * @param   string  $message
61
     * @return  void
62
     */
63
    public function info($message)
64
    {
65
        $this->write('info', $message);
66
    }
67
68
    /**
69
     * writes log
70
     *
71
     * @param   string  $message
72
     * @return  void
73
     */
74
    public function error($message)
75
    {
76
        $this->write('error', $message);
77
    }
78
}
79