AdImage::read()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 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\Object;
26
27
use FacebookAds\ApiRequest;
28
use FacebookAds\Cursor;
29
use FacebookAds\Http\RequestInterface;
30
use FacebookAds\TypeChecker;
31
use FacebookAds\Object\Fields\AdImageFields;
32
use FacebookAds\Object\Values\AdImageStatusValues;
33
34
/**
35
 * This class is auto-generated.
36
 *
37
 * For any issues or feature requests related to this class, please let us know
38
 * on github and we'll fix in our codegen framework. We'll not be able to accept
39
 * pull request for this class.
40
 *
41
 */
42
43
class AdImage extends AbstractCrudObject {
44
45
  /**
46
   * @deprecated getEndpoint function is deprecated
47
   */
48
  protected function getEndpoint() {
49
    return 'adimages';
50
  }
51
52
  /**
53
   * @return AdImageFields
54
   */
55
  public static function getFieldsEnum() {
56
    return AdImageFields::getInstance();
57
  }
58
59
  protected static function getReferencedEnums() {
60
    $ref_enums = array();
61
    $ref_enums['Status'] = AdImageStatusValues::getInstance()->getValues();
62
    return $ref_enums;
63
  }
64
65
66
  public function getSelf(array $fields = array(), array $params = array(), $pending = false) {
67
    $this->assureId();
68
69
    $param_types = array(
70
    );
71
    $enums = array(
72
    );
73
74
    $request = new ApiRequest(
75
      $this->api,
76
      $this->data['id'],
77
      RequestInterface::METHOD_GET,
78
      '/',
79
      new AdImage(),
80
      'NODE',
81
      AdImage::getFieldsEnum()->getValues(),
82
      new TypeChecker($param_types, $enums)
83
    );
84
    $request->addParams($params);
85
    $request->addFields($fields);
86
    return $pending ? $request : $request->execute();
87
  }
88
89
  /**
90
   * Uploads images from a zip file and returns a cursor of results
91
   *
92
   * @param string $file_path
93
   * @param string $account_id
94
   * @param array $params
95
   * @param Api $api
96
   * @return array
97
   */
98
  public static function createFromZip(
99
    $file_path, $account_id, array $params = array(), Api $api = null) {
100
101
    $image = new AdImage(null, $account_id, $api);
102
    $image->{AdImageFields::FILENAME} = $file_path;
103
    return $image->arrayFromZip($params);
104
  }
105
106
  /**
107
   * @return string
108
   */
109
  protected function getNodePath() {
110
    return '/'.$this->assureParentId().'/'.$this->getEndpoint();
0 ignored issues
show
Deprecated Code introduced by
The method FacebookAds\Object\AdImage::getEndpoint() has been deprecated with message: getEndpoint function is deprecated

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
111
  }
112
113
  /**
114
   * Create function for the object.
115
   *
116
   * @param array $params Additional parameters to include in the request
117
   * @return $this
118
   * @throws \Exception
119
   * @throws \RuntimeException
120
   */
121
  public function create(array $params = array()) {
122
    if ($this->data[static::FIELD_ID]) {
123
      throw new \Exception("Object has already an ID");
124
    }
125
126
    if ($this->isZipFile($this->data[AdImageFields::FILENAME])) {
127
      throw new \RuntimeException(
128
        "use AdImage::createFromZip to create zip files");
129
    }
130
131
    $data = $this->exportData();
132
    $filename = $data[AdImageFields::FILENAME];
133
    unset($data[AdImageFields::FILENAME]);
134
    $params = array_merge($data, $params);
135
136
    $request = $this->getApi()->prepareRequest(
137
      '/'.$this->assureParentId().'/'.$this->getEndpoint(),
0 ignored issues
show
Deprecated Code introduced by
The method FacebookAds\Object\AdImage::getEndpoint() has been deprecated with message: getEndpoint function is deprecated

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
138
      RequestInterface::METHOD_POST,
139
      $params
140
    );
141
142
    $request->getFileParams()->offsetSet(AdImageFields::FILENAME, $filename);
143
    $response = $this->getApi()->executeRequest($request);
144
145
    $this->clearHistory();
146
    $content = $response->getContent();
147
    $data = $content['images'][basename($this->{AdImageFields::FILENAME})];
148
149
    $this->data[AdImageFields::HASH] = $data[AdImageFields::HASH];
150
151
    $this->data[static::FIELD_ID]
152
      = substr($this->getParentId(), 4).':'.$this->data[AdImageFields::HASH];
153
154
    return $this;
155
  }
156
157
  /**
158
   * Uploads images from a zip file and returns a cursor of results
159
   *
160
   * @param array $params
161
   * @return array
162
   * @throws \RuntimeException
163
   */
164
  protected function arrayFromZip($params = array()) {
165
    if (!$this->isZipFile($this->data[AdImageFields::FILENAME])) {
166
      throw new \RuntimeException(
167
        $this->data[AdImageFields::FILENAME]." doesn't resolve to a zip file");
168
    }
169
170
    $data = $this->exportData();
171
    $filename = $data[AdImageFields::FILENAME];
172
    unset($data[AdImageFields::FILENAME]);
173
    $params = array_merge($data, $params);
174
175
    $request = $this->getApi()->prepareRequest(
176
      '/'.$this->assureParentId().'/'.$this->getEndpoint(),
0 ignored issues
show
Deprecated Code introduced by
The method FacebookAds\Object\AdImage::getEndpoint() has been deprecated with message: getEndpoint function is deprecated

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
177
      RequestInterface::METHOD_POST,
178
      $params
179
    );
180
181
    $request->getFileParams()->offsetSet(AdImageFields::FILENAME, $filename);
182
    $response = $this->getApi()->executeRequest($request);
183
184
    $result = array();
185
    $content = $response->getContent();
186
    foreach ($content['images'] as $image) {
187
      $adimage = new AdImage(
188
        substr($this->getParentId(), 4).':'.$image[AdImageFields::HASH],
189
        $this->getParentId(),
190
        $this->getApi());
191
192
      $adimage->{AdImageFields::HASH} = $image[AdImageFields::HASH];
193
194
      $result[] = $adimage;
195
    }
196
197
    return $result;
198
  }
199
200
  /**
201
   * Checks if a given path is a zip file
202
   *
203
   * @param string $file_path
204
   * @return bool
205
   */
206
  protected function isZipFile($file_path) {
207
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
208
    $file_mime_type = finfo_file($finfo, $file_path);
209
    return $file_mime_type == 'application/zip' ||
210
      $file_mime_type == 'multipart/x-zip';
211
  }
212
213
  /**
214
   * Read object data from the graph
215
   *
216
   * @param string[] $fields Fields to request
217
   * @param array $params Additional request parameters
218
   * @return $this
219
   */
220
  public function read(array $fields = array(), array $params = array()) {
221
    $fields = implode(',', $fields ?: static::getDefaultReadFields());
222
    if ($fields) {
223
      $params['fields'] = $fields;
224
    }
225
    $params['hashes'] = array(explode(':', $this->assureId())[1]);
226
227
    $response = $this->getApi()->call(
228
      $this->getNodePath(),
229
      RequestInterface::METHOD_GET,
230
      $params);
231
232
    $content = $response->getContent();
233
    $data = $content['data'];
234
    if ($data) {
235
      $this->setDataWithoutValidation((array) $data[0]);
236
    }
237
238
    $this->clearHistory();
239
240
    return $this;
241
  }
242
243
  /**
244
   * Delete this object from the graph
245
   *
246
   * @param array $params
247
   * @return void
248
   * @throws \Exception
249
   */
250
  public function delete(array $params = array()) {
251
    if (!$this->data[AdImageFields::HASH]) {
252
      throw new \Exception("AdImage hash is required to delete");
253
    }
254
255
    $params
256
      = array_merge($params, array('hash' => $this->data[AdImageFields::HASH]));
257
258
    parent::delete($params);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on FacebookAds\Object\AbstractCrudObject. Did you maybe mean deleteSelf()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
259
  }
260
}
261