Completed
Push — master ( 678818...49679a )
by Sudar
02:11
created

AddonListRenderer::render_addon()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 40
nc 4
nop 2
dl 0
loc 56
rs 9.0544
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AddonListRenderer::render_empty_list() 0 12 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace EmailLog\Core\UI\Component;
2
3
use EmailLog\Addon\Addon;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Retrieve the list of add-ons and render them.
9
 *
10
 * @since 2.0.0
11
 */
12
class AddonListRenderer {
13
14
	const API_URL = 'https://wpemaillog.com/edd-api/products/?category=addon';
15
	const CACHE_EXPIRY_IN_HRS = 12;
16
	const CACHE_KEY = 'el_addon_list';
17
18
	/**
19
	 * Plugin File.
20
	 *
21
	 * @var string
22
	 */
23
	private $plugin_file;
24
25
	/**
26
	 * Create a new instance with the passed in plugin file.
27
	 *
28
	 * @param string $plugin_file Plugin File.
29
	 */
30
	public function __construct( $plugin_file ) {
31
		$this->plugin_file = $plugin_file;
32
	}
33
34
	/**
35
	 * Setup page to render the list of add-ons.
36
	 */
37
	public function render() {
38
		$email_log = email_log();
39
40
		wp_enqueue_style( 'el_addon_list', plugins_url( 'assets/css/admin/addon-list.css', $this->plugin_file ), array(), $email_log->get_version() );
41
		?>
42
43
		<div class="el-container">
44
			<?php $this->render_addons(); ?>
45
			<div class="clear"></div>
46
		</div> <!-- .el-container -->
47
		<?php
48
	}
49
50
	/**
51
	 * Retrieve the list of add-ons by calling the store API.
52
	 *
53
	 * @return Addon[] List of add-ons, empty array if API call fails.
54
	 */
55
	protected function get_addons() {
56
		if ( false === ( $addons = get_transient( self::CACHE_KEY ) ) ) {
57
			$response = wp_remote_get( self::API_URL );
58
59
			if ( is_wp_error( $response ) || ! is_array( $response ) ) {
60
				// TODO: Don't keep trying if the server is down.
61
				return array();
62
			}
63
64
			$addons = $this->parse_response( wp_remote_retrieve_body( $response ) );
65
66
			if ( ! empty( $addons ) ) {
67
				set_transient( self::CACHE_KEY, $addons, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
68
			}
69
		}
70
71
		return $addons;
72
	}
73
74
	/**
75
	 * Parse the response and get the list of add-on.
76
	 *
77
	 * @param string $response API Response.
78
	 *
79
	 * @return array List of Add-ons.
80
	 */
81
	protected function parse_response( $response ) {
82
		$json = json_decode( $response, true );
83
84
		if ( ! is_array( $json ) || ! array_key_exists( 'products', $json ) ) {
85
			return array();
86
		}
87
88
		return $this->build_addon_list( $json['products'] );
89
	}
90
91
	/**
92
	 * Build a list of Addon objects from products data array.
93
	 *
94
	 * @param array $products Products data array.
95
	 *
96
	 * @return Addon[] List of Addons.
97
	 */
98
	protected function build_addon_list( $products ) {
99
		$addons = array();
100
101
		foreach ( $products as $product ) {
102
			$addons[] = new Addon( $product );
103
		}
104
105
		return $addons;
106
	}
107
108
	/**
109
	 * Render the add-on list or display an error if the list can't be retrieved.
110
	 */
111
	protected function render_addons() {
112
		$addons = $this->get_addons();
113
114
		if ( empty( $addons ) ) {
115
			$this->render_empty_list();
116
		}
117
118
		foreach ( $addons as $addon ) {
119
			$addon->render();
120
		}
121
	}
122
123
	/**
124
	 * Display a notice if the list of add-on can't be retrieved.
125
	 */
126
	protected function render_empty_list() {
127
		?>
128
		<span class="el-addon-empty">
129
			<?php
130
				printf(
131
					__( 'We are not able to retrieve the add-on list now. Please visit the <a href="%s">add-on page</a> to view the add-ons.', 'email-log' ), // @codingStandardsIgnoreLine
132
					'https://wpemaillog.com/addons'
133
				);
134
			?>
135
		</span>
136
		<?php
137
	}
138
}
139