Passed
Push — master ( 1000a3...064e0b )
by Radu
01:41
created

AbstractFileLogger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A __construct() 0 13 3
A getLastLine() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Log;
6
7
use WebServCo\Framework\Exceptions\LoggerException;
8
9
abstract class AbstractFileLogger extends AbstractLogger implements \WebServCo\Framework\Interfaces\FileLoggerInterface
10
{
11
    protected string $channel;
12
    protected string $logDir;
13
    protected string $logPath;
14
15
    public function __construct(string $channel, string $logDir)
16
    {
17
        $this->channel = $channel;
18
        $this->logDir = $logDir;
19
20
        if (!$this->logDir) {
21
            throw new LoggerException(\sprintf('Log directory not set for channel "%s".', $channel));
22
        }
23
24
        if (!\is_writable($this->logDir)) {
25
            throw new LoggerException(\sprintf('Log directory not writeable: %s.', $this->logDir));
26
        }
27
        $this->logPath = \sprintf('%s%s.log', $this->logDir, $this->channel);
28
    }
29
30
    public function clear(): bool
31
    {
32
        return (bool) \file_put_contents($this->logPath, null);
33
    }
34
35
    public function getLastLine(): int
36
    {
37
        $file = new \SplFileObject($this->logPath, 'r');
38
        $file->seek(\PHP_INT_MAX);
39
        return $file->key();
40
    }
41
}
42