Passed
Push — master ( 27868f...f8a6d5 )
by Warwick
02:59
created

LSX_Logger   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A init() 0 8 2
A log() 0 3 1
B output_log() 0 19 6
A loop_through_logs() 0 7 2
A wp_admin_bar_menu() 0 19 1
1
<?php
2
3
namespace lsx;
4
5
if ( ! class_exists( '\lsx\LSX_Logger' ) ) {
6
7
	/**
8
	 * LSX_Logger Main Class
9
	 *
10
	 * @package   LSX_Logger
11
	 * @author    LightSpeed
12
	 * @license   GPL-3.0+
13
	 * @link
14
	 * @copyright 2017 LightSpeedDevelopment
15
	 */
16
	class LSX_Logger {
17
18
		/**
19
		 * Holds instance of the class
20
		 *
21
		 * @since   1.1.0
22
		 * @var     \lsx\Geo_Content
23
		 */
24
		private static $instance;
25
26
		/**
27
		 * Holds the Logs of what happened
28
		 */
29
		private $logs = array();
30
31
		/**
32
		 * Constructor.
33
		 */
34
		public function __construct() {
35
			add_action( 'wp_before_admin_bar_render', array( $this, 'wp_admin_bar_menu' ) );
36
		}
37
38
		/**
39
		 * Return an instance of this class.
40
		 *
41
		 * @return  LSX_Logger  A single instance
42
		 */
43
		public static function init() {
44
45
			// If the single instance hasn't been set, set it now.
46
			if ( ! isset( self::$instance ) ) {
47
				self::$instance = new self();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type object<lsx\LSX_Logger> is incompatible with the declared type object<lsx\Geo_Content> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
			}
49
			return self::$instance;
0 ignored issues
show
Bug Compatibility introduced by
The expression self::$instance; of type lsx\LSX_Logger|lsx\Geo_Content adds the type lsx\Geo_Content to the return on line 49 which is incompatible with the return type documented by lsx\LSX_Logger::init of type lsx\LSX_Logger.
Loading history...
50
		}
51
52
		/**
53
		 * Adds a field to the log
54
		 *
55
		 * @param $plugin string
56
		 * @param $key string
57
		 * @param $message string
58
		 * @return  void
59
		 */
60
		public function log( $plugin = '', $key = '', $message = '' ) {
61
			$this->logs[ $plugin ][ $key ] = $message;
62
		}
63
64
		/**
65
		 * Gets a field from the API lookup object
66
		 *
67
		 * @param $plugin string | boolean
68
		 * @return  string
69
		 */
70
		public function output_log( $plugin = false ) {
71
			$return = '';
72
			if ( ! empty( $this->logs ) ) {
73
				foreach ( $this->logs as $plugin_key => $log ) {
74
75
					$return = '<div style="padding: 15px;">';
76
					if ( false !== $plugin && $plugin_key !== $plugin ) {
77
						continue;
78
					} else if ( false === $plugin ) {
79
						$return .= '<h4>' . $plugin_key . '</h4>';
80
					}
81
					$return .= '<ul>';
82
					$return .= $this->loop_through_logs( $log );
83
					$return .= '</ul>';
84
					$return .= '</div>';
85
				}
86
			}
87
			return $return;
88
		}
89
90
		/**
91
		 * Gets a field from the API lookup object
92
		 *
93
		 * @param $log_array array
94
		 * @return  string
95
		 */
96
		public function loop_through_logs( $log_array = array() ) {
97
			$return = '';
98
			foreach ( $log_array as $key => $message ) {
99
				$return .= '<li>' . $message . ' <small>(' . $key . ')</small></li>';
100
			}
101
			return $return;
102
		}
103
104
		/**
105
		 * Registers an admin bar menu so you can see the output of the log messages
106
		 *
107
		 * @return  void
108
		 */
109
		function wp_admin_bar_menu() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
			global $wp_admin_bar;
111
			$wp_admin_bar->add_menu( array(
112
				'parent' => false,
113
				'id' => 'lsx_logger',
114
				'title' => __( 'LSX Log' ), // link title
115
				'href' => '',
116
			));
117
			$wp_admin_bar->add_menu( array(
118
				'parent' => 'lsx_logger',
119
				'id' => 'lsx_logger_output',
120
				'title' => '', // link title
121
				'href' => '',
122
				'meta' => array(
123
					'html' => $this->output_log(),
124
					'class' => '',
125
				),
126
			));
127
		}
128
129
	}
130
}
131