LogWatcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B watch() 0 22 5
A checkLogPermissions() 0 6 3
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