Completed
Pull Request — staging (#840)
by
unknown
16:56
created

MediaAsset::get_image()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * All media assets should be defined as CONSTs in this class prior to usage.
6
 * Sample usage: <img src="<?php echo ( new MediaAsset() )->get_image( MediaAsset::BANNER ); ?>">
7
 *
8
 * @package   YIKES\EasyForms
9
 * @author    Freddie Mixell
10
 * @license   GPL2
11
 */
12
13
namespace YIKES\EasyForms\Assets;
14
15
use YIKES\EasyForms\PluginHelper;
16
17
/**
18
 * Class MediaAsset.
19
 *
20
 * @since   %VERSION%
21
 *
22
 * @package YIKES\EasyForms\Assets
23
 * @author  Freddie Mixell
24
 */
25
final class MediaAsset {
26
27
	use PluginHelper;
28
29
	const IMG_ASSETS_DIR = '/assets/images/';
30
	const IMG_NOT_FOUND  = '';
31
32
	/**
33
	 * Get internal image URL.
34
	 *
35
	 * @param string $filename filename of image.
36
	 *
37
	 * @return string $file_url
38
	 */
39
	public function get_image( $filename ) {
40
		$relative_file = self::IMG_ASSETS_DIR . $filename;
41
		$file_url      = $this->get_plugin_url( $relative_file );
42
43
		// Build absolute path to file to confirm file exists.
44
		$file_path = $this->get_root_dir() . $relative_file;
45
46
		if ( ! file_exists( $file_path ) ) {
47
			return self::IMG_NOT_FOUND;
48
		}
49
50
		return $file_url;
51
	}
52
}
53