Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
40.00% |
6 / 15 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FileLogger | |
42.86% |
6 / 14 |
|
0.00% |
0 / 3 |
16.14 | |
0.00% |
0 / 1 |
| __construct | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| log | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| check_and_clear_file | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * FileLogger file. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | namespace Imports\Utils\Logger; |
| 9 | |
| 10 | require_once __DIR__ . '/class-logger-interface.php'; |
| 11 | |
| 12 | use Imports\Utils\LoggerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Class FileLogger |
| 16 | * |
| 17 | * The FileLogger class provides a mechanism for logging messages to a file. |
| 18 | * It implements the LoggerInterface. |
| 19 | */ |
| 20 | class FileLogger implements LoggerInterface { |
| 21 | /** |
| 22 | * The path to the log file. |
| 23 | * |
| 24 | * @var ?string |
| 25 | */ |
| 26 | private $log_file = '/tmp/restore_log/restoration_log.txt'; |
| 27 | |
| 28 | /** |
| 29 | * FileLogger constructor. |
| 30 | * |
| 31 | * Initializes a new instance of the FileLogger class with the specified |
| 32 | * log file. If the log file exists, it is cleared; if it does not exist, |
| 33 | * it is created. |
| 34 | * |
| 35 | * @param string $log_file The path to the log file. |
| 36 | */ |
| 37 | public function __construct( $log_file = '' ) { |
| 38 | if ( $log_file ) { |
| 39 | $this->log_file = $log_file; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Logs a message to the log file. |
| 45 | * |
| 46 | * @param string $message The message to log. |
| 47 | */ |
| 48 | public function log( $message ) { |
| 49 | /** |
| 50 | * We can't use WP_Filesystem::put_contents because it uses |
| 51 | * write mode instead of append, so all the content gets overriden. |
| 52 | */ |
| 53 | if ( ! $this->log_file ) { |
| 54 | return; |
| 55 | } |
| 56 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen |
| 57 | $log_file = fopen( $this->log_file, 'a' ); |
| 58 | |
| 59 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite |
| 60 | fwrite( $log_file, gmdate( 'c' ) . ' ' . $message . "\n" ); |
| 61 | |
| 62 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 63 | fclose( $log_file ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Checks and clears a file. |
| 68 | * |
| 69 | * @return bool True if the directory exists or was successfully created and the file was created or truncated, false otherwise. |
| 70 | */ |
| 71 | public function check_and_clear_file() { |
| 72 | $file_path = $this->log_file; |
| 73 | $directory = pathinfo( $file_path, PATHINFO_DIRNAME ); |
| 74 | if ( ! is_dir( $directory ) && ! wp_mkdir_p( $directory ) ) { |
| 75 | $this->log_file = null; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Create or truncate the file |
| 80 | file_put_contents( $file_path, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 81 | return true; |
| 82 | } |
| 83 | } |