Passed
Branch 4 (f3d551)
by Simon
03:46
created

AnalyticsUpdateTask::getReport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\GoogleAPI\Tasks;
4
5
use Firesphere\GoogleAPI\Services\GoogleAnalyticsReportService;
6
use Firesphere\GoogleAPI\Services\GoogleClientService;
7
use Firesphere\GoogleAPI\Services\PageUpdateService;
8
use Google_Exception;
9
use LogicException;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Dev\BuildTask;
12
use SilverStripe\ORM\ValidationException;
13
14
class AnalyticsUpdateTask extends BuildTask
15
{
16
    /**
17
     * @return string Title
18
     */
19
    public function getTitle()
20
    {
21
        return 'Update Google Analytics information for the configured pages';
22
    }
23
24
    /**
25
     * Start booting up
26
     * @param HTTPRequest $request
27
     * @throws Google_Exception
28
     * @throws LogicException
29
     */
30
    public function run($request)
31
    {
32
        $clientService = new GoogleClientService();
33
34
        $this->getReport($clientService);
35
    }
36
37
    /**
38
     * Get the report and ask the service to update neatly
39
     *
40
     * @param GoogleClientService $client
41
     * @throws ValidationException
42
     */
43
    protected function getReport($client)
44
    {
45
        $service = new GoogleAnalyticsReportService($client);
46
47
        $reports = $service->getReport();
48
        $count = 0;
49
50
        $updateService = new PageUpdateService();
51
        /** @var array $rows */
52
        foreach ($reports as $report) {
53
            /** @var array $rows */
54
            $rows = $report->getData()->getRows();
55
            $count += $updateService->updateVisits($rows);
56
        }
57
        echo "<p>$count Pages updated with Google Analytics visit count</p>";
58
    }
59
}
60