Completed
Push — master ( 11db71...c21d9a )
by Derek
02:28
created

File::write()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
cc 4
eloc 12
nc 5
nop 1
crap 4
1
<?php
2
3
namespace Subreality\Dilmun\Nabu\LoggerHandler;
4
5
/**
6
 * Log file object implementing a standard HandlerInterface for log entry handlers.
7
 *
8
 * @author derek
9
 *
10
 * @todo Create methods that set Class properties from actual file properties.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
11
 * @todo Implement log rotation mechanism based on the size of an existing log file specified for writing.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
12
 */
13
class File implements HandlerInterface
14
{
15
    private $file_name;
16
    private $file_path;
17
18
    private $output_enabled = true;
19
20
    /**
21
     * @var \SplFileObject
22
     */
23
    private $file_pointer;
24
25
    /**
26
     * Sets up the file name and path if optional parameters provided.
27
     *
28
     * @param string|null   $file_name [optional] The name of the file used for logging; file_path is not required if
29
     *                      file_name is fully-qualified.
30
     * @param string|null   $file_path [optional] The path of the file used for logging without the trailing directory
31
     *                          separator
32
     */
33 16
    public function __construct($file_name = null, $file_path = null)
34
    {
35 16
        $this->file_name = $file_name;
36
37
        //Trim any trailing directory separators from the path.
38 16
        if (isset($file_path)) {
39 1
            $this->setFilePath($file_path);
40 1
        }
41 16
    }
42
43
    /**
44
     * Clean-up
45
     */
46 16
    public function __destruct()
47
    {
48 16
        $this->close();
49 16
    }
50
51
    /**
52
     * @return null|string  The file_name set for the File instance
53
     */
54 1
    public function getFileName()
55
    {
56 1
        return $this->file_name;
57
    }
58
59
    /**
60
     * @return string|null  The file_path set for the File instance
61
     */
62 2
    public function getFilePath()
63
    {
64 2
        return $this->file_path;
65
    }
66
67
    /**
68
     * Sets the name of the file to be used for logging; setting file_path is not required if the specified file name
69
     * is fully-qualified.
70
     *
71
     * @param string $file_name The name of the file to be used for logging
72
     *
73
     * @todo Perform some sort of validation against the file name string
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
74
     */
75 2
    public function setFileName($file_name)
76
    {
77 2
        $this->file_name = $file_name;
78 2
    }
79
80
    /**
81
     * Sets the path of the file to be used for logging; file path should not be set if the File's file name is
82
     * fully-qualified.
83
     *
84
     * @see File::setFileName()
85
     *
86
     * @param string $file_path The path of the file to be used for logging
87
     *
88
     * @todo Perform some sort of validation against the file path string, maybe considering file_name contents
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
89
     */
90 3
    public function setFilePath($file_path)
91
    {
92
        //Trip any trailing directory separators from the path.
93 3
        $this->file_path = rtrim($file_path, '\/');
94 3
    }
95
96
    /**
97
     * Truncates the File to zero bytes, effectively clearing the file's contents. Does not delete the file.
98
     *
99
     * @return bool Returns true if truncate succeeded; returns false on failure.
100
     */
101 24
    public function clearFile()
102
    {
103 24
        $cleared = false;
104
105 24
        if (isset($this->file_pointer)) {
106 23
            $cleared = $this->file_pointer->ftruncate(0);
107 23
        }
108
109 24
        return $cleared;
110
    }
111
112
    /**
113
     * Sets the File's file_pointer to null to clean up resource.
114
     */
115 16
    public function close()
116
    {
117 16
        $this->file_pointer = null;
118 16
    }
119
120
    /**
121
     * Initializes the File for writing via append. Initialization attempts to create an SplFileObject pointer using
122
     * the File's name and path properties.
123
     *
124
     * @see \SplFileObject
125
     *
126
     * @return bool Returns true on successful file pointer creation; returns false on failure.
127
     *
128
     * @todo Detect whether file_path has trailing directory separator; add both cases to FileTest.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
129
     */
130 11
    public function initialize()
131
    {
132 11
        $full_file = "";
133
134 11
        if (isset($this->file_path)) {
135 1
            $full_file = $this->file_path . DIRECTORY_SEPARATOR;
136 1
        }
137
138 11
        $full_file .= $this->file_name;
139
140
        try {
141 11
            $this->file_pointer = new \SplFileObject($full_file, "a");
142
143 5
            return true;
144 6
        } catch (\Exception $e) {
145 6
            return false;
146
        }
147
    }
148
149
    /**
150
     * Writes the specified message to the File pointer. Requires initialization via File::initialize().
151
     *
152
     * @see File::initialize()
153
     *
154
     * @param string $message   Message to be written to the file.
155
     *
156
     * @return bool             Returns true on successful write; returns false on failure.
157
     */
158 24
    public function write($message)
159
    {
160 24
        $file_writable   = false;
161 24
        $message_written = false;
162
163 24
        if ($this->output_enabled) {
164 23
            if (isset($this->file_pointer)) {
165 19
                $file_writable = $this->file_pointer->flock(LOCK_EX);
166 19
            }
167
168 23
            if ($file_writable) {
169 19
                $now = date('Y-m-d H:i:s');
170 19
                $this->file_pointer->fwrite("$now : $message" . "\r\n");
171 19
                $this->file_pointer->flock(LOCK_UN);
172
173 19
                $message_written = true;
174 19
            }
175 23
        }
176
177 24
        return $message_written;
178
    }
179
180 2
    public function suppressOutput()
181
    {
182 2
        $this->output_enabled = false;
183 2
    }
184
185 1
    public function allowOutput()
186
    {
187 1
        $this->output_enabled = true;
188 1
    }
189
}
190