Completed
Push — master ( d0d590...4bea8e )
by Daniel
08:46
created

base   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 151
ccs 33
cts 44
cp 0.75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 2
A __construct() 0 18 2
A get_message() 0 12 2
A add_lang() 0 16 2
B translate_portions() 0 39 5
1
<?php
2
/**
3
*
4
* Pages extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace blitze\sitemaker\exception;
12
13
/**
14
* Base exception
15
*/
16
class base extends \Exception
17
{
18
	/**
19
	* Null if the message is a string, array if the message was submitted as an array
20
	* @var string|array
21
	*/
22
	protected $message_full;
23
24
	protected $previous;
25
26
	/**
27
	* Constructor
28
	*
29
	* Different from normal exceptions in that we do not enforce $message to be a string.
30
	*
31
	* @param string|array $message
32
	* @param int $code
33
	* @param \Exception $previous
34
	* @access public
35
	*/
36 32
	public function __construct($message = null, $code = 0, \Exception $previous = null)
37
	{
38
		// We're slightly changing the way exceptions work
39
		// Tools, such as xdebug, expect the message to be a string, so to prevent errors
40
		// with those tools, we store our full message in message_full and only a string in message
41 32
		if (is_array($message))
42 32
		{
43 20
			$this->message = (string) $message[0];
44 20
		}
45
		else
46
		{
47 12
			$this->message = $message;
48
		}
49 32
		$this->message_full = $message;
50
51 32
		$this->code = $code;
52 32
		$this->previous = $previous;
53 32
	}
54
55
	/**
56
	* Basic message translation for our exceptions
57
	*
58
	* @param \phpbb\user $user
59
	* @return string|array
60
	* @access public
61
	*/
62
	public function get_message(\phpbb\user $user)
63
	{
64
		// Make sure our language file has been loaded
65
		$this->add_lang($user);
66
67
		if (is_array($this->message_full))
68
		{
69
			return call_user_func_array(array($user, 'lang'), $this->message_full);
70
		}
71
72
		return $user->lang($this->message_full);
73
	}
74
75
	/**
76
	* Translate all portions of the message sent to the exception
77
	*
78
	* Goes through each element of the array and tries to translate them
79
	*
80
	* @param \phpbb\user $user
81
	* @param string|array $message_portions The message portions to translate
82
	* @param string|null $parent_message Send a string to translate all of the
83
	*     portions with the parent message (typically used to format a string
84
	*     with the given message portions). Null to ignore. Default: Null
85
	* @return array|string Array if $parent_message === null else a string
86
	* @access protected
87
	*/
88 32
	protected function translate_portions(\phpbb\user $user, $message_portions, $parent_message = null)
89
	{
90
		// Make sure our language file has been loaded
91 32
		$this->add_lang($user);
92
93
		// Ensure we have an array
94 32
		if (!is_array($message_portions))
95 32
		{
96 12
			$message_portions = array($message_portions);
97 12
		}
98
99
		// Translate each message portion
100 32
		foreach ($message_portions as &$message)
101
		{
102
			// Attempt to translate each portion
103 32
			$translated_message = $user->lang('EXCEPTION_' . $message);
104
105
			// Check if translating did anything
106 32
			if ($translated_message !== 'EXCEPTION_' . $message)
107 32
			{
108
				// It did, so replace message with the translated version
109
				$message = $translated_message;
110
			}
111 32
		}
112
		// Always unset a variable passed by reference in a foreach loop
113 32
		unset($message);
114
115 32
		if ($parent_message !== null)
116 32
		{
117
			// Prepend the parent message to the message portions
118 32
			array_unshift($message_portions, (string) $parent_message);
119
120
			// We return a string
121 32
			return call_user_func_array(array($user, 'lang'), $message_portions);
122
		}
123
124
		// We return an array
125
		return $message_portions;
126
	}
127
128
	/**
129
	* Add our language file
130
	*
131
	* @param \phpbb\user $user
132
	* @return null
133
	* @access public
134
	*/
135 32
	public function add_lang(\phpbb\user $user)
136
	{
137 32
		static $is_loaded = false;
138
139
		// We only need to load the language file once
140
		if ($is_loaded)
141 32
		{
142 29
			return;
143
		}
144
145
		// Add our language file
146 3
		$user->add_lang_ext('blitze/sitemaker', 'exceptions');
147
148
		// So the language file is only loaded once
149 3
		$is_loaded = true;
150 3
	}
151
152
	/**
153
	* Output a string of this error message
154
	*
155
	* This will hopefully be never called, always catch the expected exceptions
156
	* and call get_message to translate them into an error that a user can
157
	* understand
158
	*
159
	* @return string
160
	* @access public
161
	*/
162
	public function __toString()
163
	{
164
		return (is_array($this->message_full)) ? var_export($this->message_full, true) : (string) $this->message_full;
165
	}
166
}
167