Completed
Push — master ( 673fc6...a88edb )
by Oleg
03:40
created

FileDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A sendMessage() 0 8 2
A __destruct() 0 6 2
1
<?php /** MicroFileDriver */
2
3
namespace Micro\Logger\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * FileDriver logger class file.
9
 *
10
 * Writer logs in file
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Logger\Driver
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class FileDriver extends LoggerDriver
22
{
23
    /** @var resource $connect File handler */
24
    protected $connect;
25
26
27
    /**
28
     * Open file for write messages
29
     *
30
     * @access public
31
     *
32
     * @param array $params configuration params
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct(array $params = [])
38
    {
39
        parent::__construct($params);
40
41
        if (is_writable($params['filename']) || is_writable(dirname($params['filename']))) {
42
            $this->connect = fopen($params['filename'], 'a+');
43
        } else {
44
            throw new Exception('Directory or file "'.$params['filename'].'" is read-only');
45
        }
46
    }
47
48
    /**
49
     * Send message in log
50
     *
51
     * @access public
52
     *
53
     * @param integer $level level number
54
     * @param string $message message to write
55
     *
56
     * @result void
57
     * @throws \Micro\Base\Exception
58
     */
59
    public function sendMessage($level, $message)
60
    {
61
        if (is_resource($this->connect)) {
62
            fwrite($this->connect, '['.date('H:i:s d.m.Y').'] '.ucfirst($level).": {$message}\n");
63
        } else {
64
            throw new Exception('Error write log in file.');
65
        }
66
    }
67
68
    /**
69
     * Close opened for messages file
70
     *
71
     * @access public
72
     * @result void
73
     */
74
    public function __destruct()
75
    {
76
        if (is_resource($this->connect)) {
77
            fclose($this->connect);
78
        }
79
    }
80
}
81