Passed
Branch master (8a6725)
by Michał
02:20
created

File::__construct()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace SimpleLog\Storage;
4
5
use SimpleLog\LogException;
6
7
class File implements StorageInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $params = [];
13
14
    /**
15
     * @param array $params
16
     */
17 14
    public function __construct(array $params)
18
    {
19 14
        $this->params = $params;
20 14
    }
21
22
    /**
23
     * @param string $message
24
     * @param string $level
25
     * @throws \SimpleLog\LogException
26
     * @return $this
27
     */
28 12
    public function store($message, $level)
29
    {
30 12
        $flag = 0;
31 12
        $logFile = $this->params['log_path'] . DIRECTORY_SEPARATOR . $level . '.log';
32
33 12
        if (!file_exists($this->params['log_path'])) {
34 10
            $bool = mkdir($this->params['log_path']);
35
36 10
            if (!$bool) {
37 1
                throw new LogException('Unable to create log directory: ' . $this->params['log_path']);
38
            }
39 9
        }
40
41 11
        if (file_exists($logFile)) {
42 2
            $flag = FILE_APPEND;
43 2
        }
44
45 11
        $bool = file_put_contents($logFile, $message, $flag);
46
47 11
        if (!$bool) {
48 1
            throw new LogException('Unable to save log file: ' . $logFile);
49
        }
50
51 10
        return $this;
52
    }
53
}
54