Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Image   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 57
rs 10
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A get_default_image() 0 6 1
A src() 0 14 2
1
<?php
2
3
namespace Classy\Models;
4
5
use Classy\Basis;
6
7
class Image extends Basis {
8
9
	/**
10
	 * Current image id
11
	 * @var int
12
	 */
13
	public $ID;
14
15
	/**
16
	 * Main constructor function. Requires image id
17
	 * @param int $pid
18
	 */
19
	public function __construct( $pid = null ) {
20
21
		// Checks if image with this id exists
22
		if ( null !== $pid && wp_get_attachment_image_src( $pid ) ) {
23
24
			$this->ID = $pid;
25
26
		} else {
27
28
			$this->ID = 0;
29
30
		}
31
	}
32
33
	/**
34
	 * Returns default image url
35
	 * @return string
36
	 */
37
	public static function get_default_image() {
38
39
		// You can put here any url
40
		return CLASSY_THEME_DIR . '/assets/noimage.png';
41
42
	}
43
44
	/**
45
	 * Returns image url. In case image ID is 0 or not set returns default image
46
	 * @param  string $size
47
	 * @return string
48
	 */
49
	public function src( $size = 'medium' ) {
50
51
		if ( $this->ID ) {
52
53
			$thumb = wp_get_attachment_image_src( $this->ID, $size );
54
55
			return $thumb[0];
56
57
		} else {
58
59
			return self::get_default_image();
60
61
		}
62
	}
63
}
64