Completed
Push — master ( 4b341a...658ebd )
by Felix
08:58
created

src/AdWordsServiceFactory.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SchulzeFelix\AdWords;
4
5
use Google\AdsApi\AdWords\AdWordsSession;
6
use Google\AdsApi\AdWords\AdWordsServices;
7
use Google\AdsApi\Common\AdsLoggerFactory;
8
use Google\AdsApi\Common\OAuth2TokenBuilder;
9
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
10
use Google\AdsApi\AdWords\v201710\o\TargetingIdeaService;
11
12
class AdWordsServiceFactory
13
{
14
    private static $DEFAULT_SOAP_LOGGER_CHANNEL = 'AW_SOAP';
15
16
    public static function createForConfig(array $adwordsConfig): AdWordsService
17
    {
18
        $session = self::createAuthenticatedAdWordsSessionBuilder($adwordsConfig);
19
20
        $adWordsServices = new AdWordsServices();
21
        $targetingIdeaService = $adWordsServices->get($session, TargetingIdeaService::class);
22
23
        return self::createTargetingIdeaService($targetingIdeaService);
0 ignored issues
show
$targetingIdeaService of type object<Google\AdsApi\Common\AdsSoapClient> is not a sub-type of object<Google\AdsApi\AdW...o\TargetingIdeaService>. It seems like you assume a child class of the class Google\AdsApi\Common\AdsSoapClient to be always present.

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.

Loading history...
24
    }
25
26
    /**
27
     * @param array $config
28
     *
29
     * @return AdWordsSession
30
     *
31
     * Generate a refreshable OAuth2 credential for authentication.
32
     * Construct an API session
33
     */
34
    protected static function createAuthenticatedAdWordsSessionBuilder(array $config): AdWordsSession
35
    {
36
        $oAuth2Credential = (new OAuth2TokenBuilder())
37
            ->withClientId($config['client_id'])
38
            ->withClientSecret($config['client_secret'])
39
            ->withRefreshToken($config['client_refresh_token'])
40
            ->build();
41
42
        $soapLogger = (new AdsLoggerFactory())
43
            ->createLogger(
44
                self::$DEFAULT_SOAP_LOGGER_CHANNEL,
45
                array_get($config, 'soap_log_file_path', null),
46
                array_get($config, 'soap_log_level', 'ERROR')
47
            );
48
49
        $session = (new AdWordsSessionBuilder())
50
            ->withOAuth2Credential($oAuth2Credential)
51
            ->withDeveloperToken($config['developer_token'])
52
            ->withUserAgent($config['user_agent'])
53
            ->withClientCustomerId($config['client_customer_id'])
54
            ->withSoapLogger($soapLogger)
55
            ->build();
56
57
        return $session;
58
    }
59
60
    protected static function createTargetingIdeaService(TargetingIdeaService $targetingIdeaService): AdWordsService
61
    {
62
        return new AdWordsService($targetingIdeaService);
63
    }
64
}
65