Completed
Push — master ( 470970...a080c6 )
by
unknown
05:31
created

AdImage::getFieldsEnum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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-genereated.
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
  protected function getEndpoint() {
46
    return 'adimages';
47
  }
48
49
  /**
50
   * @return AdImageFields
51
   */
52
  public static function getFieldsEnum() {
53
    return AdImageFields::getInstance();
54
  }
55
56
  protected static function getReferencedEnums() {
57
    $ref_enums = array();
58
    $ref_enums['Status'] = AdImageStatusValues::getInstance()->getValues();
59
    return $ref_enums;
60
  }
61
62
63
  public function getSelf(array $fields = array(), array $params = array(), $pending = false) {
64
    $this->assureId();
65
66
    $param_types = array(
67
    );
68
    $enums = array(
69
    );
70
71
    $request = new ApiRequest(
72
      $this->api,
73
      $this->data['id'],
74
      RequestInterface::METHOD_GET,
75
      '/',
76
      new AdImage(),
77
      'NODE',
78
      AdImage::getFieldsEnum()->getValues(),
79
      new TypeChecker($param_types, $enums)
80
    );
81
    $request->addParams($params);
82
    $request->addFields($fields);
83
    return $pending ? $request : $request->execute();
84
  }
85
86
  /**
87
   * Uploads images from a zip file and returns a cursor of results
88
   *
89
   * @param string $file_path
90
   * @param string $account_id
91
   * @param array $params
92
   * @param Api $api
93
   * @return array
94
   */
95
  public static function createFromZip(
96
    $file_path, $account_id, array $params = array(), Api $api = null) {
97
98
    $image = new AdImage(null, $account_id, $api);
99
    $image->{AdImageFields::FILENAME} = $file_path;
100
    return $image->arrayFromZip($params);
101
  }
102
103
  /**
104
   * @return string
105
   */
106
  protected function getNodePath() {
107
    return '/'.$this->assureParentId().'/'.$this->getEndpoint();
108
  }
109
110
  /**
111
   * Create function for the object.
112
   *
113
   * @param array $params Additional parameters to include in the request
114
   * @return $this
115
   * @throws \Exception
116
   * @throws \RuntimeException
117
   */
118
  public function create(array $params = array()) {
119
    if ($this->data[static::FIELD_ID]) {
120
      throw new \Exception("Object has already an ID");
121
    }
122
123
    if ($this->isZipFile($this->data[AdImageFields::FILENAME])) {
124
      throw new \RuntimeException(
125
        "use AdImage::createFromZip to create zip files");
126
    }
127
128
    $data = $this->exportData();
129
    $filename = $data[AdImageFields::FILENAME];
130
    unset($data[AdImageFields::FILENAME]);
131
    $params = array_merge($data, $params);
132
133
    $request = $this->getApi()->prepareRequest(
134
      '/'.$this->assureParentId().'/'.$this->getEndpoint(),
135
      RequestInterface::METHOD_POST,
136
      $params
137
    );
138
139
    $request->getFileParams()->offsetSet(AdImageFields::FILENAME, $filename);
140
    $response = $this->getApi()->executeRequest($request);
141
142
    $this->clearHistory();
143
    $content = $response->getContent();
144
    $images = $content['images'];
145
    $data = $images[basename($this->{AdImageFields::FILENAME})];
146
147
    $this->data[AdImageFields::HASH] = $data[AdImageFields::HASH];
148
149
    $this->data[static::FIELD_ID]
150
      = substr($this->getParentId(), 4).':'.$this->data[AdImageFields::HASH];
151
152
    return $this;
153
  }
154
155
  /**
156
   * Uploads images from a zip file and returns a cursor of results
157
   *
158
   * @param array $params
159
   * @return array
160
   * @throws \RuntimeException
161
   */
162
  protected function arrayFromZip($params = array()) {
163
    if (!$this->isZipFile($this->data[AdImageFields::FILENAME])) {
164
      throw new \RuntimeException(
165
        $this->data[AdImageFields::FILENAME]." doesn't resolve to a zip file");
166
    }
167
168
    $data = $this->exportData();
169
    $filename = $data[AdImageFields::FILENAME];
170
    unset($data[AdImageFields::FILENAME]);
171
    $params = array_merge($data, $params);
172
173
    $request = $this->getApi()->prepareRequest(
174
      '/'.$this->assureParentId().'/'.$this->getEndpoint(),
175
      RequestInterface::METHOD_POST,
176
      $params
177
    );
178
179
    $request->getFileParams()->offsetSet(AdImageFields::FILENAME, $filename);
180
    $response = $this->getApi()->executeRequest($request);
181
182
    $result = array();
183
    $content = $response->getContent();
184
    foreach ($content['images'] as $image) {
185
      $adimage = new AdImage(
186
        substr($this->getParentId(), 4).':'.$image[AdImageFields::HASH],
187
        $this->getParentId(),
188
        $this->getApi());
189
190
      $adimage->{AdImageFields::HASH} = $image[AdImageFields::HASH];
191
192
      $result[] = $adimage;
193
    }
194
195
    return $result;
196
  }
197
198
  /**
199
   * Checks if a given path is a zip file
200
   *
201
   * @param string $file_path
202
   * @return bool
203
   */
204
  protected function isZipFile($file_path) {
205
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
206
    $file_mime_type = finfo_file($finfo, $file_path);
207
    return $file_mime_type == 'application/zip' ||
208
      $file_mime_type == 'multipart/x-zip';
209
  }
210
211
  /**
212
   * Read object data from the graph
213
   *
214
   * @param string[] $fields Fields to request
215
   * @param array $params Additional request parameters
216
   * @return $this
217
   */
218
  public function read(array $fields = array(), array $params = array()) {
219
    $fields = implode(',', $fields ?: static::getDefaultReadFields());
220
    if ($fields) {
221
      $params['fields'] = $fields;
222
    }
223
    $params['hashes'] = array(explode(':', $this->assureId())[1]);
224
225
    $response = $this->getApi()->call(
226
      $this->getNodePath(),
227
      RequestInterface::METHOD_GET,
228
      $params);
229
230
    $content = $response->getContent();
231
    $data = $content['data'];
232
    if ($data) {
233
      $this->setDataWithoutValidation((array) $data[0]);
234
    }
235
236
    $this->clearHistory();
237
238
    return $this;
239
  }
240
241
  /**
242
   * Delete this object from the graph
243
   *
244
   * @param array $params
245
   * @return void
246
   * @throws \Exception
247
   */
248
  public function delete(array $params = array()) {
249
    if (!$this->data[AdImageFields::HASH]) {
250
      throw new \Exception("AdImage hash is required to delete");
251
    }
252
253
    $params
254
      = array_merge($params, array('hash' => $this->data[AdImageFields::HASH]));
255
256
    parent::delete($params);
257
  }
258
}
259