Completed
Push — master ( f0673b...d8d853 )
by Hector
06:46
created

Analytics   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A stats() 0 4 1
D all_stats() 0 44 10
A inAvailableEntities() 0 13 1
A getId() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hborras
5
 * Date: 23/04/16
6
 * Time: 10:47
7
 */
8
9
namespace Hborras\TwitterAdsSDK\TwitterAds;
10
11
12
use Hborras\TwitterAdsSDK\TwitterAds\Analytics\Job;
13
use Hborras\TwitterAdsSDK\TwitterAds\Errors\BadRequest;
14
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AnalyticsFields;
15
16
class Analytics extends Resource
17
{
18
    const ENTITY              = "";
19
    const RESOURCE_STATS      = 'stats/accounts/{account_id}/';
20
    const RESOURCE_STATS_JOBS = 'stats/jobs/accounts/{account_id}/';
21
22
    /**
23
     * Pulls a list of metrics for the current object instance.
24
     * @param $metricGroups
25
     * @param array $params
26
     * @param bool $async
27
     * @return $this
28
     * @throws BadRequest
29
     */
30
    public function stats($metricGroups, $params = [], $async = false)
31
    {
32
        return $this->all_stats([$this->getId()], $metricGroups, $params, $async);
33
    }
34
35
36
    public function all_stats($ids, $metricGroups, $params = [], $async = false)
37
    {
38
        $endTime = isset($params[AnalyticsFields::END_TIME]) ? $params[AnalyticsFields::END_TIME] : new \DateTime('now');
39
        $endTime->setTime($endTime->format('H'), 0, 0);
40
        $startTime = isset($params[AnalyticsFields::START_TIME]) ? $params[AnalyticsFields::START_TIME] : new \DateTime($endTime->format('c') . " - 7 days");
41
        $startTime->setTime($startTime->format('H'), 0, 0);
42
        $granularity = isset($params[AnalyticsFields::GRANULARITY]) ? $params[AnalyticsFields::GRANULARITY] : Enumerations::GRANULARITY_TOTAL;
43
        $placement = isset($params[AnalyticsFields::PLACEMENT]) ? $params[AnalyticsFields::PLACEMENT] : Enumerations::PLACEMENT_ALL_ON_TWITTER;
44
        if (isset($params[AnalyticsFields::ENTITY])) {
45
            $entity = $params[AnalyticsFields::ENTITY];
46
        } else {
47
            throw new BadRequest('Entity parameter is mandatory', 500, []);
0 ignored issues
show
Bug introduced by
The call to BadRequest::__construct() misses a required argument $errors.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array() is of type array, but the function expects a null|object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        }
49
        if (!self::inAvailableEntities($entity)) {
50
            throw new BadRequest('Entity must be one of ACCOUNT,FUNDING_INSTRUMENT,CAMPAIGN,LINE_ITEM,PROMOTED_TWEET,ORGANIC_TWEET', 500, []);
0 ignored issues
show
Bug introduced by
The call to BadRequest::__construct() misses a required argument $errors.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array() is of type array, but the function expects a null|object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
        }
52
        $segmentationType = isset($params[AnalyticsFields::SEGMENTATION_TYPE]) ? $params[AnalyticsFields::SEGMENTATION_TYPE] : null;
53
54
        $params = [
55
            AnalyticsFields::METRIC_GROUPS => implode(",", $metricGroups),
56
            AnalyticsFields::START_TIME => $startTime->format('c'),
57
            AnalyticsFields::END_TIME => $endTime->format('c'),
58
            AnalyticsFields::GRANULARITY => $granularity,
59
            AnalyticsFields::ENTITY => $entity,
60
            AnalyticsFields::ENTITY_IDS => implode(",", $ids),
61
            AnalyticsFields::PLACEMENT => $placement,
62
        ];
63
64
        if (!$async) {
65
            $resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_STATS);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Hborras\TwitterAdsSDK\TwitterAds\Analytics as the method getTwitterAds() does only exist in the following sub-classes of Hborras\TwitterAdsSDK\TwitterAds\Analytics: Hborras\TwitterAdsSDK\TwitterAds\Account. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
66
            $response = $this->getTwitterAds()->get($resource, $params);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Hborras\TwitterAdsSDK\TwitterAds\Analytics as the method getTwitterAds() does only exist in the following sub-classes of Hborras\TwitterAdsSDK\TwitterAds\Analytics: Hborras\TwitterAdsSDK\TwitterAds\Account. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
67
68
            return $response->getBody()->data;
69
        } else {
70
            if (!is_null($segmentationType)) {
71
                $params[AnalyticsFields::SEGMENTATION_TYPE] = $segmentationType;
72
            }
73
74
            $resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_STATS_JOBS);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Hborras\TwitterAdsSDK\TwitterAds\Analytics as the method getTwitterAds() does only exist in the following sub-classes of Hborras\TwitterAdsSDK\TwitterAds\Analytics: Hborras\TwitterAdsSDK\TwitterAds\Account. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
75
            $response = $this->getTwitterAds()->post($resource, $params);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Hborras\TwitterAdsSDK\TwitterAds\Analytics as the method getTwitterAds() does only exist in the following sub-classes of Hborras\TwitterAdsSDK\TwitterAds\Analytics: Hborras\TwitterAdsSDK\TwitterAds\Account. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
76
            $job = new Job();
77
            return $job->fromResponse($response->getBody()->data);
78
        }
79
    }
80
81
    public static function inAvailableEntities($entity)
82
    {
83
        $availableEntities = [
84
            AnalyticsFields::ACCOUNT,
85
            AnalyticsFields::FUNDING_INSTRUMENT,
86
            AnalyticsFields::CAMPAIGN,
87
            AnalyticsFields::LINE_ITEM,
88
            AnalyticsFields::PROMOTED_TWEET,
89
            AnalyticsFields::ORGANIC_TWEET,
90
        ];
91
92
        return in_array($entity, $availableEntities);
93
    }
94
95
    public function getId()
96
    {
97
        return parent::getId();
98
    }
99
}