Alert::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 10
nc 8
nop 2
1
<?php namespace Cornford\Alerter;
2
3
use Cornford\Alerter\Contracts\AlertableInterface;
4
use Cornford\Alerter\Exceptions\AlertContentException;
5
use Cornford\Alerter\Exceptions\AlertTypeException;
6
use Cornford\Alerter\Exceptions\AlertVariableException;
7
use Illuminate\Support\Facades\Config;
8
9
class Alert extends AlertType implements AlertableInterface {
10
11
	/**
12
	 * The content of the alert.
13
	 *
14
	 * @var string
15
	 */
16
	protected $content;
17
18
	/**
19
	 * The type of the alert.
20
	 *
21
	 * @var string
22
	 */
23
	protected $type;
24
25
	/**
26
	 * The view of the alert.
27
	 *
28
	 * @var string
29
	 */
30
	protected $view;
31
32
	/**
33
	 * The path for the alert view.
34
	 *
35
	 * @var string
36
	 */
37
	protected $viewPath;
38
39
	/**
40
	 * Construct a new alerter instance.
41
	 *
42
	 * @param string $type
43
	 * @param string $content
44
	 *
45
	 * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
	 */
47
	private function __construct($type, $content)
48
	{
49
		$this->type = $type;
50
		$this->content = $content;
51
52
		$viewPath = '';
53
		$view = '';
54
55
		if (class_exists('Config')) {
56
			$viewPath = Config::get('alerter::path');
57
			$view = Config::get('alerter::alert');
58
		}
59
60
		$this->viewPath = $viewPath ?: dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Views';
61
		$this->view = $view ?: AlertDisplay::VIEW_SIMPLE;
62
	}
63
64
	/**
65
	 * Create a new alerter instance.
66
	 *
67
	 * @param string $type
68
	 * @param string $content
69
	 *
70
	 * @return self
71
	 *
72
	 * @throws \Cornford\Alerter\Exceptions\AlertContentException
73
	 * @throws \Cornford\Alerter\Exceptions\AlertTypeException
74
	 */
75
	public static function create($type, $content)
76
	{
77
		if (empty($type)) {
78
			throw new AlertTypeException('Unable to create message with invalid type.');
79
		}
80
81
		if (empty($content)) {
82
			throw new AlertContentException('Unable to create message with invalid content.');
83
		}
84
85
		return new self($type, $content);
86
	}
87
88
	/**
89
	 * Return the stored alert type.
90
	 *
91
	 * @return string
92
	 */
93
	public function getType()
94
	{
95
		return $this->type;
96
	}
97
98
	/**
99
	 * Return the stored alert content.
100
	 *
101
	 * @return string
102
	 */
103
	public function getContent()
104
	{
105
		return $this->content;
106
	}
107
108
	/**
109
	 * Return the stored alert variable if it exists.
110
	 *
111
	 * @param string $variable ob
112
	 *
113
	 * @return string
114
	 *
115
	 * @throws \Cornford\Alerter\Exceptions\AlertVariableException
116
	 */
117
	public function __get($variable)
118
	{
119
		if (!property_exists($this, $variable)) {
120
			throw new AlertVariableException('Unable to find variable: ' . $variable . '.');
121
		}
122
123
		return $this->{$variable};
124
	}
125
126
	/**
127
	 * Set the alert view.
128
	 *
129
	 * @param string $view
130
	 *
131
	 * @return self
132
	 */
133
	public function useView($view)
134
	{
135
		$this->view = $view;
136
137
		return $this;
138
	}
139
140
	/**
141
	 * Set the alert view path.
142
	 *
143
	 * @param string $path
144
	 *
145
	 * @return self
146
	 */
147
	public function useViewPath($path)
148
	{
149
		$this->viewPath = $path;
150
151
		return $this;
152
	}
153
154
	/**
155
	 * Return a string of alert display HTML for a set display view.
156
	 *
157
	 * @param string $view
158
	 * @param string $viewPath
159
	 *
160
	 * @return string
161
	 */
162
	public function display($viewPath = null, $view = null)
163
	{
164
		if (!$viewPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $viewPath of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
165
			$viewPath = $this->viewPath;
166
		}
167
168
		if (!$view) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
169
			$view = $this->view;
170
		}
171
172
		return (new AlertDisplay($this, $viewPath, $view))->display();
173
	}
174
175
}
176