partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

TipOfTheDayService.php (2824B)


      1 <?php
      2 
      3 namespace PartKeepr\TipOfTheDayBundle\Services;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\CronLoggerBundle\Services\CronLoggerService;
      7 use PartKeepr\RemoteFileLoader\RemoteFileLoaderFactory;
      8 use PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay;
      9 use Symfony\Component\DependencyInjection\ContainerInterface;
     10 
     11 class TipOfTheDayService
     12 {
     13     /**
     14      * @var ContainerInterface
     15      */
     16     private $container;
     17 
     18     /**
     19      * @var EntityManager
     20      */
     21     private $entityManager;
     22 
     23     /**
     24      * @var CronLoggerService
     25      */
     26     private $cronLoggerService;
     27 
     28     /**
     29      * @var RemoteFileLoaderFactory
     30      */
     31     private $remoteFileLoader;
     32 
     33     public function __construct(ContainerInterface $container, EntityManager $entityManager, CronLoggerService $cronLoggerService, RemoteFileLoaderFactory $remoteFileLoader)
     34     {
     35         $this->container = $container;
     36         $this->entityManager = $entityManager;
     37         $this->cronLoggerService = $cronLoggerService;
     38         $this->remoteFileLoader = $remoteFileLoader;
     39     }
     40 
     41     /**
     42      * Syncronizes the tip database against the master wiki.
     43      *
     44      * @throws \Exception
     45      */
     46     public function syncTips()
     47     {
     48         $uri = $this->container->getParameter('partkeepr.tip_of_the_day_list');
     49 
     50         $tipsString = $this->remoteFileLoader->createLoader()->load($uri);
     51 
     52         $aPageNames = $this->extractPageNames($tipsString);
     53 
     54         $this->updateTipDatabase($aPageNames);
     55     }
     56 
     57     /**
     58      * Updates the tip database. Expects an array of page names.
     59      *
     60      * This method clears all page names and re-creates them. This saves
     61      * alot of engineering, because we don't need to match contents
     62      * within the database against contents in an array.
     63      *
     64      * @param array $aPageNames The page names as array. Page names are stored as string.
     65      */
     66     private function updateTipDatabase(array $aPageNames)
     67     {
     68         $dql = 'DELETE FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay';
     69         $query = $this->entityManager->createQuery($dql);
     70 
     71         $query->execute();
     72 
     73         foreach ($aPageNames as $pageName) {
     74             $tip = new TipOfTheDay();
     75             $tip->setName($pageName);
     76             $this->entityManager->persist($tip);
     77         }
     78 
     79         $this->entityManager->flush();
     80     }
     81 
     82     /**
     83      * Extracts the page names from the mediawiki JSON returned.
     84      *
     85      * @param string $response The encoded json string
     86      *
     87      * @return array An array with the titles of each page
     88      */
     89     private function extractPageNames($response)
     90     {
     91         $aTipsStructure = json_decode($response, true);
     92         $aTips = $aTipsStructure['query']['categorymembers'];
     93 
     94         $aPageNames = [];
     95 
     96         foreach ($aTips as $tip) {
     97             $aPageNames[] = $tip['title'];
     98         }
     99 
    100         return $aPageNames;
    101     }
    102 }