Completed
Pull Request — develop (#1249)
by Naveen
02:52
created

Key_Validation_Notice::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Admin;
4
5
use Wordlift\Cache\Ttl_Cache;
6
7
/**
8
 * @since 3.28.0
9
 * @author Naveen Muthusamy <[email protected]>
10
 */
11
class Key_Validation_Notice {
12
13
	const CACHE_KEY = 'is_key_valid';
14
15
	/**
16
	 * @var \Wordlift_Key_Validation_Service
17
	 */
18
	private $key_validation_service;
19
20
	/**
21
	 * @var \Wordlift_Configuration_Service
22
	 */
23
	private $configuration_service;
24
	/**
25
	 * @var Ttl_Cache
26
	 */
27
	private $ttl_cache_service;
28
29
	/**
30
	 * Key_Validation_Notice constructor.
31
	 *
32
	 * @param \Wordlift_Key_Validation_Service $key_validation_service
33
	 * @param \Wordlift_Configuration_Service $configuration_service
34
	 */
35
	public function __construct( $key_validation_service, $configuration_service ) {
36
37
		$this->key_validation_service = $key_validation_service;
38
39
		$this->configuration_service = $configuration_service;
40
41
		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification', 60 * 60 * 8 );
42
43
		add_action( 'admin_notices', function () {
44
45
			$key = $this->configuration_service->get_key();
46
47
			if ( ! $key ) {
48
				// Dont show warning or make API call, return early.
49
				return;
50
			}
51
52
			if ( $this->is_key_valid() ) {
53
				return;
54
			}
55
56
			echo $this->get_notification_template();
57
58
		} );
59
60
	}
61
62
63
	public function get_notification_template() {
64
		return <<<EOF
65
<p></p>
66
EOF;
67
68
	}
69
70
	private function is_key_valid() {
71
72
		$key = $this->configuration_service->get_key();
73
74
		// Check cache if the result is present, if not get the results
75
		// save it and return the data.
76
		if ( $this->ttl_cache_service->get( self::CACHE_KEY ) !== null ) {
77
			return $this->ttl_cache_service->get( self::CACHE_KEY );
78
		}
79
80
		$is_valid = $this->key_validation_service->is_key_valid( $key );
81
		$this->ttl_cache_service->put( self::CACHE_KEY, $is_valid );
82
83
		return $is_valid;
84
	}
85
86
}
87
88