Passed
Push — master ( 0bccda...b35e9b )
by Paul
05:49 queued 02:55
created

Notice::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use BadMethodCallException;
6
use Exception;
7
use GeminiLabs\Pollux\Application;
8
9
/**
10
 * @property array $all
11
 * @method void addError( mixed $messages, bool $dismissible = true )
12
 * @method void addInfo( mixed $messages, bool $dismissible = true )
13
 * @method void addSuccess( mixed $messages, bool $dismissible = true )
14
 * @method void addWarning( mixed $messages, bool $dismissible = true )
15
 */
16
class Notice
17
{
18
	/**
19
	 * @var Application
20
	 */
21
	protected $app;
22
23
	public function __construct( Application $app )
24
	{
25
		$this->app = $app;
26
	}
27
28
	public function __call( $name, $args )
29
	{
30
		$method = strtolower( $name );
31
		$status = substr( $method, 3 );
32
		if( 'add' == substr( $method, 0, 3 ) && in_array( $status, ['error', 'info', 'success', 'warning'] )) {
33
			return call_user_func_array( [$this, 'addNotice'], array_merge( [$status], $args ));
34
		}
35
		throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
36
	}
37
38
	public function __get( $property )
39
	{
40
		if( $property == 'all' ) {
41
			return $this->app->notices;
42
		}
43
		throw new Exception( sprintf( 'Not a valid property: %s', $property ));
44
	}
45
46
	/**
47
	 * @return string
48
	 */
49 View Code Duplication
	public function activateButton( array $plugin )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
	{
51
		$actionUrl = self_admin_url( sprintf( 'options-general.php?page=%s&action=activate&plugin=%s', $this->app->id, $plugin['plugin'] ));
52
		return $this->button( sprintf( '%s %s', __( 'Activate', 'pollux' ), $plugin['name'] ), [
53
			'data-name' => $plugin['name'],
54
			'data-plugin' => $plugin['plugin'],
55
			'data-slug' => $plugin['slug'],
56
			'href' => wp_nonce_url( $actionUrl, sprintf( 'activate-plugin_%s', $plugin['plugin'] )),
57
		]);
58
	}
59
60
	/**
61
	 * @param string $title
62
	 * @return string
63
	 */
64
	public function button( $title, array $atts = [] )
65
	{
66
		$atts = wp_parse_args( $atts, [
67
			'class' => '',
68
			'href' => '',
69
		]);
70
		$atts['class'] = trim( $atts['class'] . ' button button-small' );
71
		$attributes = array_reduce( array_keys( $atts ), function( $carry, $key ) use( $atts ) {
72
			return $carry . sprintf( ' %s="%s"', $key, $atts[$key] );
73
		});
74
		return sprintf( '<a%s>%s</a>', $attributes, $title );
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	public function generate( array $notice, $unset = true )
81
	{
82
		if( $unset ) {
83
			$index = array_search( $notice, $this->app->notices );
84
			if( $index !== false ) {
85
				unset( $this->app->notices[$index] );
86
			}
87
		}
88
		return $this->buildNotice( $notice );
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94 View Code Duplication
	public function installButton( array $plugin )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
	{
96
		$actionUrl = self_admin_url( sprintf( 'update.php?action=install-plugin&plugin=%s', $plugin['slug'] ));
97
		return $this->button( sprintf( '%s %s', __( 'Install', 'pollux' ), $plugin['name'] ), [
98
			'data-name' => $plugin['name'],
99
			'data-plugin' => $plugin['plugin'],
100
			'data-slug' => $plugin['slug'],
101
			'href' => wp_nonce_url( $actionUrl, sprintf( 'install-plugin_%s', $plugin['slug'] )),
102
		]);
103
	}
104
105
	/**
106
	 * @return string
107
	 */
108 View Code Duplication
	public function updateButton( array $plugin )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
	{
110
		$actionUrl = self_admin_url( sprintf( 'update.php?action=upgrade-plugin&plugin=%s', $plugin['plugin'] ));
111
		return $this->button( sprintf( '%s %s', __( 'Update', 'pollux' ), $plugin['name'] ), [
112
			'data-name' => $plugin['name'],
113
			'data-plugin' => $plugin['plugin'],
114
			'data-slug' => $plugin['slug'],
115
			'href' => wp_nonce_url( $actionUrl, sprintf( 'upgrade-plugin_%s', $plugin['plugin'] )),
116
		]);
117
	}
118
119
	/**
120
	 * @param string $string
121
	 * @return string
122
	 */
123
	public function title( $string )
124
	{
125
		return sprintf( '<strong>%s</strong>', $string );
126
	}
127
128
	/**
129
	 * @param string $type
130
	 * @param string|array $messages
131
	 * @param bool $dismissible
132
	 * @return void
133
	 */
134
	protected function addNotice( $type, $messages, $dismissible = true )
135
	{
136
		$this->app->notices[] = [
137
			'dismissible' => $dismissible,
138
			'message' => $this->buildMessage( array_filter( (array) $messages )),
139
			'type' => $type,
140
		];
141
		$this->app->notices = array_unique( $this->app->notices, SORT_REGULAR );
142
	}
143
144
	/**
145
	 * @return string
146
	 */
147
	protected function buildMessage( array $messages )
148
	{
149
		foreach( $messages as $key => &$message ) {
150
			if( !is_wp_error( $message ))continue;
151
			$message = $message->get_error_message();
152
		}
153
		return wpautop( implode( PHP_EOL . PHP_EOL, $messages ));
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	protected function buildNotice( array $notice )
160
	{
161
		$class = sprintf( 'notice notice-%s', $notice['type'] );
162
		if( $notice['dismissible'] ) {
163
			$class .= ' is-dismissible';
164
		}
165
		return sprintf( '<div class="pollux-notice %s">%s</div>', $class, $notice['message'] );
166
	}
167
}
168