1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Google Analytics Report |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\ga_report\models; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Cache, |
13
|
|
|
gplcart\core\Hook; |
14
|
|
|
use gplcart\core\models\Oauth as OauthModel, |
15
|
|
|
gplcart\core\models\Translation as TranslationModel; |
16
|
|
|
use gplcart\core\exceptions\OauthAuthorization as OauthAuthorizationException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manages basic behaviors and data related to Google Analytics Report |
20
|
|
|
*/ |
21
|
|
|
class Report |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Hook class instance |
26
|
|
|
* @var \gplcart\core\Hook $hook |
27
|
|
|
*/ |
28
|
|
|
protected $hook; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Translation UI model instance |
32
|
|
|
* @var \gplcart\core\models\Translation $translation |
33
|
|
|
*/ |
34
|
|
|
protected $translation; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Oauth model instance |
38
|
|
|
* @var \gplcart\core\models\Oauth $oauth |
39
|
|
|
*/ |
40
|
|
|
protected $oauth; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Cache class instance |
44
|
|
|
* @var \gplcart\core\Cache $cache |
45
|
|
|
*/ |
46
|
|
|
protected $cache; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Hook $hook |
50
|
|
|
* @param Cache $cache |
51
|
|
|
* @param OauthModel $oauth |
52
|
|
|
* @param TranslationModel $translation |
53
|
|
|
*/ |
54
|
|
|
public function __construct(Hook $hook, Cache $cache, OauthModel $oauth, TranslationModel $translation) |
55
|
|
|
{ |
56
|
|
|
$this->hook = $hook; |
57
|
|
|
$this->cache = $cache; |
58
|
|
|
$this->oauth = $oauth; |
59
|
|
|
$this->translation = $translation; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns reporting data |
64
|
|
|
* @param string $handler_id |
65
|
|
|
* @param string|array $data |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function get($handler_id, $data) |
69
|
|
|
{ |
70
|
|
|
$data += array( |
71
|
|
|
'cache' => 0, |
72
|
|
|
'store_id' => 1, |
73
|
|
|
'query' => array() |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$handler = $this->getHandler($handler_id); |
77
|
|
|
|
78
|
|
|
if (empty($handler)) { |
79
|
|
|
return array('error' => $this->translation->text('Invalid Oauth provider')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$cache_key = "ga_report.$handler_id.{$data['store_id']}"; |
83
|
|
|
|
84
|
|
|
$provider = $this->oauth->getProvider('ga'); |
85
|
|
|
$report = array('handler' => $handler, 'provider' => $provider); |
86
|
|
|
$cache = $this->cache->get($cache_key, array('lifespan' => $data['cache'])); |
87
|
|
|
|
88
|
|
|
if (!empty($data['cache']) && isset($cache)) { |
89
|
|
|
return $report + array( |
90
|
|
|
'data' => $cache, |
91
|
|
|
'updated' => $this->cache->getFileMtime() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$token = $this->oauth->exchangeTokenServer($provider, $provider['settings']); |
97
|
|
|
} catch (OauthAuthorizationException $ex) { |
|
|
|
|
98
|
|
|
return $report + array('error' => $ex->getMessage()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (empty($token['access_token'])) { |
102
|
|
|
return $report + array('error' => $this->translation->text('Failed to get access token')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$data['query']['access_token'] = $token['access_token']; |
106
|
|
|
$results = $this->oauth->process($provider, array_merge($handler['query'], $data['query'])); |
107
|
|
|
|
108
|
|
|
if (isset($results['error']['message'])) { |
109
|
|
|
return $report + array('error' => $results['error']['message']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$report += array( |
113
|
|
|
'data' => $results, |
114
|
|
|
'updated' => GC_TIME |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$this->cache->set($cache_key, $results); |
118
|
|
|
return $report; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Clear cached report data |
123
|
|
|
* @param string|null $handler_id |
124
|
|
|
* @param integer|string|null $store_id |
125
|
|
|
*/ |
126
|
|
|
public function clearCache($handler_id = null, $store_id = null) |
127
|
|
|
{ |
128
|
|
|
$pattern = 'ga_report.'; |
129
|
|
|
|
130
|
|
|
if (isset($handler_id)) { |
131
|
|
|
$pattern .= "$handler_id."; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (isset($store_id)) { |
135
|
|
|
$pattern .= "$store_id"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->cache->clear('', array('pattern' => "$pattern*")); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns a handler data |
143
|
|
|
* @param string $handler_id |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getHandler($handler_id) |
147
|
|
|
{ |
148
|
|
|
$handlers = $this->getHandlers(); |
149
|
|
|
return empty($handlers[$handler_id]) ? array() : $handlers[$handler_id]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns an array of handlers |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getHandlers() |
157
|
|
|
{ |
158
|
|
|
$handlers = &gplcart_static(__METHOD__); |
159
|
|
|
|
160
|
|
|
if (isset($handlers)) { |
161
|
|
|
return $handlers; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$handlers = $this->getDefaultHandlers(); |
165
|
|
|
|
166
|
|
|
$default = array( |
167
|
|
|
'end-date' => date('Y-m-d'), |
168
|
|
|
'start-date' => date('Y-m-d', strtotime('-1 month')) |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
foreach ($handlers as $id => &$handler) { |
172
|
|
|
$handler += array('template' => "ga_report|panels/$id"); |
173
|
|
|
$handler['query'] += $default; |
174
|
|
|
$handler['id'] = $id; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->hook->attach('module.ga.report.handlers', $handlers); |
178
|
|
|
return $handlers; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns an array of default handlers |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
protected function getDefaultHandlers() |
186
|
|
|
{ |
187
|
|
|
$handlers = gplcart_config_get(__DIR__ . '/../config/handlers.php'); |
188
|
|
|
foreach ($handlers as &$handler) { |
189
|
|
|
$handler['name'] = $this->translation->text($handler['name']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $handlers; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.