File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleLog\Storage;
6
7
use SimpleLog\LogException;
8
9
class File implements StorageInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $params = [];
15
16
    /**
17
     * @param array $params
18
     */
19 17
    public function __construct(array $params)
20
    {
21 17
        $this->params = $params;
22 17
    }
23
24
    /**
25
     * @param string $message
26
     * @param string $level
27
     * @throws LogException
28
     * @return $this
29
     */
30 15
    public function store(string $message, string $level): StorageInterface
31
    {
32 15
        $flag = 0;
33 15
        $logFile = $this->params['log_path'] . DIRECTORY_SEPARATOR . $level . '.log';
34
35 15
        if (!\file_exists($this->params['log_path'])) {
36 13
            $bool = @\mkdir($this->params['log_path']);
37
38 13
            if (!$bool) {
39 1
                throw new LogException('Unable to create log directory: ' . $this->params['log_path']);
40
            }
41
        }
42
43 14
        if (\file_exists($logFile)) {
44 3
            $flag = FILE_APPEND;
45
        }
46
47 14
        $bool = @\file_put_contents($logFile, $message, $flag | LOCK_EX);
48
49 14
        if (!$bool) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bool of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50 1
            throw new LogException('Unable to save log file: ' . $logFile);
51
        }
52
53 13
        return $this;
54
    }
55
}
56