Completed
Branch BETA-4.9-messages-queue-fixed (941081)
by
unknown
17:38 queued 10s
created

Meta   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 101
rs 10
c 4
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle_request_models_meta() 0 12 2
C _get_models_metadata_entity() 0 55 12
A filter_ee_metadata_into_index() 0 19 2
1
<?php
2
namespace EventEspresso\core\libraries\rest_api\controllers\model;
3
if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) {
4
	exit( 'No direct script access allowed' );
5
}
6
7
/**
8
 *
9
 * Controller for handling requests regarding meta info about the models
10
 *
11
 * Handles requests relating to meta info
12
 *
13
 * @package			Event Espresso
14
 * @subpackage
15
 * @author				Mike Nelson
16
 *
17
 */
18
class Meta extends Base {
19
20
21
	public static function handle_request_models_meta( \WP_REST_Request $request ) {
22
		$controller = new Meta();
23
		$matches = $controller->parse_route( 
24
			$request->get_route(), 
25
			'~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . 'resources~', 
26
			array( 'version' ) ); 
27
		if( $matches instanceof \WP_REST_Response ) {
0 ignored issues
show
Bug introduced by
The class WP_REST_Response does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
28
			return $matches;
29
		}
30
		$controller->set_requested_version( $matches[ 'version' ] );
31
		return $controller->send_response( $controller->_get_models_metadata_entity() );
32
	}
33
34
	/*
35
	 * Gets the model metadata resource entity
36
	 * @return array for JSON response, describing all the models available in teh requested version
37
	 */
38
	protected function _get_models_metadata_entity(){
39
		$response = array();
40
		foreach( $this->get_model_version_info()->models_for_requested_version() as $model_name => $model_classname ){
41
			$model = $this->get_model_version_info()->load_model( $model_name );
42
			$fields_json = array();
43
			foreach( $this->get_model_version_info()->fields_on_model_in_this_version( $model ) as $field_name => $field_obj ) {
0 ignored issues
show
Documentation introduced by
$model is of type boolean, but the function expects a object<EEM_Base>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
				if( $this->get_model_version_info()->field_is_ignored( $field_obj ) ) {
45
					continue;
46
				}
47
				if( $field_obj instanceof \EE_Boolean_Field ) {
48
					$datatype = 'Boolean';
49
				}elseif( $field_obj->get_wpdb_data_type() == '%d' ) {
50
					$datatype = 'Number';
51
				}elseif( $field_name instanceof \EE_Serialized_Text_Field ) {
52
					$datatype = 'Object';
53
				}else{
54
					$datatype = 'String';
55
				}
56
				$default_value = $field_obj->get_default_value();
57
				if( $default_value === EE_INF ) {
58
					$default_value = EE_INF_IN_DB;
59
				} elseif( $field_obj instanceof \EE_Datetime_Field &&
60
					$default_value instanceof \DateTime ) {
61
					$default_value = $default_value->format( 'c' );
62
				}
63
				$field_json = array(
64
					'name' => $field_name,
65
					'nicename' => $field_obj->get_nicename(),
66
					'has_rendered_format' => $this->get_model_version_info()->field_has_rendered_format( $field_obj ),
67
					'has_pretty_format' => $this->get_model_version_info()->field_has_pretty_format( $field_obj ),
68
					'type' => str_replace('EE_', '', get_class( $field_obj ) ),
69
					'datatype' => $datatype,
70
					'nullable' => $field_obj->is_nullable(),
71
					'default' => $default_value,
72
					'table_alias' => $field_obj->get_table_alias(),
73
					'table_column' => $field_obj->get_table_column(),
74
				);
75
				$fields_json[ $field_json[ 'name' ] ] = $field_json;
76
77
			}
78
			$fields_json = array_merge( $fields_json, $this->get_model_version_info()->extra_resource_properties_for_model( $model ) );
0 ignored issues
show
Documentation introduced by
$model is of type boolean, but the function expects a object<EEM_Base>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
			$response[ $model_name ]['fields'] = apply_filters( 'FHEE__Meta__handle_request_models_meta__fields', $fields_json, $model );
80
			$relations_json = array();
81
			foreach( $model->relation_settings()  as $relation_name => $relation_obj ) {
0 ignored issues
show
Bug introduced by
The method relation_settings cannot be called on $model (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82
				$relation_json = array(
83
					'name' => $relation_name,
84
					'type' => str_replace( 'EE_', '', get_class( $relation_obj ) ),
85
					'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false,
86
				);
87
				$relations_json[ $relation_name ] = $relation_json;
88
			}
89
			$response[ $model_name ][ 'relations' ] = apply_filters( 'FHEE__Meta__handle_request_models_meta__relations', $relations_json, $model );
90
		}
91
		return $response;
92
	}
93
94
	/**
95
	 * Adds EE metadata to the index
96
	 * @param WP_REST_Response $rest_response_obj
97
	 * @return WP_REST_Response
98
	 */
99
	public static function filter_ee_metadata_into_index( $rest_response_obj ) {
100
		$response_data = $rest_response_obj->get_data();
101
		$addons = array();
102
		foreach( \EE_Registry::instance()->addons as $addon){
103
			$addon_json = array(
104
				'name' => $addon->name(),
105
				'version' => $addon->version()
106
			);
107
			$addons[ $addon_json[ 'name' ] ] = $addon_json;
108
		}
109
		$response_data[ 'ee' ] = array(
110
			'version' => \EEM_System_Status::instance()->get_ee_version(),
111
			'addons' => $addons,
112
			'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(),
113
			'served_core_versions' => array_keys( \EED_Core_Rest_Api::versions_served() )
114
		);
115
		$rest_response_obj->set_data( $response_data );
116
		return $rest_response_obj;
117
	}
118
}
119
120
121
// End of file Read.class.php