File::log()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
/**
5
 * Micro
6
 *
7
 * @author    Raffael Sahli <[email protected]>
8
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
9
 * @license   MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Log\Adapter;
13
14
class File extends AbstractAdapter
15
{
16
    /**
17
     * Filename
18
     *
19
     * @var string
20
     */
21
    protected $file = '/tmp/out.log';
22
23
24
    /**
25
     * Filename
26
     *
27
     * @var resource
28
     */
29
    protected $resource;
30
31
32
    /**
33
     * Set options
34
     *
35
     * @return  AdapterInterface
36
     */
37
    public function setOptions(? Iterable $config = null) : AdapterInterface
38
    {
39
        if ($config === null) {
40
            $this->resource = fopen($this->file, 'a');
41
            return $this;
42
        }
43
44
        foreach ($config as $option => $value) {
45
            switch ($option) {
46
                case 'file':
47
                    $this->file = str_replace('APPLICATION_PATH', APPLICATION_PATH, (string)$value);
48
                    unset($config[$option]);
49
                break;
50
            }
51
        }
52
53
        parent::setOptions($config);
54
55
        $this->resource = fopen($this->file, 'a');
56
        return $this;
57
    }
58
59
60
    /**
61
     * Log
62
     *
63
     * @param   int $priority
64
     * @param   string $message
65
     * @return  bool
66
     */
67
    public function log(string $priority, string $message): bool
68
    {
69
        if (!is_resource($this->resource)) {
70
            return false;
71
        }
72
73
        $result = fwrite($this->resource, $message."\n");
74
        return (bool)$result;
75
    }
76
}
77