GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AnalyticsVideo::cast()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 38
rs 9.568
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace ApiVideo\Client\Api;
5
6
use ApiVideo\Client\Model\Analytic\PlayerSession;
7
use DateTime;
8
use Exception;
9
use InvalidArgumentException;
10
11
class AnalyticsVideo extends BaseApi
12
{
13
    /**
14
     * @param            $videoId
15
     * @param null       $period
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $period is correct as it would always require null to be passed?
Loading history...
16
     * @param array|null $metadata
17
     * @param array      $parameters
18
     * @return PlayerSession[]|null
19
     */
20
    public function search($videoId, $period = null, array $metadata = null, array $parameters = array())
21
    {
22
        if (null !== $period) {
0 ignored issues
show
introduced by
The condition null !== $period is always false.
Loading history...
23
            $parameters['period'] = $period;
24
        }
25
26
        if (null !== $metadata) {
27
            if (!is_array($metadata)) {
0 ignored issues
show
introduced by
The condition is_array($metadata) is always true.
Loading history...
28
                throw new InvalidArgumentException('Metadata argument must be an array');
29
            }
30
31
            $parameters['metadata'] = $metadata;
32
        }
33
34
        $parameters['currentPage'] = isset($parameters['currentPage']) ? $parameters['currentPage'] : 1;
35
        $parameters['pageSize'] = isset($parameters['pageSize']) ? $parameters['pageSize'] : 100;
36
        $items           = array();
37
38
        do {
39
            $response = $this->browser->get("/analytics/videos/$videoId?".http_build_query($parameters));
40
41
            if (!$response->isSuccessful()) {
42
                $this->registerLastError($response);
43
44
                return null;
45
            }
46
47
            $json = json_decode($response->getContent(), true);
48
            $analytics = $json['data'];
49
            $items[] = $this->castAll($analytics);
50
51
            if (isset($parameters['currentPage'])) {
52
                break;
53
            }
54
55
            $pagination = $json['pagination'];
56
            $pagination['currentPage']++;
57
        } while ($pagination['pagesTotal'] >= $pagination['currentPage']);
58
59
        $items = call_user_func_array('array_merge', $items);
60
61
        return $items;
62
    }
63
64
    /**
65
     * @param array $object
66
     * @return PlayerSession
67
     * @throws Exception
68
     */
69
    protected function cast(array $object)
70
    {
71
        $analyticData = new PlayerSession();
72
73
        // Build Analytic Session
74
        $analyticData->session->sessionId = $object['session']['sessionId'];
75
        $analyticData->session->loadedAt = new DateTime($object['session']['loadedAt']);
76
        $analyticData->session->endedAt = new DateTime($object['session']['endedAt']);
77
        if (isset($object['session']['metadata'])) {
78
            $analyticData->session->metadata = $object['session']['metadata'];
79
        }
80
81
        // Build Analytic Location
82
        $analyticData->location->country = $object['location']['country'];
83
        $analyticData->location->city = $object['location']['city'];
84
85
        // Build Analytic Referrer
86
        $analyticData->referrer->url = $object['referrer']['url'];
87
        $analyticData->referrer->medium = $object['referrer']['medium'];
88
        $analyticData->referrer->source = $object['referrer']['source'];
89
        $analyticData->referrer->searchTerm = $object['referrer']['searchTerm'];
90
91
        // Build Analytic Device
92
        $analyticData->device->type = $object['device']['type'];
93
        $analyticData->device->vendor = $object['device']['vendor'];
94
        $analyticData->device->model = $object['device']['model'];
95
96
        // Build Analytic Os
97
        $analyticData->os->name = $object['os']['name'];
98
        $analyticData->os->shortname = $object['os']['shortname'];
99
        $analyticData->os->version = $object['os']['version'];
100
101
        // Build Analytic Client
102
        $analyticData->client->type = $object['client']['type'];
103
        $analyticData->client->name = $object['client']['name'];
104
        $analyticData->client->version = $object['client']['version'];
105
106
        return $analyticData;
107
    }
108
}
109