DeploynautLogFile::setBasePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Simple support class for reading and writing deploynaut job logs
5
 */
6
class DeploynautLogFile {
7
8
	protected $logFile;
9
10
	protected $basePath;
11
12
	/**
13
	 * @param string $logFile The log filename
14
	 * @param string|null $basePath Base path of where logs reside. Defaults to DEPLOYNAUT_LOG_PATH
15
	 */
16
	public function __construct($logFile, $basePath = null) {
17
		$this->logFile = $logFile;
18
		if ($basePath !== null) {
19
			$this->basePath = $basePath;
20
		} else if (defined('DEPLOYNAUT_LOG_PATH')) {
21
			$this->basePath = DEPLOYNAUT_LOG_PATH;
22
		} else {
23
			$this->basePath = sys_get_temp_dir();
24
		}
25
	}
26
27
	/**
28
	 * Set the log filename
29
	 * @param string $filename
30
	 */
31
	public function setLogFile($filename) {
32
		$this->logFile = $filename;
33
	}
34
35
	/**
36
	 * Set the base path of where logs reside
37
	 * @param string $path
38
	 */
39
	public function setBasePath($path) {
40
		$this->basePath = $path;
41
	}
42
43
	/**
44
	 * Return the un-sanitised log path.
45
	 * @return string
46
	 */
47
	public function getRawFilePath() {
48
		return $this->basePath . '/' . $this->logFile;
49
	}
50
51
	/**
52
	 * Get the sanitised log path.
53
	 * @return string
54
	 */
55
	public function getSanitisedLogFilePath() {
56
		return $this->basePath . '/' . strtolower(FileNameFilter::create()->filter($this->logFile));
57
	}
58
59
	/**
60
	 * Return log file path, assuming it exists. Returns NULL if nothing found.
61
	 * @return string|null
62
	 */
63
	public function getLogFilePath() {
64
		$path = $this->getSanitisedLogFilePath();
65
66
		// for backwards compatibility on old logs
67
		if (!file_exists($path)) {
68
			$path = $this->getRawFilePath();
69
70
			if (!file_exists($path)) {
71
				return null;
72
			}
73
		}
74
75
		return $path;
76
	}
77
78
	/**
79
	 * Write a message line into the log file.
80
	 * @param string $message
81
	 */
82
	public function write($message) {
83
		// Make sure we write into the old path for existing logs. New logs use the sanitised file path instead.
84
		$path = file_exists($this->getRawFilePath()) ? $this->getRawFilePath() : $this->getSanitisedLogFilePath();
85
86
		error_log('[' . date('Y-m-d H:i:s') . '] ' . $message . PHP_EOL, 3, $path);
87
		@chmod($path, 0666);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
88
	}
89
90
	/**
91
	 * Does the log file exist?
92
	 * @return bool
93
	 */
94
	public function exists() {
95
		return (bool) $this->getLogFilePath();
96
	}
97
98
	/**
99
	 * Return the content of the log file.
100
	 * @return string
101
	 */
102
	public function content() {
103
		return $this->exists() ? file_get_contents($this->getLogFilePath()) : 'Log has not been created yet.';
104
	}
105
106
}
107