Completed
Push — master ( 0b2f36...19d6a1 )
by Freek
08:35
created

AnalyticsClient::setCacheLifeTimeInMinutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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 ($resource, $viewId, $startDate, $endDate, $metrics, $others) {
0 ignored issues
show
Bug introduced by
The variable $resource does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
62
           return $this->service->data_ga->get(
63
               "ga:{$viewId}",
64
               $startDate->format('Y-m-d'),
65
               $endDate->format('Y-m-d'),
66
               $metrics,
67
               $others
68
           );
69
        });
70
    }
71
72
    public function getAnalyticsService(): Google_Service_Analytics
73
    {
74
        return $this->service;
75
    }
76
77
    /*
78
     * Determine the cache name for the set of query properties given.
79
     */
80
    protected function determineCacheName(array $properties): string
81
    {
82
        return 'spatie.laravel-analytics.'.md5(serialize($properties));
83
    }
84
}
85