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

File   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B store() 0 24 5
A __construct() 0 3 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