|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SchulzeFelix\AdWords; |
|
4
|
|
|
|
|
5
|
|
|
use Google\AdsApi\AdWords\AdWordsServices; |
|
6
|
|
|
use Google\AdsApi\AdWords\AdWordsSession; |
|
7
|
|
|
use Google\AdsApi\AdWords\AdWordsSessionBuilder; |
|
8
|
|
|
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaService; |
|
9
|
|
|
use Google\AdsApi\Common\AdsLoggerFactory; |
|
10
|
|
|
use Google\AdsApi\Common\OAuth2TokenBuilder; |
|
11
|
|
|
use Illuminate\Support\Arr; |
|
12
|
|
|
|
|
13
|
|
|
class AdWordsServiceFactory |
|
14
|
|
|
{ |
|
15
|
|
|
private static $DEFAULT_SOAP_LOGGER_CHANNEL = 'AW_SOAP'; |
|
16
|
|
|
|
|
17
|
|
|
public static function createForConfig(array $adwordsConfig): AdWordsService |
|
18
|
|
|
{ |
|
19
|
|
|
$session = self::createAuthenticatedAdWordsSessionBuilder($adwordsConfig); |
|
20
|
|
|
|
|
21
|
|
|
$adWordsServices = new AdWordsServices(); |
|
22
|
|
|
$targetingIdeaService = $adWordsServices->get($session, TargetingIdeaService::class); |
|
23
|
|
|
|
|
24
|
|
|
return self::createTargetingIdeaService($targetingIdeaService); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $config |
|
29
|
|
|
* |
|
30
|
|
|
* @return AdWordsSession |
|
31
|
|
|
* |
|
32
|
|
|
* Generate a refreshable OAuth2 credential for authentication. |
|
33
|
|
|
* Construct an API session |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static function createAuthenticatedAdWordsSessionBuilder(array $config): AdWordsSession |
|
36
|
|
|
{ |
|
37
|
|
|
$oAuth2Credential = (new OAuth2TokenBuilder()) |
|
38
|
|
|
->withClientId($config['client_id']) |
|
39
|
|
|
->withClientSecret($config['client_secret']) |
|
40
|
|
|
->withRefreshToken($config['client_refresh_token']) |
|
41
|
|
|
->build(); |
|
42
|
|
|
|
|
43
|
|
|
$soapLogger = (new AdsLoggerFactory()) |
|
44
|
|
|
->createLogger( |
|
45
|
|
|
self::$DEFAULT_SOAP_LOGGER_CHANNEL, |
|
46
|
|
|
Arr::get($config, 'soap_log_file_path', null), |
|
47
|
|
|
Arr::get($config, 'soap_log_level', 'ERROR') |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$session = (new AdWordsSessionBuilder()) |
|
51
|
|
|
->withOAuth2Credential($oAuth2Credential) |
|
52
|
|
|
->withDeveloperToken($config['developer_token']) |
|
53
|
|
|
->withUserAgent($config['user_agent']) |
|
54
|
|
|
->withClientCustomerId($config['client_customer_id']) |
|
55
|
|
|
->withSoapLogger($soapLogger) |
|
56
|
|
|
->build(); |
|
57
|
|
|
|
|
58
|
|
|
return $session; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected static function createTargetingIdeaService(TargetingIdeaService $targetingIdeaService): AdWordsService |
|
62
|
|
|
{ |
|
63
|
|
|
return new AdWordsService($targetingIdeaService); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.