Passed
Pull Request — master (#371)
by Brian
104:46
created

GetPaid_Data_Store::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * GetPaid Data Store.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Data store class.
11
 */
12
class GetPaid_Data_Store {
13
14
	/**
15
	 * Contains an instance of the data store class that we are working with.
16
	 *
17
	 * @var GetPaid_Data_Store
18
	 */
19
	private $instance = null;
20
21
	/**
22
	 * Contains an array of default GetPaid supported data stores.
23
	 * Format of object name => class name.
24
	 * Example: 'item' => 'GetPaid_Item_Data_Store'
25
	 * You can also pass something like item-<type> for item stores and
26
	 * that type will be used first when available, if a store is requested like
27
	 * this and doesn't exist, then the store would fall back to 'item'.
28
	 * Ran through `getpaid_data_stores`.
29
	 *
30
	 * @var array
31
	 */
32
	private $stores = array(
33
		'item' => 'GetPaid_Item_Data_Store',
34
	);
35
36
	/**
37
	 * Contains the name of the current data store's class name.
38
	 *
39
	 * @var string
40
	 */
41
	private $current_class_name = '';
42
43
	/**
44
	 * The object type this store works with.
45
	 *
46
	 * @var string
47
	 */
48
	private $object_type = '';
49
50
	/**
51
	 * Tells GetPaid_Data_Store which object
52
	 * store we want to work with.
53
	 *
54
	 * @param string $object_type Name of object.
55
	 */
56
	public function __construct( $object_type ) {
57
		$this->object_type = $object_type;
58
		$this->stores      = apply_filters( 'getpaid_data_stores', $this->stores );
59
60
		// If this object type can't be found, check to see if we can load one
61
		// level up (so if item-type isn't found, we try item).
62
		if ( ! array_key_exists( $object_type, $this->stores ) ) {
63
			$pieces      = explode( '-', $object_type );
64
			$object_type = $pieces[0];
65
		}
66
67
		if ( array_key_exists( $object_type, $this->stores ) ) {
68
			$store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] );
69
			if ( is_object( $store ) ) {
70
				$this->current_class_name = get_class( $store );
71
				$this->instance           = $store;
72
			} else {
73
				if ( ! class_exists( $store ) ) {
74
					throw new Exception( __( 'Invalid data store.', 'invoicing' ) );
75
				}
76
				$this->current_class_name = $store;
77
				$this->instance           = new $store();
78
			}
79
		} else {
80
			throw new Exception( __( 'Invalid data store.', 'invoicing' ) );
81
		}
82
	}
83
84
	/**
85
	 * Only store the object type to avoid serializing the data store instance.
86
	 *
87
	 * @return array
88
	 */
89
	public function __sleep() {
90
		return array( 'object_type' );
91
	}
92
93
	/**
94
	 * Re-run the constructor with the object type.
95
	 *
96
	 * @throws Exception When validation fails.
97
	 */
98
	public function __wakeup() {
99
		$this->__construct( $this->object_type );
100
	}
101
102
	/**
103
	 * Loads a data store.
104
	 *
105
	 * @param string $object_type Name of object.
106
	 *
107
	 * @since 1.0.19
108
	 * @throws Exception When validation fails.
109
	 * @return GetPaid_Data_Store
110
	 */
111
	public static function load( $object_type ) {
112
		return new GetPaid_Data_Store( $object_type );
113
	}
114
115
	/**
116
	 * Returns the class name of the current data store.
117
	 *
118
	 * @since 1.0.19
119
	 * @return string
120
	 */
121
	public function get_current_class_name() {
122
		return $this->current_class_name;
123
	}
124
125
	/**
126
	 * Reads an object from the data store.
127
	 *
128
	 * @since 1.0.19
129
	 * @param GetPaid_Data $data GetPaid data instance.
130
	 */
131
	public function read( &$data ) {
132
		$this->instance->read( $data );
133
	}
134
135
	/**
136
	 * Create an object in the data store.
137
	 *
138
	 * @since 1.0.19
139
	 * @param GetPaid_Data $data GetPaid data instance.
140
	 */
141
	public function create( &$data ) {
142
		$this->instance->create( $data );
143
	}
144
145
	/**
146
	 * Update an object in the data store.
147
	 *
148
	 * @since 1.0.19
149
	 * @param GetPaid_Data $data GetPaid data instance.
150
	 */
151
	public function update( &$data ) {
152
		$this->instance->update( $data );
153
	}
154
155
	/**
156
	 * Delete an object from the data store.
157
	 *
158
	 * @since 1.0.19
159
	 * @param GetPaid_Data $data GetPaid data instance.
160
	 * @param array   $args Array of args to pass to the delete method.
161
	 */
162
	public function delete( &$data, $args = array() ) {
163
		$this->instance->delete( $data, $args );
164
	}
165
166
	/**
167
	 * Data stores can define additional function. This passes
168
	 * through to the instance if that function exists.
169
	 *
170
	 * @since 1.0.19
171
	 * @param string $method     Method.
172
	 * @return mixed
173
	 */
174
	public function __call( $method, $parameters ) {
175
		if ( is_callable( array( $this->instance, $method ) ) ) {
176
			$object     = array_shift( $parameters );
177
			$parameters = array_merge( array( &$object ), $parameters );
178
			return call_user_func_array( array( $this->instance, $method ), $parameters );
179
		}
180
	}
181
}
182