ApiRequest   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 3
dl 0
loc 186
ccs 0
cts 117
cp 0
rs 8.2857
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 1
B addParam() 0 23 6
A addParams() 0 6 2
A removeParam() 0 8 3
A clearParams() 0 5 1
A getParams() 0 4 1
A addField() 0 11 4
A addFields() 0 6 2
A removeField() 0 7 2
A clearFields() 0 4 1
A getFields() 0 3 1
A addFile() 0 11 4
B execute() 0 17 5
A extractValue() 0 13 4
A createObject() 0 8 2
1
<?php
2
/**
3
 * Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
namespace FacebookAds;
26
27
use FacebookAds\Cursor;
28
use FacebookAds\AbstractObject;
29
30
class ApiRequest {
31
  protected $api;
32
  protected $id;
33
  protected $method;
34
  protected $endpoint;
35
  protected $return_prototype;
0 ignored issues
show
Coding Style introduced by
$return_prototype does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
36
  protected $accepted_fields;
0 ignored issues
show
Coding Style introduced by
$accepted_fields does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
37
  protected $param_checker;
0 ignored issues
show
Coding Style introduced by
$param_checker does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
38
  protected $api_type;
0 ignored issues
show
Coding Style introduced by
$api_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
39
  protected $use_graph_video_endpoint;
0 ignored issues
show
Coding Style introduced by
$use_graph_video_endpoint does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
40
  private $fields;
41
  private $params;
42
  private $file_params;
0 ignored issues
show
Coding Style introduced by
$file_params does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
43
  private $allow_file_upload;
0 ignored issues
show
Coding Style introduced by
$allow_file_upload does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
44
  private $file_counter;
0 ignored issues
show
Coding Style introduced by
$file_counter does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
45
46
  public function __construct(
47
    Api $api,
48
    $id,
49
    $method,
50
    $endpoint,
51
    $return_prototype = null,
52
    $api_type = null,
53
    $accepted_fields = array(),
54
    TypeChecker $param_checker = null,
55
    $allow_file_upload = false,
56
    $use_graph_video_endpoint = false) {
57
    $this->fields = [];
58
    $this->params = [];
59
    $this->file_params = [];
60
    $this->file_counter = 0;
61
    $this->api = $api;
62
    $this->id = $id;
63
    $this->method = $method;
64
    $this->endpoint = $endpoint;
65
    $this->return_prototype = $return_prototype;
66
    $this->api_type = $api_type;
67
    $this->accepted_fields = $accepted_fields;
68
    $this->param_checker = $param_checker;
69
    $this->allow_file_upload = $allow_file_upload;
70
    $this->use_graph_video_endpoint = $use_graph_video_endpoint;
71
  }
72
73
  public function addParam($param, $value) {
74
    $extracted_value = $this->extractValue($value);
75
    if (!ApiConfig::TYPE_CHECKER_STRICT_MODE
76
      || !$this->param_checker->isValidParam($param)
77
    ) {
78
      $this->params[$param] = $extracted_value;
79
    } else {
80
      if ($this->param_checker->isValidParamPair($param, $value)) {
81
        if ($this->param_checker->isFileParam($param)) {
82
          $this->file_params[$param] = $extracted_value;
83
        } else {
84
          $this->params[$param] = $extracted_value;
85
        }
86
      } elseif ($this->param_checker->isPrimitiveType($param)) {
87
        $param_type = $this->param_checker->getType($param);
88
        $this->params[$param] = $this->param_checker->convertStringToPrimType(
89
          $param_type, $value);
90
      } else {
91
        throw new \LogicException('The value for '.$param.' is not compatible');
92
      }
93
    }
94
    return $this;
95
  }
96
97
  public function addParams($params) {
98
    foreach ($params as $key => $value) {
99
      $this->addParam($key, $value);
100
    }
101
    return $this;
102
  }
103
104
  public function removeParam($param) {
105
    if (array_key_exists($param, $this->params)) {
106
      unset($this->params[$param]);
107
    } elseif (array_key_exists($param, $this->params)) {
108
      unset($this->file_params[$param]);
109
    }
110
    return $this;
111
  }
112
113
  public function clearParams() {
114
    $this->params = [];
115
    $this->file_params = [];
116
    return this;
117
  }
118
119
  public function getParams() {
120
    $all_params = array_merge($this->params, $this->file_params);
121
    return $all_params;
122
  }
123
124
  public function addField($field) {
125
    if (ApiConfig::TYPE_CHECKER_STRICT_MODE
126
      && !in_array($field, $this->accepted_fields)
127
    ) {
128
      throw new \LogicException('Field '.$field.' is not supported');
129
    }
130
    if (!(in_array($field, $this->fields))) {
131
      $this->fields[] = $field;
132
    }
133
    return $this;
134
  }
135
136
  public function addFields($fields) {
137
    foreach ($fields as $field) {
138
      $this->addField($field);
139
    }
140
    return $this;
141
  }
142
143
  public function removeField($field) {
144
    if (in_array($field, $this->fields)) {
145
      $index_to_remove = array_search($field, $this->fields);
146
      unset($this->fields[$index_to_remove]);
147
    }
148
    return $this;
149
  }
150
151
  public function clearFields() {
152
    $this->fields = [];
153
    return this;
154
  }
155
156
  public function getFields() {
157
    return $this->fields;
158
  }
159
160
  public function addFile($filename) {
161
    if (ApiConfig::TYPE_CHECKER_STRICT_MODE && !$this->allow_file_upload) {
162
      throw new \LogicException("This api cannot upload files");
163
    }
164
    $file_key = 'source'.$this->file_counter;
165
    if (file_exists($filename)) {
166
      $this->file_params[$file_key] = $filename;
167
      $this->file_counter++;
168
    }
169
    return $this;
170
  }
171
172
  /**
173
   * Execute the request
174
   */
175
  public function execute() {
176
    $url_path = '/'.$this->id.$this->endpoint;
177
    $updated_params = $this->params;
178
    if (!empty($this->fields)) {
179
      $fields = implode(',', $this->fields);
180
      $updated_params['fields'] = $fields;
181
    }
182
    $response = $this->api->call(
183
      $url_path, $this->method, $updated_params, $this->file_params);
0 ignored issues
show
Unused Code introduced by
The call to Api::call() has too many arguments starting with $this->file_params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
184
    if ($this->api_type === "EDGE" && $this->method === "GET") {
185
      return new Cursor($response, $this->return_prototype, $this->api);
186
    } else if ($this->method === "DELETE") {
187
      return $response;
188
    } else {
189
      return $this->createObject($response->getContent());
0 ignored issues
show
Bug introduced by
It seems like $response->getContent() targeting FacebookAds\Http\ResponseInterface::getContent() can also be of type null; however, FacebookAds\ApiRequest::createObject() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
190
    }
191
  }
192
193
  protected function extractValue($value) {
194
    if ($this->param_checker->isAbstractObject($value)) {
195
      return $value->exportAllData();
196
    } else if (is_array($value)) {
197
      $extracted_value = [];
198
      foreach ($value as $key => $sub_value) {
199
        $extracted_value[$key] = $this->extractValue($sub_value);
200
      }
201
      return $extracted_value;
202
    } else {
203
      return $value;
204
    }
205
  }
206
207
  protected function createObject(array $object_data) {
208
    $object = clone $this->return_prototype;
209
    $object->setDataWithoutValidation($object_data);
210
    if ($object instanceof AbstractCrudObject) {
0 ignored issues
show
Bug introduced by
The class FacebookAds\AbstractCrudObject 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...
211
      $object->setApi($this->api);
212
    }
213
    return $object;
214
  }
215
}
216