Completed
Push — master ( 388ae8...d9e930 )
by Felix
09:31
created

createAuthenticatedAdWordsSessionBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
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\v201705\o\TargetingIdeaService;
9
use Google\AdsApi\Common\AdsLoggerFactory;
10
use Google\AdsApi\Common\OAuth2TokenBuilder;
11
12
class AdWordsServiceFactory
13
{
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
                array_get($config, 'soap_log_file_path', null),
47
                array_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