Completed
Push — fix/15025-map-block-infinite-s... ( 9a8b21...52d6c3 )
by
unknown
63:16 queued 56:01
created

Plugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 7 1
A remove() 0 5 1
A is_only() 0 5 3
1
<?php
2
/**
3
 * Plugin connection management class.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
/**
11
 * Plugin connection management class.
12
 * The class represents a single plugin that uses Jetpack connection.
13
 * Its functionality has been pretty simplistic so far: add to the storage (`Plugin_Storage`), remove it from there,
14
 * and determine whether it's the last active connection. As the component grows, there'll be more functionality added.
15
 */
16
class Plugin {
17
18
	/**
19
	 * List of the keys allowed as arguments
20
	 *
21
	 * @var array
22
	 */
23
	private $arguments_whitelist = array(
24
		'url_info',
25
	);
26
27
	/**
28
	 * Plugin slug.
29
	 *
30
	 * @var string
31
	 */
32
	private $slug;
33
34
	/**
35
	 * Initialize the plugin manager.
36
	 *
37
	 * @param string $slug Plugin slug.
38
	 */
39
	public function __construct( $slug ) {
40
		$this->slug = $slug;
41
	}
42
43
	/**
44
	 * Add the plugin connection info into Jetpack.
45
	 *
46
	 * @param string $name Plugin name, required.
47
	 * @param array  $args Plugin arguments, optional.
48
	 *
49
	 * @return $this
50
	 * @see $this->arguments_whitelist
51
	 */
52
	public function add( $name, array $args = array() ) {
53
		$args = compact( 'name' ) + array_intersect_key( $args, array_flip( $this->arguments_whitelist ) );
54
55
		Plugin_Storage::upsert( $this->slug, $args );
56
57
		return $this;
58
	}
59
60
	/**
61
	 * Remove the plugin connection info from Jetpack.
62
	 *
63
	 * @return $this
64
	 */
65
	public function remove() {
66
		Plugin_Storage::delete( $this->slug );
67
68
		return $this;
69
	}
70
71
	/**
72
	 * Determine if this plugin connection is the only one active at the moment, if any.
73
	 *
74
	 * @return bool
75
	 */
76
	public function is_only() {
77
		$plugins = Plugin_Storage::get_all();
78
79
		return ! $plugins || ( array_key_exists( $this->slug, $plugins ) && 1 === count( $plugins ) );
80
	}
81
82
}
83