Completed
Push — master ( 44198b...431447 )
by Derek
03:47 queued 01:34
created

File::allowOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 extends AbstractHandler
14
{
15
    private $file_name;
16
    private $file_path;
17
18
    /**
19
     * @var \SplFileObject
20
     */
21
    private $file_pointer;
22
23
    /**
24
     * Sets up the file name and path if optional parameters provided.
25
     *
26
     * @param string|null   $file_name [optional] The name of the file used for logging; file_path is not required if
27
     *                      file_name is fully-qualified.
28
     * @param string|null   $file_path [optional] The path of the file used for logging without the trailing directory
29
     *                          separator
30
     */
31 16
    public function __construct($file_name = null, $file_path = null)
32
    {
33 16
        $this->file_name = $file_name;
34
35
        //Trim any trailing directory separators from the path.
36 16
        if (isset($file_path)) {
37 1
            $this->setFilePath($file_path);
38 1
        }
39 16
    }
40
41
    /**
42
     * Clean-up
43
     */
44 16
    public function __destruct()
45
    {
46 16
        $this->close();
47 16
    }
48
49
    /**
50
     * @return null|string  The file_name set for the File instance
51
     */
52 1
    public function getFileName()
53
    {
54 1
        return $this->file_name;
55
    }
56
57
    /**
58
     * @return string|null  The file_path set for the File instance
59
     */
60 2
    public function getFilePath()
61
    {
62 2
        return $this->file_path;
63
    }
64
65
    /**
66
     * Sets the name of the file to be used for logging; setting file_path is not required if the specified file name
67
     * is fully-qualified.
68
     *
69
     * @param string $file_name The name of the file to be used for logging
70
     *
71
     * @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...
72
     */
73 2
    public function setFileName($file_name)
74
    {
75 2
        $this->file_name = $file_name;
76 2
    }
77
78
    /**
79
     * Sets the path of the file to be used for logging; file path should not be set if the File's file name is
80
     * fully-qualified.
81
     *
82
     * @see File::setFileName()
83
     *
84
     * @param string $file_path The path of the file to be used for logging
85
     *
86
     * @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...
87
     */
88 3
    public function setFilePath($file_path)
89
    {
90
        //Trip any trailing directory separators from the path.
91 3
        $this->file_path = rtrim($file_path, '\/');
92 3
    }
93
94
    /**
95
     * Truncates the File to zero bytes, effectively clearing the file's contents. Does not delete the file.
96
     *
97
     * @return bool Returns true if truncate succeeded; returns false on failure.
98
     */
99 24
    public function clearFile()
100
    {
101 24
        $cleared = false;
102
103 24
        if (isset($this->file_pointer)) {
104 23
            $cleared = $this->file_pointer->ftruncate(0);
105 23
        }
106
107 24
        return $cleared;
108
    }
109
110
    /**
111
     * Sets the File's file_pointer to null to clean up resource.
112
     */
113 16
    public function close()
114
    {
115 16
        $this->file_pointer = null;
116 16
    }
117
118
    /**
119
     * Initializes the File for writing via append. Initialization attempts to create an SplFileObject pointer using
120
     * the File's name and path properties.
121
     *
122
     * @see \SplFileObject
123
     *
124
     * @return bool Returns true on successful file pointer creation; returns false on failure.
125
     *
126
     * @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...
127
     */
128 11
    public function initialize()
129
    {
130 11
        $full_file = "";
131
132 11
        if (isset($this->file_path)) {
133 1
            $full_file = $this->file_path . DIRECTORY_SEPARATOR;
134 1
        }
135
136 11
        $full_file .= $this->file_name;
137
138
        try {
139 11
            $this->file_pointer = new \SplFileObject($full_file, "a");
140
141 5
            return true;
142 6
        } catch (\Exception $e) {
143 6
            return false;
144
        }
145
    }
146
147
    /**
148
     * Writes the specified message to the File pointer. Requires initialization via File::initialize().
149
     *
150
     * @see File::initialize()
151
     *
152
     * @param string $message   Message to be written to the file.
153
     *
154
     * @return bool             Returns true on successful write; returns false on failure.
155
     */
156 24
    public function write($message)
157
    {
158 24
        $file_writable   = false;
159 24
        $message_written = false;
160
161 24
        if ($this->output_allowed) {
162 23
            if (isset($this->file_pointer)) {
163 19
                $file_writable = $this->file_pointer->flock(LOCK_EX);
164 19
            }
165
166 23
            if ($file_writable) {
167 19
                $now = date('Y-m-d H:i:s');
168 19
                $this->file_pointer->fwrite("$now : $message" . "\r\n");
169 19
                $this->file_pointer->flock(LOCK_UN);
170
171 19
                $message_written = true;
172 19
            }
173 23
        }
174
175 24
        return $message_written;
176
    }
177
}
178