Completed
Push — master ( 44215e...42dd84 )
by Hector
10s
created

Analytics::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hborras\TwitterAdsSDK\TwitterAds;
4
5
use Hborras\TwitterAdsSDK\TwitterAds\Analytics\Job;
6
use Hborras\TwitterAdsSDK\TwitterAds\Errors\BadRequest;
7
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AnalyticsFields;
8
use Hborras\TwitterAdsSDK\TwitterAds\Fields\JobFields;
9
10
class Analytics extends Resource
11
{
12
    const ENTITY              = "";
13
    const RESOURCE_STATS      = 'stats/accounts/{account_id}/';
14
    const RESOURCE_STATS_JOBS = 'stats/jobs/accounts/{account_id}/';
15
16
    /**
17
     * Pulls a list of metrics for the current object instance.
18
     * @param $metricGroups
19
     * @param array $params
20
     * @param bool $async
21
     * @return $this
22
     * @throws BadRequest
23
     */
24
    public function stats($metricGroups, $params = [], $async = false)
25
    {
26
        return $this->all_stats([$this->getId()], $metricGroups, $params, $async);
27
    }
28
29
30
    public function all_stats($ids, $metricGroups, $params = [], $async = false)
31
    {
32
        $endTime = isset($params[AnalyticsFields::END_TIME]) ? $params[AnalyticsFields::END_TIME] : new \DateTime('now');
33
        $endTime->setTime($endTime->format('H'), 0, 0);
34
        $startTime = isset($params[AnalyticsFields::START_TIME]) ? $params[AnalyticsFields::START_TIME] : new \DateTime($endTime->format('c') . " - 7 days");
35
        $startTime->setTime($startTime->format('H'), 0, 0);
36
        $granularity = isset($params[AnalyticsFields::GRANULARITY]) ? $params[AnalyticsFields::GRANULARITY] : Enumerations::GRANULARITY_TOTAL;
37
        $placement = isset($params[AnalyticsFields::PLACEMENT]) ? $params[AnalyticsFields::PLACEMENT] : Enumerations::PLACEMENT_ALL_ON_TWITTER;
38
        if (isset($params[AnalyticsFields::ENTITY])) {
39
            $entity = $params[AnalyticsFields::ENTITY];
40
        } else {
41
            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...
42
        }
43
        if (!self::inAvailableEntities($entity)) {
44
            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...
45
        }
46
        $segmentationType = isset($params[AnalyticsFields::SEGMENTATION_TYPE]) ? $params[AnalyticsFields::SEGMENTATION_TYPE] : null;
47
        $country = isset($params[JobFields::COUNTRY]) ? $params[JobFields::COUNTRY] : null;
48
        $platform = isset($params[JobFields::PLATFORM]) ? $params[JobFields::PLATFORM] : null;
49
50
        $params = [
51
            AnalyticsFields::METRIC_GROUPS => implode(",", $metricGroups),
52
            AnalyticsFields::START_TIME => $startTime->format('c'),
53
            AnalyticsFields::END_TIME => $endTime->format('c'),
54
            AnalyticsFields::GRANULARITY => $granularity,
55
            AnalyticsFields::ENTITY => $entity,
56
            AnalyticsFields::ENTITY_IDS => implode(",", $ids),
57
            AnalyticsFields::PLACEMENT => $placement,
58
        ];
59
60
        if (!$async) {
61
            $resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_STATS);
62
            $response = $this->getTwitterAds()->get($resource, $params);
63
64
            return $response->getBody()->data;
65
        } else {
66
            if (!is_null($segmentationType)) {
67
                $params[AnalyticsFields::SEGMENTATION_TYPE] = $segmentationType;
68
            }
69
70
            if (!is_null($country)) {
71
                $params[JobFields::COUNTRY] = $country;
72
            }
73
74
            if (!is_null($platform)) {
75
                $params[JobFields::PLATFORM] = $platform;
76
            }
77
78
            $resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_STATS_JOBS);
79
            $response = $this->getTwitterAds()->post($resource, $params);
80
            $job = new Job();
81
            return $job->fromResponse($response->getBody()->data);
82
        }
83
    }
84
85
    public static function inAvailableEntities($entity)
86
    {
87
        $availableEntities = [
88
            AnalyticsFields::ACCOUNT,
89
            AnalyticsFields::FUNDING_INSTRUMENT,
90
            AnalyticsFields::CAMPAIGN,
91
            AnalyticsFields::LINE_ITEM,
92
            AnalyticsFields::PROMOTED_TWEET,
93
            AnalyticsFields::ORGANIC_TWEET,
94
        ];
95
96
        return in_array($entity, $availableEntities);
97
    }
98
99
    public function getId()
100
    {
101
        return parent::getId();
102
    }
103
}
104