base   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 124
ccs 32
cts 35
cp 0.9143
rs 10
wmc 9

4 Methods

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