src/Service/DiscoveryService.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\DBAL\Driver\Connection;
  5. use App\Service\CommonService;
  6. use App\Service\D4SeoService;
  7. use Psr\Log\LoggerInterface;
  8. use App\Repository\Seo\SEOTasksRepository;
  9. use App\Entity\Seo\SEOTasks;
  10. use App\Repository\Fp\FPTasksRepository;
  11. use App\Entity\Fp\FPTasks;
  12. use App\Repository\Seo\IntelQueueRepository;
  13. use App\Entity\Seo\IntelQueue;
  14. use App\Repository\Seo\GPlacesRepository;
  15. class DiscoveryService
  16. {
  17.     public function __construct(IntelQueueRepository $intelQueueRepositoryEntityManagerInterface $em,
  18.         LoggerInterface $loggerLoggerInterface $seoinfoLoggerSEOTasksRepository $seoTasksRepositoryGPlacesRepository $gPlacesRepository
  19.         D4SeoService $d4SeoService
  20.     {
  21.         $this->intelQueueRepository $intelQueueRepository;
  22.         $this->seoTasksRepository $seoTasksRepository;
  23.         $this->gPlacesRepository $gPlacesRepository;
  24.         $this->seologger $seoinfoLogger;
  25.         // FOR ERROR and CRITICAL 
  26.         // $this->seologger->error("COME INFORMATION HERE - ".__FUNCTION__." ".__LINE__);
  27.         // FOR FLOW INFORMAITON
  28.         // $this->seologger->info("WHAT WERE DOING - ".__FUNCTION__."->businessDataStoreG"); 
  29.         $this->logger $logger;
  30.         $this->d4seo $d4SeoService;
  31.         $this->entityManager $em;
  32.     }
  33.     public function lostSeoTasks() {
  34.         $this->d4seo->retrieveOrphanTasks();
  35.     }
  36.     /**
  37.      * Finds all intel tasks with designated purpose
  38.      */
  39.     public function findAllIntelTasks($purpose '') {
  40.         $rows $this->intelQueueRepository->findAllPurpose($purpose);
  41.         // Build a simple object or array from the returned rows
  42.         foreach($rows as $row) {
  43.             $prepped[] = array($row['id'], $row['actionapi'], $row['function'], $row['content'], 
  44.             $row['more'], $row['created']->format('Y-m-d H:i:s') );
  45.         }
  46.         $data['data'] = $prepped;
  47.         
  48.         return $data;
  49.     }
  50.     /**
  51.      * Finds all gPlaces here
  52.      */
  53.     public function findAllGPlaces() {
  54.         $rows $this->gPlacesRepository->findAllMost();
  55.         // Build a simple object or array from the returned rows
  56.         foreach($rows as $row) {
  57.             $prepped[] = array($row['id'], $row['cid'], $row['title'], 
  58.             $row['address'], $row['phone'], $row['url'], $row['rating_value'] );
  59.         }
  60.         $data['data'] = $prepped;
  61.         return $data;
  62.     }
  63.     /**
  64.      * Finds all gsapiPlaces here
  65.      */
  66.     public function findGeoGsapi($lat$lng$rad) {
  67.         $rows $this->gPlacesRepository->findByGeo($lat$lng$rad);
  68.         // Build a simple object or array from the returned rows
  69.         foreach($rows as $row) {
  70.             $prepped[] = array($row['b_id'], $row['distance'], $row['b_cid'], $row['b_title'], 
  71.             $row['b_address'], $row['b_phone'], $row['b_url'], $row['b_rating_value'] );
  72.         }
  73.         $data['data'] = $prepped;
  74.         return $data;
  75.     }
  76.     /**
  77.      * Gets 10 tasks from intel for use in cron job. Runs the tasks, returns to cron. Defaults to discovery.
  78.      */
  79.     public function getTenIntel($purpose 'discovery'$function 'my_business_info') {
  80.         
  81.         $rows $this->intelQueueRepository->distinctTen($purpose,$function);
  82.         $em $this->entityManager;
  83.         //dd($rows);
  84.         foreach ($rows as $row) { 
  85.             if($row['actionapi'] == 'business_data' && $row['function'] == 'my_business_info') {
  86.                 // Get Google My Business information for specific entry uses 
  87.                 // api:business_data function:my_business_info se:google
  88.                 if(is_array($row['more'])) {
  89.                     $postinfo = (object) $row['more'];
  90.                 } else {
  91.                     $postinfo = (object) json_decode($row['more']);
  92.                 }
  93.                 
  94.                 $postinfo->priority 1
  95.                 if($purpose == 'footprint') {
  96.                     // This intel is for use with footprints. We'll do some stuff with it later.
  97.                     $task $em->getRepository(FPTasks::class)->findOneBy(array('keyword' => $postinfo->keyword'status' => 'sent'));
  98.                 } else {
  99.                     // We're working with GMB Business Info - Check if we already obtained a CID through earlier 
  100.                     // discovery. If so we search by cid with cid: prefix not with the keyword/title. 
  101.                     if(isset($postinfo->cid)) {
  102.                         $postinfo->keyword 'cid:'.$postinfo->cid;
  103.                     }
  104.                     if(isset($postinfo->place_id)) {
  105.                         $postinfo->keyword 'place_id:'.$postinfo->place_id;
  106.                     }
  107.                     $postinfo->purpose $row['purpose'];
  108.                     $task $em->getRepository(SEOTasks::class)->findOneBy(array('keyword' => $postinfo->keyword'status' => 'sent'));
  109.                     $this->seologger->info("Investigating {$postinfo->keyword} for {$purpose} - ".__FUNCTION__." ".__LINE__); 
  110.                 }
  111.                 if (!$task) {
  112.                     $taskinfo $this->d4seo->businessData($postinfo);
  113.                     //dd($taskinfo);
  114.                     if($taskinfo->status == 'sent') { 
  115.                         // If a success sending, store the task.
  116.                         $this->d4seo->storeTask($taskinfo);
  117.                         $this->seologger->info("No existing task. Stored task for '{$row['content']}{$taskinfo->task_id} - ".__FUNCTION__." ".__LINE__); 
  118.                     }
  119.                     // Now remove the item from the Intel Queue.
  120.                     $task $em->getRepository(IntelQueue::class)->findOneBy(array('id' => $row['id']));
  121.                     //dd($task);
  122.                     $em->remove($task);
  123.                     $em->flush();
  124.                     // Sleep a bit to lessen the load to the D4 api. Low priority here.
  125.                     sleep(2);
  126.                 }
  127.             }
  128.             
  129.         }
  130.         return $rows;
  131.     }
  132. }
  133. ?>