/
Report.php
379 lines (313 loc) · 7.3 KB
/
Report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Project: google-ads.
* User: Edujugon
* Email: edujugon@gmail.com
* Date: 10/2/17
* Time: 10:43
*/
namespace Edujugon\GoogleAds\Reports;
use Edujugon\GoogleAds\Session\AdwordsSession;
use Google\AdsApi\AdWords\Reporting\v201702\ReportDownloader;
use Google\AdsApi\AdWords\v201702\cm\ApiException;
class Report
{
protected $session;
/**
* @var ReportDownloader
*/
protected $reportDownloader;
/**
* @var
*/
protected $format;
/**
* Report Data
* @var
*/
protected $data;
/**
* @var
*/
protected $query = '';
/**
* @var string
*/
protected $fields = [];
/**
* During clause
* @var string
*/
protected $during = [];
/**
* Where Condition
*
* @var string
*/
protected $where='';
/**
* @var string
*/
protected $type = '';
/**
* @var bool
*/
protected $magicSelect = false;
/**
* Reports constructor.
* @param null $session
*/
function __construct($session = null)
{
$this->session = $session ? $session : (new AdwordsSession())->oAuth()->build();
$this->reportDownloader = new ReportDownloader($this->session);
//Set the default format.
$this->format = Format::get('csv');
}
/**
* Set the format for the report.
* @param $format
* @return $this
*/
public function format($format)
{
if(Format::exist($format))
$this->format = Format::get($format);
else
Format::invalidType();
return $this;
}
/**
* Set the fields to retrieve
*
* @param $fields
* @return $this
*/
public function select($fields)
{
$this->fields = is_array($fields) ? $fields : func_get_args();
return $this;
}
/**
* Set the report type
*
* @param $reportType
* @return $this
*/
public function from($reportType)
{
if(!ReportTypes::exist($reportType))
ReportTypes::invalidType();
$this->type = ReportTypes::get($reportType);
return $this;
}
/**
* Set the starting and ending dates for the report.
*
* dates format 'Ymd' => e.g. 20170112,20171020
*
* @param $starting
* @param $ending
* @return $this
* @throws \Edujugon\GoogleAds\Exceptions\Report
*/
public function during($starting,$ending)
{
if(! is_numeric($starting) || ! is_numeric($ending))
throw new \Edujugon\GoogleAds\Exceptions\Report('During clause only accepts the following date format: "Ymd" => e.g. 20170112');
$this->during = [$starting,$ending];
return $this;
}
/**
* Set where condition
*
* @param $condition
* @return $this
*/
public function where($condition)
{
$this->where = ! $this->where ? $condition : $this->where . ' and ' . $condition;
return $this;
}
/**
* Mark magicSelect as true.
*
* @return $this
*/
public function magicSelect()
{
$this->magicSelect = true;
return $this;
}
/**
* Get the report as a SimpleXMLObj
*
* @return \SimpleXMLElement
*/
public function getAsSimpleXMLObj()
{
$this->format(Format::get('xml'));
$this->run();
return simplexml_load_string($this->data->getAsString());
}
/**
* Get the report as an object
*
* @return \Edujugon\GoogleAds\Reports\MyReport
*/
public function getAsObj()
{
$this->format(Format::get('xml'));
$this->run();
return (new MyReport(simplexml_load_string($this->data->getAsString())));
}
/**
* Get report as string
* @return string
*/
public function getAsString()
{
$this->run();
return $this->data->getAsString();
}
/**
* Get report as stream
* @return Stream
*/
public function getStream()
{
$this->run();
return $this->data->getStream();
}
/**
* Save the report in a file.
* @param $filePath
* @param string $format
* @return bool
*/
public function saveToFile($filePath,$format = 'csvforexcel')
{
$this->format(Format::get($format));
$this->run();
$this->data->saveToFile($filePath);
return true;
}
/**
* Run the AWQL
*/
private function run()
{
if($this->magicSelect){
$this->selectAllFields();
$this->magicRun();
}
$query = $this->createQuery();
$this->data = $this->reportDownloader->downloadReportWithAwql(
$query, $this->format);
}
/**
* Magic Run the AWQL
*
* Will try to download the report and if fails,
* It pulls out the conflicted field and try again the download.
*/
private function magicRun()
{
$query = $this->createQuery();
try{
$this->data = $this->reportDownloader->downloadReportWithAwql(
$query, $this->format);
}catch(ApiException $e)
{
$field = $e->getErrors()[0]->getFieldPath();
if(!empty($field)){
$this->except($field);
$this->magicRun();
}else
var_dump($e->getMessage());
}
}
/**
* Set all fields from report type
* @return $this
*/
private function selectAllFields()
{
$this->fields = (new Fields())->of($this->type)->asList();
return $this;
}
/**
* Pull fields out of fields list.
*
* @param $excepts
* @return $this
*/
public function except($excepts)
{
$excepts = is_array($excepts) ? $excepts : func_get_args();
$this->fields = array_filter($this->fields,function($field) use($excepts){
return !in_array($field,$excepts);
});
return $this;
}
/**
* @return string
*/
private function createQuery()
{
$query = '';
if(!empty($this->fields))
$query = 'SELECT ' . $this->querify($this->fields);
if(!empty($this->type))
$query .= ' FROM ' . $this->type;
if(!empty($this->where))
$query .= ' WHERE ' . $this->where;
if(!empty($this->during))
$query .= ' DURING ' . $this->querify($this->during);
$this->query = $query;
return $query;
}
/**
* @param array $data
* @return string
*/
private function querify(array $data)
{
return implode(',',$data);
}
//////////////////////
// GETTERS
/////////////////////
/**
* @return array
*/
public function getTypes()
{
return ReportTypes::getList();
}
/**
* @return array
*/
public function getFields()
{
return (new Fields($this->session))->of($this->type)->asList();
}
/**
* Get a list of the report formats.
*
* @return array
*/
public function getFormats()
{
return Format::getList();
}
/**
* @param $name
* @return
*/
function __get($name)
{
if(property_exists(__CLASS__,$name))
return $this->$name;
}
}