<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Driver\Connection;
use App\Service\CommonService;
use App\Service\D4SeoService;
use Psr\Log\LoggerInterface;
use App\Repository\Seo\SEOTasksRepository;
use App\Entity\Seo\SEOTasks;
use App\Repository\Fp\FPTasksRepository;
use App\Entity\Fp\FPTasks;
use App\Repository\Seo\IntelQueueRepository;
use App\Entity\Seo\IntelQueue;
use App\Repository\Seo\GPlacesRepository;
class DiscoveryService
{
public function __construct(IntelQueueRepository $intelQueueRepository, EntityManagerInterface $em,
LoggerInterface $logger, LoggerInterface $seoinfoLogger, SEOTasksRepository $seoTasksRepository, GPlacesRepository $gPlacesRepository,
D4SeoService $d4SeoService)
{
$this->intelQueueRepository = $intelQueueRepository;
$this->seoTasksRepository = $seoTasksRepository;
$this->gPlacesRepository = $gPlacesRepository;
$this->seologger = $seoinfoLogger;
// FOR ERROR and CRITICAL
// $this->seologger->error("COME INFORMATION HERE - ".__FUNCTION__." ".__LINE__);
// FOR FLOW INFORMAITON
// $this->seologger->info("WHAT WERE DOING - ".__FUNCTION__."->businessDataStoreG");
$this->logger = $logger;
$this->d4seo = $d4SeoService;
$this->entityManager = $em;
}
public function lostSeoTasks() {
$this->d4seo->retrieveOrphanTasks();
}
/**
* Finds all intel tasks with designated purpose
*/
public function findAllIntelTasks($purpose = '') {
$rows = $this->intelQueueRepository->findAllPurpose($purpose);
// Build a simple object or array from the returned rows
foreach($rows as $row) {
$prepped[] = array($row['id'], $row['actionapi'], $row['function'], $row['content'],
$row['more'], $row['created']->format('Y-m-d H:i:s') );
}
$data['data'] = $prepped;
return $data;
}
/**
* Finds all gPlaces here
*/
public function findAllGPlaces() {
$rows = $this->gPlacesRepository->findAllMost();
// Build a simple object or array from the returned rows
foreach($rows as $row) {
$prepped[] = array($row['id'], $row['cid'], $row['title'],
$row['address'], $row['phone'], $row['url'], $row['rating_value'] );
}
$data['data'] = $prepped;
return $data;
}
/**
* Finds all gsapiPlaces here
*/
public function findGeoGsapi($lat, $lng, $rad) {
$rows = $this->gPlacesRepository->findByGeo($lat, $lng, $rad);
// Build a simple object or array from the returned rows
foreach($rows as $row) {
$prepped[] = array($row['b_id'], $row['distance'], $row['b_cid'], $row['b_title'],
$row['b_address'], $row['b_phone'], $row['b_url'], $row['b_rating_value'] );
}
$data['data'] = $prepped;
return $data;
}
/**
* Gets 10 tasks from intel for use in cron job. Runs the tasks, returns to cron. Defaults to discovery.
*/
public function getTenIntel($purpose = 'discovery', $function = 'my_business_info') {
$rows = $this->intelQueueRepository->distinctTen($purpose,$function);
$em = $this->entityManager;
//dd($rows);
foreach ($rows as $row) {
if($row['actionapi'] == 'business_data' && $row['function'] == 'my_business_info') {
// Get Google My Business information for specific entry uses
// api:business_data function:my_business_info se:google
if(is_array($row['more'])) {
$postinfo = (object) $row['more'];
} else {
$postinfo = (object) json_decode($row['more']);
}
$postinfo->priority = 1;
if($purpose == 'footprint') {
// This intel is for use with footprints. We'll do some stuff with it later.
$task = $em->getRepository(FPTasks::class)->findOneBy(array('keyword' => $postinfo->keyword, 'status' => 'sent'));
} else {
// We're working with GMB Business Info - Check if we already obtained a CID through earlier
// discovery. If so we search by cid with cid: prefix not with the keyword/title.
if(isset($postinfo->cid)) {
$postinfo->keyword = 'cid:'.$postinfo->cid;
}
if(isset($postinfo->place_id)) {
$postinfo->keyword = 'place_id:'.$postinfo->place_id;
}
$postinfo->purpose = $row['purpose'];
$task = $em->getRepository(SEOTasks::class)->findOneBy(array('keyword' => $postinfo->keyword, 'status' => 'sent'));
$this->seologger->info("Investigating {$postinfo->keyword} for {$purpose} - ".__FUNCTION__." ".__LINE__);
}
if (!$task) {
$taskinfo = $this->d4seo->businessData($postinfo);
//dd($taskinfo);
if($taskinfo->status == 'sent') {
// If a success sending, store the task.
$this->d4seo->storeTask($taskinfo);
$this->seologger->info("No existing task. Stored task for '{$row['content']}' {$taskinfo->task_id} - ".__FUNCTION__." ".__LINE__);
}
// Now remove the item from the Intel Queue.
$task = $em->getRepository(IntelQueue::class)->findOneBy(array('id' => $row['id']));
//dd($task);
$em->remove($task);
$em->flush();
// Sleep a bit to lessen the load to the D4 api. Low priority here.
sleep(2);
}
}
}
return $rows;
}
}
?>