src/Command/Apidae/ApidaeImportUpdateCommand.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Command\Apidae;
  3. use App\Constant\ApidaeConstants;
  4. use App\Event\ApidaeEvent;
  5. use App\Factory\ApidaeAccommodationFactory;
  6. use App\Factory\ApidaeActivityFactory;
  7. use App\Factory\ApidaeExperienceFactory;
  8. use App\Factory\ApidaeHostFactory;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Sonata\MediaBundle\Command\BaseCommand;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Style\SymfonyStyle;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Sonata\MediaBundle\Provider\ImageProvider;
  17. class ApidaeImportUpdateCommand extends BaseCommand
  18. {
  19.     private $apidaeDir;
  20.     private $updateDir;
  21.     private $eventDispatcher;
  22.     private $entityManager;
  23.     private $apidaeAccommodationFactory;
  24.     private $apidaeHostFactory;
  25.     private $apidaeActivityFactory;
  26.     private $apidaeSelectionFactory;
  27.     private $sonataImageProvider;
  28.     public function __construct(
  29.         string $apidaeDir,
  30.         EventDispatcherInterface $eventDispatcher,
  31.         EntityManagerInterface $entityManager,
  32.         ApidaeAccommodationFactory $apidaeAccommodationFactory,
  33.         ApidaeHostFactory $apidaeHostFactory,
  34.         ApidaeActivityFactory $apidaeActivityFactory,
  35.         ApidaeExperienceFactory $apidaeSelectionFactory,
  36.         ImageProvider $sonataImageProvider
  37.     ) {
  38.         parent::__construct('isere:apidae:update');
  39.         $this->setDescription('read and save imported APIDAE data');
  40.         $this->apidaeDir                  $apidaeDir;
  41.         $this->updateDir                  $this->apidaeDir.'/todo/';
  42.         $this->eventDispatcher            $eventDispatcher;
  43.         $this->entityManager              $entityManager;
  44.         $this->apidaeAccommodationFactory $apidaeAccommodationFactory;
  45.         $this->apidaeHostFactory          $apidaeHostFactory;
  46.         $this->apidaeActivityFactory      $apidaeActivityFactory;
  47.         $this->apidaeSelectionFactory     $apidaeSelectionFactory;
  48.         $this->sonataImageProvider        $sonataImageProvider;
  49.     }
  50.     protected function execute(InputInterface $inputOutputInterface $output)
  51.     {
  52.         //sleep(300);
  53.         $io = new SymfonyStyle($input$output);
  54.         $io->text("Apidae Update begins...");
  55. //        try {
  56.         $selections json_decode(file_get_contents($this->updateDir.'/selections.json'));
  57.         foreach ($selections as $selection) {
  58.             if (isset($selection->objetsTouristiques)) {
  59.                 foreach ($selection->objetsTouristiques as $touristicObject) {
  60.                     if (file_exists(
  61.                         $this->updateDir '/objets_modifies/objets_modifies-' $touristicObject->id '.json'
  62.                     )) {
  63.                         $data json_decode(
  64.                             file_get_contents(
  65.                                 $this->updateDir '/objets_modifies/objets_modifies-' $touristicObject->id '.json'
  66.                             )
  67.                         );
  68.                         if (in_array($touristicObject->typeApidaeConstants::ARRAY_ACCOMODATION_TYPES)) {
  69.                             $accommodation $this->apidaeAccommodationFactory->load($data);
  70.                             $exp[$selection->id]['accommodationId'] = $touristicObject->id;
  71.                             $this->entityManager->persist($accommodation);
  72.                             $this->entityManager->flush();
  73.                         } elseif (in_array($touristicObject->typeApidaeConstants::ARRAY_HOST_TYPES)) {
  74.                             $host $this->apidaeHostFactory->load($data);
  75.                             $this->entityManager->persist($host);
  76.                             $this->entityManager->flush();
  77.                         } elseif (in_array($touristicObject->typeApidaeConstants::ARRAY_POI_TYPES)) {
  78.                             $activity $this->apidaeActivityFactory->loadWithMediaManager(
  79.                                 $data,
  80.                                 $this->getMediaManager()
  81.                             );
  82.                             $exp[$selection->id]['activityIds'][] = $touristicObject->id;
  83.                             $this->entityManager->persist($activity);
  84.                             $this->entityManager->flush();
  85.                             // This line is mandatory, to avoid memory leak
  86.                             $this->sonataImageProvider->getFilesystem()->clearFileRegister();
  87.                         } else {
  88.                             $this->eventDispatcher->dispatch(
  89.                                 ApidaeEvent::APIDAE_UPDATE_ERROR,
  90.                                 new ApidaeEvent(sprintf("Touristic Object type not found : %s "$touristicObject->type), 1)
  91.                             );
  92.                         }
  93.                         $io->text(
  94.                             sprintf("Apidae Selection #%s Object #%s updated"$selection->id$touristicObject->id)
  95.                         );
  96.                     }
  97.                 }
  98.                 $selectionEntity $this->apidaeSelectionFactory->load($selection);
  99.                 $this->entityManager->persist($selectionEntity);
  100.                 $this->entityManager->flush();
  101.                 $this->entityManager->clear();
  102.             }
  103.         }
  104. //        } catch (\Exception $e) {
  105. //            throw new ApidaeUpdateException($this->eventDispatcher, $e->getMessage(), $e->getCode());
  106. //        }
  107.         $this->eventDispatcher->dispatch(
  108.             ApidaeEvent::APIDAE_UPDATE_INFO,
  109.             new ApidaeEvent('Selection Update succeed'0)
  110.         );
  111.         $io->success('Update succeeded.');
  112.     }
  113. }