Completed
Push — develop ( f6ac94...a82602 )
by Paul
01:59
created

Image   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 19 2
A normalizeSrc() 0 11 3
1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
use GeminiLabs\Castor\Helpers\Utility;
6
7
class Image
8
{
9
	public $utility;
10
11
	public function __construct( Utility $utility )
12
	{
13
		$this->utility = $utility;
14
	}
15
16
	/**
17
	 * @param int $attachmentId
18
	 *
19
	 * @return null|object
20
	 */
21
	public function get( $attachmentId )
22
	{
23
		$thumbnail = wp_get_attachment_image_src( $attachmentId, 'thumbnail' );
24
25
		if( !$thumbnail )return;
26
27
		$medium = $this->normalizeSrc( wp_get_attachment_image_src( $attachmentId, 'medium' ), $thumbnail );
28
		$large = $this->normalizeSrc( wp_get_attachment_image_src( $attachmentId, 'large' ), $medium );
29
30
		return (object) [
31
			'alt'       => $this->utility->cleanString( get_post_meta( $attachmentId, '_wp_attachment_image_alt', true )),
32
			'caption'   => wp_get_attachment_caption( $attachmentId ),
33
			'copyright' => $this->utility->cleanString( get_post_meta( $attachmentId, '_copyright', true )),
34
			'large'     => $large,
35
			'medium'    => $medium,
36
			'permalink' => get_attachment_link( $attachmentId ),
37
			'thumbnail' => $this->normalizeSrc( $thumbnail ),
38
		];
39
	}
40
41
	/**
42
	 * @param mixed $fallback
43
	 *
44
	 * @return array
45
	 */
46
	protected function normalizeSrc( array $image, $fallback = false )
47
	{
48
		if( is_array( $fallback ) && count( array_diff( $image, $fallback )) < 2 ) {
49
			$image = $fallback;
50
		}
51
		return [
52
			'url'    => $image[0],
53
			'width'  => $image[1],
54
			'height' => $image[2],
55
		];
56
	}
57
}
58