Completed
Push — develop ( a82602...1bde17 )
by Paul
02:00
created

Gallery   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 271
Duplicated Lines 2.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 4
dl 7
loc 271
rs 8.6
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 14 1
A render() 0 7 1
A renderImage() 0 18 2
A renderPagination() 0 12 2
A getBoolValue() 0 11 4
A getGalleryArg() 0 11 4
A getLazyloadArg() 0 4 1
A getMediaArg() 0 14 3
A getPaged() 0 4 3
A getPaginationArg() 0 4 1
A getPermalinksArg() 0 4 1
A getPhotoswipeData() 0 11 1
A getValue() 0 7 3
A normalizeArgs() 0 22 2
A renderImageTag() 0 16 3
A __construct() 7 7 1
A getImagesPerPageArg() 0 11 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
use GeminiLabs\Castor\Helpers\PostMeta;
6
use GeminiLabs\Castor\Helpers\Theme;
7
use GeminiLabs\Castor\Helpers\Utility;
8
use GeminiLabs\Castor\Image;
9
use WP_Post;
10
use WP_Query;
11
12
class Gallery
13
{
14
	public $image;
15
	public $postmeta;
16
	public $theme;
17
	public $utility;
18
19
	protected $args;
20
21 View Code Duplication
	public function __construct( Image $image, PostMeta $postmeta, Theme $theme, Utility $utility )
1 ignored issue
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...
22
	{
23
		$this->image    = $image;
24
		$this->postmeta = $postmeta;
25
		$this->theme    = $theme;
26
		$this->utility  = $utility;
27
	}
28
29
	/**
30
	 * @return WP_Query
31
	 */
32
	public function query( array $args = [] )
33
	{
34
		$this->normalizeArgs( $args );
35
36
		return new WP_Query([
37
			'orderby'        => 'post__in',
38
			'paged'          => $this->getPaged(),
39
			'post__in'       => $this->args['media'],
40
			'post_mime_type' => 'image',
41
			'post_type'      => 'attachment',
42
			'post_status'    => 'inherit',
43
			'posts_per_page' => $this->args['images_per_page'],
44
		]);
45
	}
46
47
	/**
48
	 * @return string
49
	 */
50
	public function render( WP_Query $gallery )
51
	{
52
		$images = array_reduce( $gallery->posts, function( $images, $attachment ) {
53
			return $images . $this->renderImage( $attachment );
54
		});
55
		return sprintf( '<div class="gallery-images" itemscope itemtype="http://schema.org/ImageGallery">%s</div>', $images );
56
	}
57
58
	/**
59
	 * @return null|string
60
	 */
61
	public function renderImage( WP_Post $attachment )
62
	{
63
		$image = $this->image->get( $attachment->ID );
64
		if( !$image )return;
65
		return sprintf(
66
			'<figure class="gallery-image" data-w="%s" data-h="%s" data-ps=\'%s\' itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">' .
67
				'%s<figcaption itemprop="caption description">' .
68
					'%s <span itemprop="copyrightHolder">%s</span>' .
69
				'</figcaption>' .
70
			'</figure>',
71
			$image->thumbnail['width'],
72
			$image->thumbnail['height'],
73
			$this->getPhotoswipeData( $image ),
74
			$this->renderImageTag( $image ),
75
			$image->caption,
76
			$image->copyright
77
		);
78
	}
79
80
	/**
81
	 * @return null|string
82
	 */
83
	public function renderPagination( WP_Query $query )
84
	{
85
		if( !$this->args['pagination'] )return;
86
		return paginate_links([
87
			'before_page_number' => '<span class="screen-reader-text">' . __( 'Page', 'castor' ) . ' </span>',
88
			'current'            => $query->query['paged'],
89
			'mid_size'           => 1,
90
			'next_text'          => __( 'Next', 'castor' ),
91
			'prev_text'          => __( 'Previous', 'castor' ),
92
			'total'              => $query->max_num_pages,
93
		]);
94
	}
95
96
	/**
97
	 * @param string $key
98
	 * @param mixed  $value
99
	 *
100
	 * @return bool
101
	 */
102
	protected function getBoolValue( $key, $value = null )
103
	{
104
		$bool = $this->getValue( $key, $value );
105
106
		if( is_string( $bool ) && !in_array( $bool, ['true', 'false'] )) {
107
			$bool = wp_validate_boolean( $this->postmeta->get( $bool ));
108
		}
109
		return is_bool( $bool )
110
			? $bool
111
			: false;
112
	}
113
114
	/**
115
	 * @param mixed $value
116
	 *
117
	 * @return int
118
	 */
119
	protected function getGalleryArg( $value = null )
120
	{
121
		$gallery = $this->getValue( 'gallery', $value );
122
123
		if( !is_numeric( $gallery ) && is_string( $gallery )) {
124
			$gallery = intval( $this->postmeta->get( $gallery ));
125
		}
126
		return !is_null( get_post( $gallery ))
127
			? intval( $gallery )
128
			: 0;
129
	}
130
131
	/**
132
	 * @param mixed $value
133
	 *
134
	 * @return int
135
	 */
136
	protected function getImagesPerPageArg( $value = null )
137
	{
138
		$perPage = $this->getValue( 'images_per_page', $value );
139
140
		if( !is_numeric( $perPage ) && is_string( $perPage )) {
141
			$perPage = $this->postmeta->get( $perPage );
142
		}
143
		return !!intval( $perPage )
144
			? $perPage
145
			: -1;
146
	}
147
148
	/**
149
	 * @param mixed $value
150
	 *
151
	 * @return bool
152
	 */
153
	protected function getLazyloadArg( $value = null )
154
	{
155
		return $this->getBoolValue( 'lazyload', $value );
156
	}
157
158
	/**
159
	 * @param mixed $value
160
	 *
161
	 * @return array
162
	 */
163
	protected function getMediaArg( $value = null )
164
	{
165
		$media = $this->getValue( 'media', $value );
166
167
		if( is_string( $media )) {
168
			$media = $this->postmeta->get( $media, [
169
				'ID'     => $this->getGalleryArg(),
170
				'single' => false,
171
			]);
172
		}
173
		return is_array( $media )
174
			? wp_parse_id_list( $media )
175
			: [];
176
	}
177
178
	/**
179
	 * @return int
180
	 */
181
	protected function getPaged()
182
	{
183
		return intval( get_query_var(( is_front_page() ? 'page' : 'paged' ))) ?: 1;
184
	}
185
186
	/**
187
	 * @param mixed $value
188
	 *
189
	 * @return bool
190
	 */
191
	protected function getPaginationArg( $value = null )
192
	{
193
		return $this->getBoolValue( 'pagination', $value );
194
	}
195
196
	/**
197
	 * @param mixed $value
198
	 *
199
	 * @return bool
200
	 */
201
	protected function getPermalinksArg( $value = null )
202
	{
203
		return $this->getBoolValue( 'permalinks', $value );
204
	}
205
206
	/**
207
	 * @return string
208
	 */
209
	protected function getPhotoswipeData( $image )
210
	{
211
		return sprintf( '{"l":{"src":"%s","w":%d,"h":%d},"m":{"src":"%s","w":%d,"h":%d}}',
212
			$image->large['url'],
213
			$image->large['width'],
214
			$image->large['height'],
215
			$image->medium['url'],
216
			$image->medium['width'],
217
			$image->medium['height']
218
		);
219
	}
220
221
	/**
222
	 * @param string $key
223
	 * @param mixed  $value
224
	 *
225
	 * @return mixed
226
	 */
227
	protected function getValue( $key, $value = null )
228
	{
229
		if( is_null( $value ) && isset( $this->args[$key] )) {
230
			$value = $this->args[$key];
231
		}
232
		return $value;
233
	}
234
235
	/**
236
	 * @return array
237
	 */
238
	protected function normalizeArgs( array $args = [] )
239
	{
240
		$defaults = [
241
			'gallery',         // (string) meta_key | (int) post_id
242
			'lazyload',        // (string) meta_key | (bool)
243
			'media',           // (string) meta_key | (array) post_ids
244
			'pagination',      // (string) meta_key | (bool)
245
			'images_per_page', // (string) meta_key | (int) number
246
			'permalinks',      // (string) meta_key | (bool)
247
		];
248
249
		$this->args = shortcode_atts( array_combine( $defaults, $defaults ), $args );
250
251
		array_walk( $this->args, function( &$value, $key ) {
252
			$method = $this->utility->buildMethodName( $key . '_arg' );
253
			if( method_exists( $this, $method )) {
254
				$value = call_user_func([ $this, $method ], $value );
255
			}
256
		});
257
258
		return $this->args;
259
	}
260
261
	/**
262
	 * @param object $image
263
	 *
264
	 * @return null|string
265
	 */
266
	protected function renderImageTag( $image )
267
	{
268
		$imgSrc = $this->getLazyloadArg()
269
			? $this->theme->imageUri( 'blank.gif' )
270
			: $image->thumbnail['url'];
271
272
		$imgTag = sprintf( '<img src="%s" data-src="%s" itemprop="thumbnail" alt="%s"/>',
273
			$imgSrc,
274
			$image->thumbnail['url'],
275
			$image->alt
276
		);
277
278
		return $this->getPermalinksArg()
279
			? sprintf( '<a href="%s" itemprop="contentUrl">%s</a>', $image->permalink, $imgTag )
280
			: $imgTag;
281
	}
282
}
283