AnalyticsUpdateJob::getTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
if (class_exists('AbstractQueuedJob')) {
4
    /**
5
     * Class AnalyticsUpdateJob
6
     *
7
     * Job to run in the QueuedJobs
8
     */
9
    class AnalyticsUpdateJob extends AbstractQueuedJob
0 ignored issues
show
Bug introduced by
The type 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...
10
    {
11
        /**
12
         * @var GoogleAnalyticsReportService
13
         */
14
        protected $service;
15
16
        /**
17
         * @var PageUpdateService
18
         */
19
        protected $updateService;
20
21
        /**
22
         * AnalyticsUpdateJob constructor.
23
         * @param array $params
24
         */
25
        public function __construct($params = [])
26
        {
27
            parent::__construct($params);
28
        }
29
30
        /**
31
         * @return string
32
         */
33
        public function getTitle()
34
        {
35
            return 'Update Google Analytics information for the configured pages';
36
        }
37
38
        /**
39
         * Boot up the process
40
         * @throws \Google_Exception
41
         * @throws \LogicException
42
         * @throws \ValidationException
43
         */
44
        public function process()
45
        {
46
            $clientService = new GoogleClientService();
47
48
            $this->getReport($clientService);
49
50
            $this->isComplete = true;
0 ignored issues
show
Bug Best Practice introduced by
The property isComplete does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        }
52
53
        /**
54
         * Execute the whole part
55
         *
56
         * @param GoogleClientService $client
57
         * @throws \ValidationException
58
         */
59
        protected function getReport($client)
60
        {
61
            $this->service = new GoogleAnalyticsReportService($client);
62
63
            /** @var array $reports */
64
            $reports = $this->service->getReport();
65
            $count = 0;
66
67
            $this->updateService = new PageUpdateService();
68
            foreach ($reports as $report) {
69
                /** @var array $rows */
70
                $rows = $report->getData()->getRows();
71
                $count += $this->updateService->updateVisits($rows);
72
            }
73
            $this->addMessage("$count Pages updated with Google Analytics visit count");
74
        }
75
76
        /**
77
         * If needed, queue itself with
78
         */
79
        public function afterComplete()
80
        {
81
            if ($this->service->batched && $this->updateService->batched) {
82
                /** @var AnalyticsUpdateJob $nextJob */
83
                $nextJob = Injector::inst()->get('AnalyticsUpdateJob');
0 ignored issues
show
Bug introduced by
The type Injector 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...
84
                $nextJob->setJobData(1, 0, false, new stdClass(), ['Batched data from Google']);
85
                /** @var QueuedJobService $jobService */
86
                $jobService = Injector::inst()->get(QueuedJobService::class);
0 ignored issues
show
Bug introduced by
The type 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...
87
                /* Queue immediately after this job */
88
                $jobService->queueJob($nextJob, date('Y-m-d H:i:s'));
89
            }
90
            parent::afterComplete();
91
        }
92
    }
93
}
94