Completed
Pull Request — master (#266)
by Alexander
02:46
created

AnalyticsClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setCacheLifeTimeInMinutes() 0 6 1
B performQuery() 0 34 3
A getAnalyticsService() 0 4 1
A determineCacheName() 0 4 1
1
<?php
2
3
namespace Spatie\Analytics;
4
5
use DateTime;
6
use Google_Service_Analytics;
7
use Illuminate\Contracts\Cache\Repository;
8
9
class AnalyticsClient
10
{
11
    /** @var \Google_Service_Analytics */
12
    protected $service;
13
14
    /** @var \Illuminate\Contracts\Cache\Repository */
15
    protected $cache;
16
17
    /** @var int */
18
    protected $cacheLifeTimeInMinutes = 0;
19
20
    public function __construct(Google_Service_Analytics $service, Repository $cache)
21
    {
22
        $this->service = $service;
23
24
        $this->cache = $cache;
25
    }
26
27
    /**
28
     * Set the cache time.
29
     *
30
     * @param int $cacheLifeTimeInMinutes
31
     *
32
     * @return self
33
     */
34
    public function setCacheLifeTimeInMinutes(int $cacheLifeTimeInMinutes)
35
    {
36
        $this->cacheLifeTimeInMinutes = $cacheLifeTimeInMinutes;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Query the Google Analytics Service with given parameters.
43
     *
44
     * @param string    $viewId
45
     * @param \DateTime $startDate
46
     * @param \DateTime $endDate
47
     * @param string    $metrics
48
     * @param array     $others
49
     *
50
     * @return array|null
51
     */
52
    public function performQuery(string $viewId, DateTime $startDate, DateTime $endDate, string $metrics, array $others = [])
53
    {
54
        $cacheName = $this->determineCacheName(func_get_args());
55
56
        if ($this->cacheLifeTimeInMinutes == 0) {
57
            $this->cache->forget($cacheName);
58
        }
59
60
        return $this->cache->remember($cacheName, $this->cacheLifeTimeInMinutes, function () use ($viewId, $startDate, $endDate, $metrics, $others) {
61
            $result = $this->service->data_ga->get(
62
                "ga:{$viewId}",
63
                $startDate->format('Y-m-d'),
64
                $endDate->format('Y-m-d'),
65
                $metrics,
66
                $others
67
            );
68
69
            $nextLink = $result->getNextLink();
70
71
            while ($nextLink) {
72
                $options = [];
73
                /**
74
                 * @source https://stackoverflow.com/a/33526740
75
                 */
76
                parse_str(substr($nextLink, strpos($nextLink, '?') + 1), $options);
77
                $data = $this->service->data_ga->call('get', [$options], "Google_Service_Analytics_GaData");
78
                $result->rows = array_merge($result->rows, $data->rows);
79
80
                $nextLink = $data->getNextLink();
81
            }
82
83
            return $result;
84
        });
85
    }
86
87
    public function getAnalyticsService(): Google_Service_Analytics
88
    {
89
        return $this->service;
90
    }
91
92
    /*
93
     * Determine the cache name for the set of query properties given.
94
     */
95
    protected function determineCacheName(array $properties): string
96
    {
97
        return 'spatie.laravel-analytics.'.md5(serialize($properties));
98
    }
99
}
100