-
Notifications
You must be signed in to change notification settings - Fork 767
/
AdManagerSessionBuilder.php
456 lines (401 loc) · 12.3 KB
/
AdManagerSessionBuilder.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AdsApi\AdManager;
use Google\AdsApi\Common\AdsBuilder;
use Google\AdsApi\Common\AdsHeaderFormatter;
use Google\AdsApi\Common\AdsLoggerFactory;
use Google\AdsApi\Common\Configuration;
use Google\AdsApi\Common\ConfigurationLoader;
use Google\AdsApi\Common\ConnectionSettings;
use Google\AdsApi\Common\ConnectionSettingsBuilder;
use Google\AdsApi\Common\SoapSettings;
use Google\AdsApi\Common\SoapSettingsBuilder;
use Google\Auth\FetchAuthTokenInterface;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
/**
* Builds Ad Manager sessions.
*
* @see AdManagerSession
*/
final class AdManagerSessionBuilder implements AdsBuilder
{
/**
* @var string the default application name included in our example library
* *.ini files
*/
const DEFAULT_APPLICATION_NAME = 'INSERT_APPLICATION_NAME_HERE';
/**
* @var string DEFAULT_ENDPOINT the default base endpoint for the
* Ad Manager API
*/
const DEFAULT_ENDPOINT = 'https://ads.google.com/';
private static $DEFAULT_SOAP_LOGGER_CHANNEL = 'DFP_SOAP';
private static $DEFAULT_REPORT_DOWNLOADER_LOGGER_CHANNEL =
'DFP_REPORT_DOWNLOADER';
private $adsLoggerFactory;
private $configurationLoader;
private $connectionSettingsBuilder;
private $soapSettingsBuilder;
private $networkCode;
private $applicationName;
private $endpoint;
private $oAuth2Credential;
private $connectionSettings;
private $soapSettings;
private $soapLogger;
private $reportDownloaderLogger;
private $adsHeaderFormatter;
public function __construct()
{
$this->adsLoggerFactory = new AdsLoggerFactory();
$this->configurationLoader = new ConfigurationLoader();
$this->connectionSettingsBuilder = new ConnectionSettingsBuilder();
$this->soapSettingsBuilder = new SoapSettingsBuilder();
}
/**
* Reads configuration settings from the specified filepath. The filepath is
* optional, and if omitted, it will look for the default configuration
* filename in the home directory of the user running PHP.
*
* @see AdsBuilder::DEFAULT_CONFIGURATION_FILENAME
*
* @param string|null $path the filepath
* @return AdManagerSessionBuilder this builder populated from the
* configuration
*
* @throws InvalidArgumentException if the configuration file could not be
* found
*/
public function fromFile(string $path = null)
{
if ($path === null) {
$path = self::DEFAULT_CONFIGURATION_FILENAME;
}
return $this->from($this->configurationLoader->fromFile($path));
}
/**
* @param Configuration $configuration a configuration object that holds the
* same data as an *.ini file would have
* @return AdManagerSessionBuilder this builder populated from the
* configuration
* @see AdsBuilder::from()
*/
public function from(Configuration $configuration)
{
$this->networkCode = $configuration->getConfiguration(
'networkCode',
'AD_MANAGER'
);
$this->applicationName = $configuration->getConfiguration(
'applicationName',
'AD_MANAGER'
);
$this->endpoint = $configuration->getConfiguration(
'endpoint',
'AD_MANAGER'
);
$this->connectionSettings = $this->connectionSettingsBuilder
->from($configuration)->build();
$this->soapSettings = $this->soapSettingsBuilder->from($configuration)
->build();
$this->soapLogger = $this->adsLoggerFactory->createLogger(
self::$DEFAULT_SOAP_LOGGER_CHANNEL,
$configuration->getConfiguration('soapLogFilePath', 'LOGGING'),
$configuration->getConfiguration('soapLogLevel', 'LOGGING')
);
$this->reportDownloaderLogger = $this->adsLoggerFactory->createLogger(
self::$DEFAULT_REPORT_DOWNLOADER_LOGGER_CHANNEL,
$configuration->getConfiguration(
'reportDownloaderLogFilePath',
'LOGGING'
),
$configuration->getConfiguration(
'reportDownloaderLogLevel',
'LOGGING'
)
);
return $this;
}
/**
* Includes network code. This is required.
*
* @param string $networkCode
* @return AdManagerSessionBuilder this builder
*/
public function withNetworkCode(string $networkCode)
{
$this->networkCode = $networkCode;
return $this;
}
/**
* Includes application name. This is required.
*
* @param string $applicationName
* @return AdManagerSessionBuilder this builder
*/
public function withApplicationName(string $applicationName)
{
$this->applicationName = $applicationName;
return $this;
}
/**
* Includes the Ad Manager API server's base endpoint. This is optional.
*
* @see self::DEFAULT_ENDPOINT
* @param string|null $endpoint
* @return AdManagerSessionBuilder this builder
*/
public function withEndpoint(string $endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
/**
* Includes the OAuth2 credential to be used for authentication. This is
* required.
*
* @param FetchAuthTokenInterface $oAuth2Credential
* @return AdManagerSessionBuilder this builder
*/
public function withOAuth2Credential(
FetchAuthTokenInterface $oAuth2Credential
) {
$this->oAuth2Credential = $oAuth2Credential;
return $this;
}
/**
* Includes connection settings. This is optional.
*
* @param ConnectionSettings $connectionSettings
* @return AdManagerSessionBuilder this builder
*/
public function withConnectionSettings(
ConnectionSettings $connectionSettings
) {
$this->connectionSettings = $connectionSettings;
return $this;
}
/**
* Includes SOAP settings. This is optional.
*
* @see SoapSettingsBuilder::defaultOptionals()
* @param SoapSettings $soapSettings
* @return AdManagerSessionBuilder this builder
*/
public function withSoapSettings(SoapSettings $soapSettings)
{
$this->soapSettings = $soapSettings;
return $this;
}
/**
* Includes a PSR-3 compliant logger for logging SOAP calls. This is
* optional.
*
* @param LoggerInterface $soapLogger
* @return AdManagerSessionBuilder this builder
*/
public function withSoapLogger(LoggerInterface $soapLogger)
{
$this->soapLogger = $soapLogger;
return $this;
}
/**
* Includes a PSR-3 compliant logger for the report downloader utility to
* log report download calls. This is optional.
*
* @param LoggerInterface $reportDownloaderLogger
* @return AdManagerSessionBuilder this builder
*/
public function withReportDownloaderLogger(
LoggerInterface $reportDownloaderLogger
) {
$this->reportDownloaderLogger = $reportDownloaderLogger;
return $this;
}
/**
* Includes ads header formatter. This is optional.
*
* @param AdsHeaderFormatter $adsHeaderFormatter
* @return AdManagerSessionBuilder this builder
*/
public function withAdsHeaderFormatter(
AdsHeaderFormatter $adsHeaderFormatter
) {
$this->adsHeaderFormatter = $adsHeaderFormatter;
return $this;
}
/**
* @see AdsBuilder::build()
*/
public function build()
{
$this->defaultOptionals();
$this->validate();
return new AdManagerSession($this);
}
/**
* @see AdsBuilder::defaultOptionals()
*/
public function defaultOptionals()
{
if ($this->endpoint === null) {
$this->endpoint = self::DEFAULT_ENDPOINT;
}
if ($this->connectionSettings === null) {
$this->connectionSettings =
(new ConnectionSettingsBuilder())->build();
}
if ($this->soapSettings === null) {
$this->soapSettings = (new SoapSettingsBuilder())->build();
}
if ($this->soapLogger === null) {
$this->soapLogger = $this->adsLoggerFactory->createLogger(
self::$DEFAULT_SOAP_LOGGER_CHANNEL
);
}
if ($this->reportDownloaderLogger === null) {
$this->reportDownloaderLogger =
$this->adsLoggerFactory->createLogger(
self::$DEFAULT_REPORT_DOWNLOADER_LOGGER_CHANNEL
);
}
if ($this->adsHeaderFormatter === null) {
$this->adsHeaderFormatter = new AdsHeaderFormatter();
}
}
/**
* @see AdsBuilder::validate()
*/
public function validate()
{
if ($this->networkCode === null || trim($this->networkCode) === '') {
// Most of Ad Manager API functions require a network code.
// GetAllNetwork (https://developers.google.com/ad-manager/api/reference/latest/NetworkService#getAllNetworks)
// is the only one function intended to be used without a network
// code.
trigger_error(
"Network code is not specified. An API" .
" function will reject requests if it requires a network code.",
E_USER_WARNING
);
}
if ($this->applicationName === null
|| trim($this->applicationName) === ''
|| strpos(
$this->applicationName,
self::DEFAULT_APPLICATION_NAME
) !== false) {
throw new InvalidArgumentException(
sprintf(
'Application name is required and cannot be the '
. 'default [%s].',
self::DEFAULT_APPLICATION_NAME
)
);
}
if (filter_var($this->endpoint, FILTER_VALIDATE_URL) === false) {
throw new InvalidArgumentException('Endpoint must be a valid URL.');
}
if ($this->oAuth2Credential === null) {
throw new InvalidArgumentException(
'Missing OAuth2 authentication credentials.'
);
}
}
/**
* Gets the network code.
*
* @return string
*/
public function getNetworkCode()
{
return $this->networkCode;
}
/**
* Gets the application name.
*
* @return string
*/
public function getApplicationName()
{
return $this->applicationName;
}
/**
* Gets the endpoint.
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint;
}
/**
* Gets the OAuth2 credential.
*
* @return FetchAuthTokenInterface
*/
public function getOAuth2Credential()
{
return $this->oAuth2Credential;
}
/**
* Gets the connection settings.
*
* @return ConnectionSettings
*/
public function getConnectionSettings()
{
return $this->connectionSettings;
}
/**
* Gets the SOAP settings.
*
* @return SoapSettings
*/
public function getSoapSettings()
{
return $this->soapSettings;
}
/**
* Gets the SOAP logger.
*
* @return LoggerInterface
*/
public function getSoapLogger()
{
return $this->soapLogger;
}
/**
* Gets the report downloader logger.
*
* @return LoggerInterface
*/
public function getReportDownloaderLogger()
{
return $this->reportDownloaderLogger;
}
/**
* Gets ads header formatter.
*
* @return AdsHeaderFormatter
*/
public function getAdsHeaderFormatter()
{
return $this->adsHeaderFormatter;
}
}