Source of file AnalyticsUpdateJob.php
Size: 2,682 Bytes - Last Modified: 2021-12-24T06:47:25+00:00
/var/www/docs.ssmods.com/process/src/code/Jobs/AnalyticsUpdateJob.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 | <?php if (class_exists('AbstractQueuedJob')) { /** * Class AnalyticsUpdateJob * * Job to run in the QueuedJobs */ class AnalyticsUpdateJob extends AbstractQueuedJob { /** * @var GoogleAnalyticsReportService */ protected $service; /** * @var PageUpdateService */ protected $updateService; /** * AnalyticsUpdateJob constructor. * @param array $params */ public function __construct($params = []) { parent::__construct($params); } /** * @return string */ public function getTitle() { return 'Update Google Analytics information for the configured pages'; } /** * Boot up the process * @throws \Google_Exception * @throws \LogicException * @throws \ValidationException */ public function process() { $clientService = new GoogleClientService(); $this->getReport($clientService); $this->isComplete = true; } /** * Execute the whole part * * @param GoogleClientService $client * @throws \ValidationException */ protected function getReport($client) { $this->service = new GoogleAnalyticsReportService($client); /** @var array $reports */ $reports = $this->service->getReport(); $count = 0; $this->updateService = new PageUpdateService(); foreach ($reports as $report) { /** @var array $rows */ $rows = $report->getData()->getRows(); $count += $this->updateService->updateVisits($rows); } $this->addMessage("$count Pages updated with Google Analytics visit count"); } /** * If needed, queue itself with */ public function afterComplete() { if ($this->service->batched && $this->updateService->batched) { /** @var AnalyticsUpdateJob $nextJob */ $nextJob = Injector::inst()->get('AnalyticsUpdateJob'); $nextJob->setJobData(1, 0, false, new stdClass(), ['Batched data from Google']); /** @var QueuedJobService $jobService */ $jobService = Injector::inst()->get(QueuedJobService::class); /* Queue immediately after this job */ $jobService->queueJob($nextJob, date('Y-m-d H:i:s')); } parent::afterComplete(); } } } |