-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAdWordsServiceFactory.php
65 lines (54 loc) · 2.16 KB
/
AdWordsServiceFactory.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
<?php
namespace SchulzeFelix\AdWords;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaService;
use Google\AdsApi\Common\AdsLoggerFactory;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Illuminate\Support\Arr;
class AdWordsServiceFactory
{
private static $DEFAULT_SOAP_LOGGER_CHANNEL = 'AW_SOAP';
public static function createForConfig(array $adwordsConfig): AdWordsService
{
$session = self::createAuthenticatedAdWordsSessionBuilder($adwordsConfig);
$adWordsServices = new AdWordsServices();
$targetingIdeaService = $adWordsServices->get($session, TargetingIdeaService::class);
return self::createTargetingIdeaService($targetingIdeaService);
}
/**
* @param array $config
*
* @return AdWordsSession
*
* Generate a refreshable OAuth2 credential for authentication.
* Construct an API session
*/
protected static function createAuthenticatedAdWordsSessionBuilder(array $config): AdWordsSession
{
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId($config['client_id'])
->withClientSecret($config['client_secret'])
->withRefreshToken($config['client_refresh_token'])
->build();
$soapLogger = (new AdsLoggerFactory())
->createLogger(
self::$DEFAULT_SOAP_LOGGER_CHANNEL,
Arr::get($config, 'soap_log_file_path', null),
Arr::get($config, 'soap_log_level', 'ERROR')
);
$session = (new AdWordsSessionBuilder())
->withOAuth2Credential($oAuth2Credential)
->withDeveloperToken($config['developer_token'])
->withUserAgent($config['user_agent'])
->withClientCustomerId($config['client_customer_id'])
->withSoapLogger($soapLogger)
->build();
return $session;
}
protected static function createTargetingIdeaService(TargetingIdeaService $targetingIdeaService): AdWordsService
{
return new AdWordsService($targetingIdeaService);
}
}