log_wrapper_output_handler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 20
c 8
b 0
f 0
dl 0
loc 72
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 6 2
A file_open() 0 12 3
A __construct() 0 4 1
A ext_folder_exists() 0 12 3
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\output_handler;
12
13
use phpbb\filesystem\filesystem_interface;
14
15
class log_wrapper_output_handler
16
{
17
	/**
18
	 * @var filesystem_interface
19
	 */
20
	protected $filesystem;
21
	/**
22
	 * Log file handle
23
	 *
24
	 * @var resource
25
	 */
26
	protected $file_handle = false;
27
	/**
28
	 * Status of the log path
29
	 *
30
	 * @var bool
31
	 */
32
	protected $log_path_result = false;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param filesystem_interface $filesystem phpBB's filesystem service
38
	 * @param string               $log_file   File to log to
39
	 */
40
	public function __construct(filesystem_interface $filesystem, $log_file)
41
	{
42
		$this->filesystem = $filesystem;
43
		$this->file_open($log_file);
44
	}
45
46
	/**
47
	 * Open file for logging
48
	 *
49
	 * @param string $file File to open
50
	 */
51
	protected function file_open($file): void
52
	{
53
		//check if the extension directory exists in the store/ folder
54
		$this->ext_folder_exists(dirname($file));
55
56
		if ($this->log_path_result && $this->filesystem->is_writable(dirname($file)))
57
		{
58
			$this->file_handle = fopen($file, 'ab');
59
		}
60
		else
61
		{
62
			throw new \RuntimeException('Unable to write to transaction log file');
63
		}
64
	}
65
66
	protected function ext_folder_exists($dir): void
67
	{
68
		// Try to create the directory if it does not exist
69
		if (!file_exists($dir))
70
		{
71
			$mkdir_result = @mkdir($dir, 0777, true);
72
			$chmod_result = $this->filesystem->phpbb_chmod($dir, CHMOD_READ | CHMOD_WRITE);
73
			$this->log_path_result = $mkdir_result && $chmod_result;
74
		}
75
		else
76
		{
77
			$this->log_path_result = true;
78
		}
79
	}
80
81
	public function write($message)
82
	{
83
		if ($this->file_handle !== false)
0 ignored issues
show
introduced by
The condition $this->file_handle !== false is always true.
Loading history...
84
		{
85
			fwrite($this->file_handle, $message);
86
			fflush($this->file_handle);
87
		}
88
	}
89
}
90