Total Complexity | 41 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GoogleAnalyticsReportService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GoogleAnalyticsReportService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class GoogleAnalyticsReportService |
||
1 ignored issue
–
show
|
|||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var GoogleClientService |
||
13 | */ |
||
14 | protected $client; |
||
15 | |||
16 | /** |
||
17 | * @var Google_Service_AnalyticsReporting_DateRange |
||
18 | */ |
||
19 | protected $dateRange; |
||
20 | |||
21 | /** |
||
22 | * @var Google_Service_AnalyticsReporting |
||
23 | */ |
||
24 | protected $analytics; |
||
25 | |||
26 | /** |
||
27 | * @var Google_Service_AnalyticsReporting_Metric |
||
28 | */ |
||
29 | protected $metrics; |
||
30 | |||
31 | public $batched = false; |
||
32 | |||
33 | /** |
||
34 | * GoogleReportService constructor. |
||
35 | * @param GoogleClientService $client |
||
36 | */ |
||
37 | public function __construct($client) |
||
38 | { |
||
39 | $config = SiteConfig::current_site_config(); |
||
40 | $this->client = $client; |
||
41 | $this->setDateRange($config->DateRange); |
||
42 | $this->setAnalytics(new Google_Service_AnalyticsReporting($client->getClient())); |
||
43 | $this->setMetrics($config->Metric); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function getReport() |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function getClient() |
||
69 | { |
||
70 | return $this->client; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param mixed $client |
||
75 | */ |
||
76 | public function setClient($client) |
||
77 | { |
||
78 | $this->client = $client; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return Google_Service_AnalyticsReporting_DateRange |
||
83 | */ |
||
84 | public function getDateRange() |
||
85 | { |
||
86 | return $this->dateRange; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param int $dateRange |
||
91 | */ |
||
92 | public function setDateRange($dateRange) |
||
93 | { |
||
94 | $analyticsRange = new Google_Service_AnalyticsReporting_DateRange(); |
||
95 | $analyticsRange->setStartDate($dateRange . 'daysAgo'); |
||
96 | $analyticsRange->setEndDate('today'); |
||
97 | $this->dateRange = $analyticsRange; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return Google_Service_AnalyticsReporting |
||
102 | */ |
||
103 | public function getAnalytics() |
||
104 | { |
||
105 | return $this->analytics; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param Google_Service_AnalyticsReporting $analytics |
||
110 | */ |
||
111 | public function setAnalytics($analytics) |
||
112 | { |
||
113 | $this->analytics = $analytics; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function getMetrics() |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param mixed $metrics |
||
126 | */ |
||
127 | public function setMetrics($metrics) |
||
132 | } |
||
133 | |||
134 | public function getDimensionFilters() |
||
147 | } |
||
148 | |||
149 | public function getPageDimensionFilters() |
||
150 | { |
||
151 | $filters = []; |
||
152 | // Operators are not constants, see here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#Operator |
||
153 | $operator = 'ENDS_WITH'; |
||
154 | $pages = $this->getPages(); |
||
155 | |||
156 | foreach ($pages as $page) { |
||
157 | // Home is recorded as just a slash in GA, we need an |
||
158 | // exception for that |
||
159 | if ($page === 'home') { |
||
160 | $page = '/'; |
||
161 | $operator = 'EXACT'; |
||
162 | } |
||
163 | |||
164 | $filters = $this->createFilter($operator, $page, $filters); |
||
165 | // Reset the operator |
||
166 | if ($operator === 'EXACT') { |
||
167 | $operator = 'ENDS_WITH'; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $filters; |
||
172 | } |
||
173 | |||
174 | public function getDimensionFilterClauses() |
||
175 | { |
||
176 | $dimensionFilters = $this->getDimensionFilters(); |
||
177 | $dimensionClauses = new Google_Service_AnalyticsReporting_DimensionFilterClause(); |
||
178 | $dimensionClauses->setFilters($dimensionFilters); |
||
179 | $dimensionClauses->setOperator('OR'); |
||
180 | |||
181 | return $dimensionClauses; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get the pages filtered by optional blacklisting and whitelisting |
||
186 | * Return an array of the URLSegments, to limit memory abuse |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getPages() |
||
190 | { |
||
191 | $blacklist = Config::inst()->get(static::class, 'blacklist'); |
||
192 | $whitelist = Config::inst()->get(static::class, 'whitelist'); |
||
193 | /** @var DataList|Page[] $pages */ |
||
194 | $pages = SiteTree::get(); |
||
195 | if ($blacklist) { |
||
196 | $pages = $this->getBlacklist($blacklist, $pages); |
||
197 | } |
||
198 | if ($whitelist) { |
||
199 | $pages = $this->getWhitelistPages($whitelist, $pages); |
||
200 | } |
||
201 | |||
202 | if ($pages->count() > 20) { |
||
203 | $this->batched = true; |
||
204 | } |
||
205 | |||
206 | // Google limits to 20 reports per complex filter setup |
||
207 | return $pages->limit(20)->column('URLSegment'); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param array $blacklist |
||
212 | * @param DataList $pages |
||
1 ignored issue
–
show
|
|||
213 | * @return DataList |
||
214 | */ |
||
215 | protected function getBlacklist($blacklist, $pages) |
||
216 | { |
||
217 | $ids = []; |
||
218 | foreach ($blacklist as $class) { |
||
219 | $blacklistpages = $class::get(); |
||
220 | $ids = array_merge($ids, $blacklistpages->column('ID')); |
||
221 | } |
||
222 | $pages = $pages->exclude(['ID' => $ids]); |
||
223 | |||
224 | return $pages; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param array $whitelist |
||
229 | * @param DataList $pages |
||
230 | * @return DataList |
||
231 | */ |
||
232 | protected function getWhitelistPages($whitelist, $pages) |
||
233 | { |
||
234 | $ids = []; |
||
235 | foreach ($whitelist as $class) { |
||
236 | $nowDate = date('Y-m-d'); |
||
237 | $whitelistpages = $class::get() |
||
238 | // This needs to be a where because of `IS NULL` |
||
239 | ->where("(`Page`.`LastAnalyticsUpdate` < $nowDate) |
||
240 | OR (`Page`.`LastAnalyticsUpdate` IS NULL)"); |
||
241 | $ids = array_merge($ids, $whitelistpages->column('ID')); |
||
242 | } |
||
243 | $pages = $pages->filter(['ID' => $ids]); |
||
244 | |||
245 | return $pages; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param $operator |
||
250 | * @param $page |
||
251 | * @param $filters |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function createFilter($operator, $page, $filters) |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param array $rows |
||
268 | * @return int |
||
269 | * @throws \ValidationException |
||
270 | */ |
||
271 | public function updateVisits($rows) |
||
272 | { |
||
273 | $count = 0; |
||
274 | foreach ($rows as $row) { |
||
275 | $metrics = $row->getMetrics(); |
||
276 | $values = $metrics[0]->getValues(); |
||
277 | $dimensions = $row->getDimensions(); |
||
278 | $dimensions = explode('/', $dimensions[0]); |
||
279 | $page = $this->findPage($dimensions); |
||
280 | // Only write if a page is found |
||
281 | if ($page) { |
||
282 | $count++; |
||
283 | $this->updatePageVisit($page, $values); |
||
284 | } |
||
285 | } |
||
286 | // If we're not getting any results back, we're out of data from Google. |
||
287 | // Stop the batching process. |
||
288 | if ($count === 0) { |
||
289 | $this->batched = false; |
||
290 | } |
||
291 | return $count; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param array $dimensions |
||
296 | * @return Page |
||
297 | */ |
||
298 | public function findPage($dimensions) |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param Page $page |
||
327 | * @param array $values |
||
328 | */ |
||
329 | protected function updatePageVisit($page, $values) |
||
340 | } |
||
341 | } |
||
342 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.