Completed
Pull Request — 2.x (#3397)
by Scott Kingsley
06:40
created

PodsAPI_CLI_Command::clear_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 235.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Implements PodsAPI command for WP-CLI
4
 */
5
class PodsAPI_CLI_Command extends WP_CLI_Command {
6
7
	/**
8
	 *
9
	 *
10
	 * @synopsis   --name=<name> --type=<type> --<field>=<value>
11
	 * @subcommand add-pod
12
	 */
13 View Code Duplication
	function add_pod( $args, $assoc_args ) {
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
15
		if ( isset( $assoc_args['id'] ) ) {
16
			unset( $assoc_args['id'] );
17
		}
18
19
		$id = pods_api()->save_pod( $assoc_args );
20
21
		if ( 0 < $id ) {
22
			WP_CLI::success( __( 'Pod added', 'pods' ) );
23
			WP_CLI::line( "ID: {$id}" );
24
		} else {
25
			WP_CLI::error( __( 'Error adding pod', 'pods' ) );
26
		}
27
28
	}
29
30
	/**
31
	 *
32
	 *
33
	 * @synopsis   --<field>=<value>
34
	 * @subcommand save-pod
35
	 */
36
	function save_pod( $args, $assoc_args ) {
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...
37
38
		$id = pods_api()->save_pod( $assoc_args );
39
40
		if ( 0 < $id ) {
41
			WP_CLI::success( __( 'Pod saved', 'pods' ) );
42
			WP_CLI::line( "ID: {$id}" );
43
		} else {
44
			WP_CLI::error( __( 'Error saving pod', 'pods' ) );
45
		}
46
47
	}
48
49
	/**
50
	 *
51
	 *
52
	 * @synopsis   --<field>=<value>
53
	 * @subcommand duplicate-pod
54
	 */
55
	function duplicate_pod( $args, $assoc_args ) {
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...
56
57
		$id = pods_api()->duplicate_pod( $assoc_args );
58
59
		if ( 0 < $id ) {
60
			WP_CLI::success( __( 'Pod duplicated', 'pods' ) );
61
			WP_CLI::line( "New ID: {$id}" );
62
		} else {
63
			WP_CLI::error( __( 'Error duplicating pod', 'pods' ) );
64
		}
65
66
	}
67
68
	/**
69
	 *
70
	 *
71
	 * @synopsis   --<field>=<value>
72
	 * @subcommand reset-pod
73
	 */
74
	function reset_pod( $args, $assoc_args ) {
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...
75
76
		$reset = pods_api()->reset_pod( $assoc_args );
77
78
		if ( $reset ) {
79
			WP_CLI::success( __( 'Pod content reset', 'pods' ) );
80
		} else {
81
			WP_CLI::error( __( 'Error resetting pod', 'pods' ) );
82
		}
83
84
	}
85
86
	/**
87
	 *
88
	 *
89
	 * @synopsis   --<field>=<value>
90
	 * @subcommand delete-pod
91
	 */
92
	function delete_pod( $args, $assoc_args ) {
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...
93
94
		$deleted = pods_api()->delete_pod( $assoc_args );
95
96
		if ( $deleted ) {
97
			WP_CLI::success( __( 'Pod deleted', 'pods' ) );
98
		} else {
99
			WP_CLI::error( __( 'Error deleting pod', 'pods' ) );
100
		}
101
102
	}
103
104
	/**
105
	 * Activate a component
106
	 *
107
	 * @synopsis   --component=<component>
108
	 * @subcommand activate-component
109
	 */
110 View Code Duplication
	function activate_component( $args, $assoc_args ) {
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
112
		if ( ! class_exists( 'PodsInit' ) ) {
113
			WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
114
115
			return;
116
		}
117
118
		$component = $assoc_args['component'];
119
120
		$active = PodsInit::$components->is_component_active( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
121
122
		if ( $active ) {
123
			WP_CLI::error( sprintf( __( 'Component %s is already active', 'pods' ), $component ) );
124
		} else {
125
			PodsInit::$components->activate_component( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
126
127
			WP_CLI::success( __( 'Component activated', 'pods' ) );
128
		}
129
130
	}
131
132
	/**
133
	 * Deactivate a component
134
	 *
135
	 * @synopsis   --component=<component>
136
	 * @subcommand deactivate-component
137
	 */
138 View Code Duplication
	function deactivate_component( $args, $assoc_args ) {
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
140
		if ( ! class_exists( 'PodsInit' ) ) {
141
			WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
142
143
			return;
144
		}
145
146
		$component = $assoc_args['component'];
147
148
		$active = PodsInit::$components->is_component_active( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
149
150
		if ( ! $active ) {
151
			WP_CLI::error( sprintf( __( 'Component %s is already not active', 'pods' ), $component ) );
152
		} else {
153
			PodsInit::$components->deactivate_component( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
154
155
			WP_CLI::success( __( 'Component deactivated', 'pods' ) );
156
		}
157
158
	}
159
160
	/**
161
	 * Clear pods cache
162
	 *
163
	 * @subcommand clear-cache
164
	 */
165
	function clear_cache() {
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...
166
167
		pods_api()->cache_flush_pods();
168
169
		WP_CLI::success( __( 'Pod cache cleared', 'pods' ) );
170
	}
171
172
	/**
173
	 *
174
	 *
175
	 * @synopsis   --pod=<pod> --file=<file>
176
	 * @subcommand export-pod
177
	 */
178
	/*function export_pod ( $args, $assoc_args ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
		$data = pods_api()->load_pod( array( 'name' => $assoc_args[ 'pod' ] ) );
180
181
		if ( !empty( $data ) ) {
182
			$data = json_encode( $data );
183
184
			// @todo write to file
185
		}
186
187
		// @todo success message
188
	}*/
189
190
	/**
191
	 *
192
	 *
193
	 * @synopsis   --file=<file>
194
	 * @subcommand import-pod
195
	 */
196
	/*function import_pod ( $args, $assoc_args ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
197
		$data = ''; // @todo get data from file
198
199
		$package = array();
200
201
		if ( !empty( $data ) )
202
			$package = @json_decode( $data, true );
203
204
		if ( is_array( $package ) && !empty( $package ) ) {
205
			$api = pods_api();
206
207
			if ( isset( $package[ 'id' ] ) )
208
				unset( $package[ 'id' ] );
209
210
			$try = 1;
211
			$check_name = $package[ 'name' ];
212
213
			while ( $api->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) {
214
				$try++;
215
				$check_name = $package[ 'name' ] . $try;
216
			}
217
218
			$package[ 'name' ] = $check_name;
219
220
			$id = $api->save_pod( $package );
221
222
			if ( 0 < $id ) {
223
				WP_CLI::success( __( 'Pod imported', 'pods' ) );
224
				WP_CLI::line( "ID: {$id}" );
225
			}
226
			else
227
				WP_CLI::error( __( 'Error importing pod', 'pods' ) );
228
		}
229
		else
230
			WP_CLI::error( __( 'Invalid package, Pod not imported', 'pods' ) );
231
	}*/
232
233
}
234
235
WP_CLI::add_command( 'pods-api', 'PodsAPI_CLI_Command' );
236