GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (e406d1)
by
unknown
02:22
created

Extensions::fetch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 11
nc 3
nop 2
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Class Extensions
7
 * @package HM\BackUpWordPress
8
 */
9
final class Extensions {
10
11
	/**
12
	 * Contains the instantiated Extensions instance.
13
	 *
14
	 * @var Extensions $this->instance
15
	 */
16
	private static $instance;
17
18
	/**
19
	 * Holds the root URL of the API.
20
	 *
21
	 * @var string
22
	 */
23
	protected $root_url = '';
24
25
	/**
26
	 * Extensions constructor.
27
	 *
28
	 */
29
	private function __construct() {
30
31
		$this->root_url  = 'https://bwp.hmn.md/wp-json/wp/v2/';
32
33
	}
34
35
	private function __wakeup() {}
36
37
	private function __clone() {}
38
39
	/**
40
	 * Returns the *Singleton* instance of this class.
41
	 *
42
	 * @staticvar Extensions $instance The *Singleton* instances of this class.
43
	 *
44
	 * @return Extensions The *Singleton* instance.
45
	 */
46
	public static function get_instance() {
47
48
		if ( ! ( self::$instance instanceof Extensions ) ) {
49
50
			self::$instance = new Extensions();
51
52
		}
53
54
		return self::$instance;
55
56
	}
57
58
	/**
59
	 * Parses the body of the API response and returns it.
60
	 *
61
	 * @return array|bool|mixed|object
62
	 */
63
	public function get_edd_data() {
64
65
		$response = $this->fetch( 'edd-downloads' );
66
67
		if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
68
			return false;
69
		}
70
71
		return json_decode( $response['body'] );
72
73
	}
74
75
	/**
76
	 * Makes a request to the JSON API or retrieves the cached response. Caches the response for one day.
77
	 *
78
	 * @param $endpoint
79
	 * @param int $ttl
80
	 *
81
	 * @return array|mixed|\WP_Error
82
	 */
83
	protected function fetch( $endpoint, $ttl = DAY_IN_SECONDS ) {
84
85
		$request_url = $this->root_url . $endpoint;
86
87
		$cache_key = md5( $request_url );
88
89
		$cached = get_transient( 'bwp_' . $cache_key );
90
91
		if ( $cached ) {
92
			return $cached;
93
		}
94
95
		$response = wp_remote_get( $request_url );
96
97
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
98
			return new \WP_Error( 'hmbkp-error', 'Unable to fetch API response' );
99
		}
100
101
		set_transient( 'bwp_' . $cache_key, $response, $ttl );
102
103
		return $response;
104
105
	}
106
107
}
108