Driver_File   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B store() 0 25 3
1
<?php
2
3
/**
4
 * Provides logging functionality on the filesystem
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Logs
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\logs;
17
18
/**
19
 * Provides logging functionality on the filesystem
20
 *
21
 * @category  Core
22
 * @package   Logs
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Driver_File extends Base
30
{
31
    /**
32
     * Stores the logs directory
33
     **/
34
    private $_dir = '';
35
36
    /**
37
     * Creates the logs handler object
38
     *
39
     * @param array $config Configuration details as an array
40
     *
41
     * @throws \Exception
42
     *
43
     * @return \csphere\core\logs\Driver_File
44
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
45
46
    public function __construct(array $config)
47
    {
48
        parent::__construct($config);
49
50
        $this->_dir = \csphere\core\init\path() . 'csphere/storage/logs/';
51
52
        if (!is_writeable($this->_dir)) {
53
54
            throw new \Exception('Directory "' . $this->_dir . '" is not writeable');
55
        }
56
    }
57
58
59
    /**
60
     * Stores the log content for later usage
61
     *
62
     * @param string $component Name of the core component
63
     * @param string $content   Content to log in this case
64
     *
65
     * @return boolean
66
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
67
68
    protected function store($component, $content)
69
    {
70
        $filename = $this->_dir . $component . '/' . date('Y-m-d') . '.log';
71
72
        $store = "--------\n" . date('H:i:s') . "\n" . $content . "\n";
73
74
        $save_log = fopen($filename, 'a');
75
76
        // Set stream encoding if possible to avoid converting issues
77
        if (function_exists('stream_encoding')) {
78
79
            stream_encoding($save_log, 'UTF-8');
80
        }
81
82
        fwrite($save_log, $store);
83
        fclose($save_log);
84
85
        // Check if the process owner is the fileowner, otherwise
86
        // chmod won't work proper and give a warning
87
        if (get_current_user()==fileowner($filename)) {
88
            chmod($filename, 0755);
89
        }
90
91
        return true;
92
    }
93
}
94