/
Service.php
197 lines (161 loc) · 3.83 KB
/
Service.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
<?php
/**
* Project: google-ads.
* User: Edujugon
* Email: edujugon@gmail.com
* Date: 9/2/17
* Time: 17:07
*/
namespace Edujugon\GoogleAds\Services;
use Edujugon\GoogleAds\Exceptions\Session;
use Edujugon\GoogleAds\Session\AdwordsSession;
use Google\AdsApi\AdWords\AdWordsServices;
class Service
{
/**
* Where Condition
*
* @var string
*/
protected $where = '';
/**
* Order By clause
* @var string
*/
protected $orderBy = '';
/**
* Limit clause
* @var string
*/
protected $limit = '';
/**
* @var mixed
*/
protected $session;
/**
* @var AdWordsServices
*/
protected $adWordsServices;
/**
* @var
*/
protected $service;
/**
* @var
*/
protected $fields;
/**
* Service constructor.
* @param Google Service $service
* @param \Google\AdsApi\AdWords\AdWordsSession $session
*/
function __construct($service, \Google\AdsApi\AdWords\AdWordsSession $session = null)
{
$this->adWordsServices = new AdWordsServices();
$this->session = $session ? $session : (new AdwordsSession())->build();
$this->setService($service);
}
/**
* @param $field
* @return $this
*/
public function orderBy($field)
{
$this->orderBy = " ORDER BY $field ";
return $this;
}
/**
* @param int $number
* @param int $offset
* @return $this
*/
public function limit($number, $offset = 0)
{
$this->limit = " LIMIT $offset,$number ";
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 where condition
*
* @param $condition
* @return $this
*/
public function where($condition)
{
$this->where = ! $this->where ? $condition : $this->where . ' AND ' . $condition;
return $this;
}
/**
* Get all items.
*
* @param array $fields
* @return ServiceCollection
* E.g. => Campaign | AdGroup | AdGroupAd | ...
*/
public function get($fields = [])
{
$fields = empty($fields) ? $this->fields : $fields;
$this->haveFields($fields);
$query = $this->createQuery($fields);
return new ServiceCollection($this->getService(), $this->service->query($query)->getEntries());
}
/**
* Get the Google service.
*
* @return mixed
*/
public function getService()
{
return $this->service;
}
/**
* @param $fields
* @return string
*/
private function createQuery($fields)
{
$query = 'SELECT ' . implode(',', $fields);
if(!empty($this->where))
$query .= ' WHERE ' . $this->where;
if(!empty($this->orderBy))
$query .= $this->orderBy;
if(!empty($this->limit))
$query .= $this->limit;
return $query;
}
/**
* @param $fields
* @throws Session
*/
private function haveFields($fields)
{
if (empty($fields))
throw new Session('You have to select some fields before calling get method. To do so, call select($fields) method before.');
}
/**
* Set the service
*
* @param $service
* @return $this
* @throws \Edujugon\GoogleAds\Exceptions\Service
*/
private function setService($service)
{
try {
$this->service = $this->adWordsServices->get($this->session, $service);
} catch (\Exception $e) {
throw new \Edujugon\GoogleAds\Exceptions\Service("The service '$service' is not available. Please pass a valid service");
}
}
}