RestApi   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare_item_for_response() 0 5 1
A permission() 0 3 1
A register_routes() 0 12 1
A callback() 0 10 2
1
<?php
2
namespace NirjharLo\WP_Plugin_Framework\Src;
3
4
use WP_REST_Controller as WP_REST_Controller;
0 ignored issues
show
Bug introduced by
The type WP_REST_Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
6
if ( ! defined( 'ABSPATH' ) ) exit;
7
8
/**
9
 * Extending REST API framework of WordPress
10
 *
11
 * @author     Nirjhar Lo
12
 * @package    wp-plugin-framework
13
 */
14
if ( ! class_exists( 'RestApi' ) ) {
15
16
	class RestApi extends WP_REST_Controller {
17
18
19
		/**
20
		 * REST API routes
21
		 *
22
		 * @return Object
23
		 */
24
		public function register_routes() {
25
26
			$version = '1';
27
    		$namespace = 'vendor/v' . $version;
28
    		$base = 'route';
29
30
			//Available options for methods are CREATABLE, READABLE, EDITABLE, DELETABLE
31
			register_rest_route( $namespace, '/' . $base, array(
0 ignored issues
show
Bug introduced by
The function register_rest_route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
			/** @scrutinizer ignore-call */ 
32
   register_rest_route( $namespace, '/' . $base, array(
Loading history...
32
        		'methods'             => WP_REST_Server::READABLE,
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Src\WP_REST_Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
        		'callback'            => array( $this, 'callback' ),
34
        		'permission_callback' => array( $this, 'permission' ),
35
        		'args'                => array('sample', 'list', 'of', 'args')
36
      		));
37
		}
38
39
40
		/**
41
		 * The request handler
42
		 *
43
		 * @return Object
44
		 */
45
		public function callback() {
46
47
    		$params = $request->get_params();
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $request seems to be never defined.
Loading history...
48
    		$items = array();
49
    		$data = $this->prepare_item_for_response( $items, $request );
50
51
    		if ( $data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
      			return new WP_REST_Response( $data, 200 );
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Src\WP_REST_Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
    		} else {
54
				return new WP_Error( 'status_code', __( 'message', 'text-domain' ) );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
				return new WP_Error( 'status_code', /** @scrutinizer ignore-call */ __( 'message', 'text-domain' ) );
Loading history...
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Src\WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
			}
56
		}
57
58
59
		/**
60
		 * Prevent unauthorized access here
61
		 *
62
		 * @return Bool
63
		 */
64
		public function permission() {
65
66
			return current_user_can( 'manage_options' );
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
			return /** @scrutinizer ignore-call */ current_user_can( 'manage_options' );
Loading history...
67
		}
68
69
70
		/**
71
		 * Processing of request takes place here
72
		 *
73
		 * @param Array
74
		 * @param Object
75
		 *
76
		 * @return Array
77
		 */
78
		public function prepare_item_for_response($items, $request) {
79
80
			//Process the data in any way you like
81
			$data = compact($items, $request);
82
			return $data;
83
		}
84
	}
85
}
86