MonitorTask::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/10/14
6
 * Time: 9:34
7
 */
8
9
namespace Jenner\LogMonitor;
10
11
12
use Jenner\LogMonitor\Filter\FilterInterface;
13
use Jenner\LogMonitor\Notification\NotificationInterface;
14
use Jenner\LogMonitor\Reader\ReaderInterface;
15
use Jenner\SimpleFork\Process;
16
17
class MonitorTask extends Process
18
{
19
    /**
20
     * @var ReaderInterface
21
     */
22
    protected $reader;
23
24
    /**
25
     * @var FilterInterface
26
     */
27
    protected $filter;
28
29
    /**
30
     * @var NotificationInterface
31
     */
32
    protected $notify;
33
34
    /**
35
     * @param ReaderInterface $reader
36
     * @param FilterInterface $filter
37
     * @param NotificationInterface $notify
38
     */
39
    public function __construct(
40
        ReaderInterface $reader,
41
        FilterInterface $filter,
42
        NotificationInterface $notify
43
    )
44
    {
45
        parent::__construct();
46
        $this->reader = $reader;
47
        $this->filter = $filter;
48
        $this->notify = $notify;
49
50
        $this->registerSignalHandler(SIGTERM, array($this, 'signalHandler'));
51
    }
52
53
    /**
54
     * sub process run
55
     */
56
    public function run()
57
    {
58
        $this->reader->open();
59
        while (true) {
60
            $line = $this->reader->read();
61
            if ($line === false) {
62
                usleep(200);
63
                continue;
64
            }
65
            if ($this->filter->filter($line)) {
66
                $message = $this->filter->getErrorMessage($line, $this->reader);
67
                $this->notify->send($this->reader->info(), $message);
68
            }
69
        }
70
    }
71
72
    protected function signalHandler($signal)
73
    {
74
        switch ($signal) {
75
            case SIGTERM :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76
                $this->reader->close();
77
                unset($this->reader);
78
                unset($this->filter);
79
                unset($this->notify);
80
                exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method signalHandler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
81
        }
82
    }
83
}