Completed
Push — develop ( c1be74...f8bbf2 )
by David
06:55
created

Response_Adapter::init_from_env()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Features;
4
5
class Response_Adapter {
6
	const WL_FEATURES = '_wl_features';
7
	const WL_1 = 'wl1';
8
9
	/**
10
	 * @var \Wordlift_Log_Service
11
	 */
12
	private $log;
13
14
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
16
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
17
18
		// Filter responses from the API calls to update the enabled features.
19
		add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 );
20
21
		// Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift
22
		// before it can actually query the enabled features via HTTP (mock), which would prevent files from being included.
23
//		$this->init_from_env();
24
25
		// Register the `wl_features__enable__{feature-name}` filters.
26
		$this->register_filters();
27
28
		// Hook to updates to the features setting to refresh the features' filters.
29
		add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 );
30
31
	}
32
33
	function response( $response ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
35
		$headers = wp_remote_retrieve_headers( $response );
36
37
		// Bail out if the `wl1` header isn't defined.
38
		if ( ! isset( $headers[ self::WL_1 ] ) ) {
39
			return $response;
40
		}
41
		$wl1_as_base64_string = $headers[ self::WL_1 ];
42
		$wl1                  = json_decode( base64_decode( $wl1_as_base64_string ), true );
43
44
		$this->log->debug( "WL1 [ encoded :: $wl1_as_base64_string ] " . var_export( $wl1, true ) );
45
46
		// Update the feature flags. There's no need to check here if values differ (thus avoiding a call to db), since
47
		// WordPress does that in `update_option`.
48
		if ( isset( $wl1['features'] ) ) {
49
			if ( update_option( self::WL_FEATURES, (array) $wl1['features'], true ) ) {
50
				$this->register_filters();
51
			}
52
		}
53
54
		return $response;
55
	}
56
57
	/**
58
	 * Registers the feature filters.
59
	 */
60
	function register_filters() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
62
		$this->log->debug( 'Registering feature filters...' );
63
64
		foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) {
65
			// Remove previous filters.
66
			remove_filter( "wl_feature__enable__${name}", '__return_true' );
67
			remove_filter( "wl_feature__enable__${name}", '__return_false' );
68
69
			$callback = ( $enabled ? '__return_true' : '__return_false' );
70
			add_filter( "wl_feature__enable__${name}", $callback );
71
		}
72
73
	}
74
75
	private function init_from_env() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
76
		$features = array_reduce( array_filter( array_keys( $_ENV ), function ( $key ) {
77
			return preg_match( '~^WL_FEATURES__.*~', $key );
78
		} ), function ( $features, $env ) {
79
			$name              = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) );
80
			$value             = wp_validate_boolean( getenv( $env ) );
81
			$features[ $name ] = $value;
82
83
			return $features;
84
		}, array() );
85
86
		update_option( self::WL_FEATURES, (array) $features, true );
87
88
	}
89
90
}
91