SimpleFile::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 3
nop 2
crap 6
1
<?php
2
/*
3
 * This file is part of the phpflo/flowtrace package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
namespace PhpFlo\Logger;
12
13
use Psr\Log\AbstractLogger;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\LogLevel;
16
17
/**
18
 * Class SimpleFile
19
 *
20
 * @package PhpFlo\Logger
21
 * @author Marc Aschmann <[email protected]>
22
 */
23
class SimpleFile extends AbstractLogger implements LoggerInterface
24
{
25
    const DEFAULT_FILENAME = 'flow.log';
26
27
    /**
28
     * @var string
29
     */
30
    private $logFile;
31
32
    /**
33
     * @var array
34
     */
35
    private $levels;
36
37
    /**
38
     * @param string $logFile path/filename to log to.
39
     * @param string $level PSR3 loglevel to log
40 3
     */
41
    public function __construct(string $logFile, string $level = LogLevel::INFO)
42 3
    {
43
        $log = pathinfo($logFile);
44 3
        // check if $logFile is a dir because of possible stream url
45 3
        $isLogdir = is_dir($logFile);
46 1
        if (!$isLogdir && (isset($log['dirname']) && !is_dir($log['dirname']))) {
47 1
            throw new \InvalidArgumentException(
48 1
                "Directory does not exist: {$log['dirname']}"
49
            );
50
        }
51 2
52 1
        if ($isLogdir || !isset($log['filename'])) {
53 1
            $logFile = $logFile . DIRECTORY_SEPARATOR . self::DEFAULT_FILENAME;
54 2
        }
55
        $this->logFile = $logFile;
56 2
57 2
        $this->prepareLogLevels($level);
58
    }
59
60
    /**
61
     * Logs with an arbitrary level.
62
     *
63
     * @param mixed $level
64
     * @param string $message
65
     * @param array $context
66
     *
67
     * @return void
68 2
     */
69
    public function log($level, $message, array $context = array())
70 2
    {
71 2
        if (in_array($level, $this->levels)) {
72 2
            file_put_contents(
73 2
                $this->logFile, $message . PHP_EOL, FILE_APPEND
74 2
            );
75 2
        }
76
    }
77
78
    /**
79
     * Prepare log levels for logfile
80
     *
81
     * @param string $level
82 2
     */
83
    private function prepareLogLevels(string $level)
84
    {
85 2
        $levels = [
86 2
            LogLevel::EMERGENCY,
87 2
            LogLevel::ALERT,
88 2
            LogLevel::CRITICAL,
89 2
            LogLevel::ERROR,
90 2
            LogLevel::WARNING,
91 2
            LogLevel::NOTICE,
92 2
            LogLevel::INFO,
93 2
            LogLevel::DEBUG,
94
        ];
95 2
96
        $key = array_search($level, $levels);
97 2
98 2
        if (null !== $key) {
99 2
            $this->levels = array_slice($levels, 0, $key + 1);
100 2
        }
101
    }
102
}
103