FileProcessorException::directoryIsNotWritable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Logging\Processors;
14
15
use Exception;
16
use Koded\Exceptions\KodedException;
17
use function date;
18
use function file_put_contents;
19
use function is_dir;
20
use function is_writable;
21
use function mkdir;
22
use function rtrim;
23
use function strtr;
24
use function umask;
25
26
/**
27
 * Log processor for storing the log messages into text files on the disk.
28
 *
29
 *  CONFIGURATION PARAMETERS
30
 *
31
 *      -   dir (string), default: sys_get_temp_dir()
32
 *          The directory for the log files. Must exist and be writable by the PHP.
33
 *
34
 *          NOTE:   the log filename is calculated from of the current
35
 *          YEAR/MONTH/DATE and appended to this directory path (ex: /path/to/logs/2000/01/01.log)
36
 *
37
 *      -   extension (string), default: .log
38
 *          The log file extension.
39
 */
40 3
class File extends Processor
41
{
42 3
    private string $dir = '';
43
    private string $ext = '';
44 3
45 3
    /**
46 3
     * {@inheritdoc}
47
     */
48 3
    public function __construct(array $settings)
49 1
    {
50
        parent::__construct($settings);
51
        umask(umask() | 0002);
52 2
        $this->dir = rtrim((string)$settings['dir'], '/');
53 1
        $this->ext = (string)($settings['extension'] ?? '.log');
54
        if (false === is_dir($this->dir)) {
55
            throw FileProcessorException::directoryDoesNotExist($this->dir);
56 1
        }
57 1
        if (false === is_writable($this->dir)) {
58
            throw FileProcessorException::directoryIsNotWritable($this->dir);
59 1
        }
60
        $this->dir .= '/';
61
    }
62
63 1
    protected function process(array $message): void
64 1
    {
65
        try {
66 1
            // The filename should be calculated at the moment of writing
67 1
            $dir = $this->dir . date('Y/m');
68
            is_dir($dir) || mkdir($dir, 0775, true);
69
70
            file_put_contents($dir . '/' . date('d') . $this->ext, strtr($this->format, $message) . PHP_EOL,
71
                FILE_APPEND);
72
73
            // @codeCoverageIgnoreStart
74 1
        } catch (Exception $e) {
75
            \error_log($e->getMessage());
76
            // @codeCoverageIgnoreEnd
77
        }
78
    }
79
}
80
81
82
class FileProcessorException extends KodedException
83
{
84
    private const
85
        E_DIRECTORY_DOES_NOT_EXIST = 1,
86
        E_DIRECTORY_NOT_WRITABLE = 2;
87
88 1
    protected array $messages = [
89
        self::E_DIRECTORY_DOES_NOT_EXIST => 'Log directory ":dir" must exist',
90 1
        self::E_DIRECTORY_NOT_WRITABLE   => 'Log directory ":dir" must be writable',
91
    ];
92
93 1
    public static function directoryDoesNotExist(string $directory): static
94
    {
95 1
        return new static(static::E_DIRECTORY_DOES_NOT_EXIST, [':dir' => $directory]);
96
    }
97
98
    public static function directoryIsNotWritable(string $directory): static
99
    {
100
        return new static(static::E_DIRECTORY_NOT_WRITABLE, [':dir' => $directory]);
101
    }
102
}
103