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

AnalyticsUpdateJob::afterComplete()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\GoogleAPI\Jobs;
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 SilverStripe\Core\Injector\Injector;
10
use stdClass;
11
use SilverStripe\ORM\ValidationException;
12
13
if (class_exists('AbstractQueuedJob')) {
14
    /**
15
     * Class AnalyticsUpdateJob
16
     *
17
     * Job to run in the QueuedJobs
18
     */
19
    class AnalyticsUpdateJob extends AbstractQueuedJob
0 ignored issues
show
Bug introduced by
The type Firesphere\GoogleAPI\Jobs\AbstractQueuedJob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
    {
21
        /**
22
         * @var GoogleAnalyticsReportService
23
         */
24
        protected $service;
25
26
        /**
27
         * @var PageUpdateService
28
         */
29
        protected $updateService;
30
31
        /**
32
         * AnalyticsUpdateJob constructor.
33
         * @param array $params
34
         */
35
        public function __construct($params = [])
36
        {
37
            parent::__construct($params);
38
        }
39
40
        /**
41
         * @return string
42
         */
43
        public function getTitle()
44
        {
45
            return 'Update Google Analytics information for the configured pages';
46
        }
47
48
        /**
49
         * Boot up the process
50
         * @throws Google_Exception
51
         * @throws \LogicException
52
         * @throws ValidationException
53
         */
54
        public function process()
55
        {
56
            $clientService = new GoogleClientService();
57
58
            $this->getReport($clientService);
59
        }
60
61
        /**
62
         * Execute the whole part
63
         *
64
         * @param GoogleClientService $client
65
         * @throws ValidationException
66
         */
67
        protected function getReport($client)
68
        {
69
            $this->service = new GoogleAnalyticsReportService($client);
70
71
            /** @var array $reports */
72
            $reports = $this->service->getReport();
73
            $count = 0;
74
75
            $this->updateService = new PageUpdateService();
76
            foreach ($reports as $report) {
77
                /** @var array $rows */
78
                $rows = $report->getData()->getRows();
79
                $count += $this->updateService->updateVisits($rows);
80
            }
81
            $this->addMessage("$count Pages updated with Google Analytics visit count");
82
        }
83
84
        /**
85
         * If needed, queue itself with
86
         */
87
        public function afterComplete()
88
        {
89
            if ($this->service->batched && $this->updateService->batched) {
90
                /** @var AnalyticsUpdateJob $nextJob */
91
                $nextJob = Injector::inst()->get('AnalyticsUpdateJob');
92
                $nextJob->setJobData(1, 0, false, new stdClass(), ['Batched data from Google']);
93
                /** @var QueuedJobService $jobService */
94
                $jobService = Injector::inst()->get(QueuedJobService::class);
0 ignored issues
show
Bug introduced by
The type Firesphere\GoogleAPI\Jobs\QueuedJobService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
95
                /* Queue immediately after this job */
96
                $jobService->queueJob($nextJob, date('Y-m-d H:i:s'));
97
            }
98
            parent::afterComplete();
99
        }
100
    }
101
}
102