LogWatcher::watch()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 16
nc 5
nop 2
1
<?php
2
3
namespace BeubiQA\Application\Lib;
4
5
class LogWatcher
6
{
7
    /**
8
     * @param array $logPath
9
     */
10
    public function watch($logPath, $stringToEcho)
11
    {
12
        $this->checkLogPermissions($logPath);
13
        $size = 0;
14
        while (true) {
15
            clearstatcache();
16
            $currentSize = filesize($logPath);
17
            if ($size === $currentSize) {
18
                usleep(500);
19
                continue;
20
            }
21
            $fh = fopen($logPath, 'r');
22
            fseek($fh, $size);
23
            while ($line = fgets($fh)) {
24
                if (strpos($line, $stringToEcho) !== false) {
25
                    echo $line;
26
                }
27
            }
28
            fclose($fh);
29
            $size = $currentSize;
30
        }
31
    }
32
33
    /**
34
     * @param string $file
35
     *
36
     * @throws \RuntimeException
37
     */
38
    private function checkLogPermissions($file)
39
    {
40
        if (file_exists($file) && !is_writable($file)) {
41
            throw new \RuntimeException('No permissions in '.$file);
42
        }
43
    }
44
}
45