Completed
Push — update/group-save-post-sync ( c97a0d )
by
unknown
38:18 queued 31:27
created

Item::get_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Item is a generic class which helps us bring uniformity to data that we sent.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync;
9
10
/**
11
 * Sync Item class lets us bring uniformity to the Sync Data that we send.
12
 */
13
class Item {
14
	/**
15
	 * The state of the object, eg `just_published`.
16
	 *
17
	 * @var mixed
18
	 */
19
	private $state;
20
21
	/**
22
	 * Item constructor.
23
	 *
24
	 * @param string $trigger What caused the Sync Item to be added.
25
	 */
26
	public function __construct( $trigger ) {
27
		$this->trigger = $trigger;
0 ignored issues
show
Bug introduced by
The property trigger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
		return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
29
	}
30
31
	/**
32
	 * Adds the state value.
33
	 *
34
	 * @param string $key Key under which we need to store the value under.
35
	 * @param mixed  $value Value of the key that we want to store.
36
	 */
37
	public function add_state_value( $key, $value ) {
38
		$this->state[ $key ] = $value;
39
	}
40
41
	/**
42
	 * Checks the state value.
43
	 *
44
	 * @param string $key Key that to check.
45
	 *
46
	 * @return bool Whether the staye key is set.
47
	 */
48
	public function has_state( $key ) {
49
		return isset( $this->state[ $key ] );
50
	}
51
52
	/**
53
	 * Checks if the state value for a key is set to true.
54
	 *
55
	 * @param string $key Key to check.
56
	 *
57
	 * @return bool Whether the value of the key is set and if the value is true.
58
	 */
59
	public function is_state_value_true( $key ) {
60
		return ( $this->has_state( $key ) && (bool) $this->get_state_value( $key ) );
61
	}
62
63
	/**
64
	 * Get a specific key value.
65
	 *
66
	 * @param string $key Key to retrieve the value for.
67
	 *
68
	 * @return mixed|null
69
	 */
70
	public function get_state_value( $key ) {
71
		if ( $this->has_state( $key ) ) {
72
			return $this->state[ $key ];
73
		}
74
75
		return null;
76
	}
77
78
	/**
79
	 * Get the whole state array from Sync Item.
80
	 *
81
	 * @return array mixed State of the sync item.
82
	 */
83
	public function get_state() {
84
		return $this->state;
85
	}
86
87
	/**
88
	 * Remove item from the state array.
89
	 *
90
	 * @param string $key Key to unset the value of.
91
	 */
92
	public function unset_state( $key ) {
93
		if ( $this->has_state( $key ) ) {
94
			unset( $this->state[ $key ] );
95
		}
96
	}
97
}
98