<?php declare(strict_types=1);
namespace App\Command\Apidae;
use App\Constant\ApidaeConstants;
use App\Event\ApidaeEvent;
use App\Factory\ApidaeAccommodationFactory;
use App\Factory\ApidaeActivityFactory;
use App\Factory\ApidaeExperienceFactory;
use App\Factory\ApidaeHostFactory;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\MediaBundle\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Sonata\MediaBundle\Provider\ImageProvider;
class ApidaeImportUpdateCommand extends BaseCommand
{
private $apidaeDir;
private $updateDir;
private $eventDispatcher;
private $entityManager;
private $apidaeAccommodationFactory;
private $apidaeHostFactory;
private $apidaeActivityFactory;
private $apidaeSelectionFactory;
private $sonataImageProvider;
public function __construct(
string $apidaeDir,
EventDispatcherInterface $eventDispatcher,
EntityManagerInterface $entityManager,
ApidaeAccommodationFactory $apidaeAccommodationFactory,
ApidaeHostFactory $apidaeHostFactory,
ApidaeActivityFactory $apidaeActivityFactory,
ApidaeExperienceFactory $apidaeSelectionFactory,
ImageProvider $sonataImageProvider
) {
parent::__construct('isere:apidae:update');
$this->setDescription('read and save imported APIDAE data');
$this->apidaeDir = $apidaeDir;
$this->updateDir = $this->apidaeDir.'/todo/';
$this->eventDispatcher = $eventDispatcher;
$this->entityManager = $entityManager;
$this->apidaeAccommodationFactory = $apidaeAccommodationFactory;
$this->apidaeHostFactory = $apidaeHostFactory;
$this->apidaeActivityFactory = $apidaeActivityFactory;
$this->apidaeSelectionFactory = $apidaeSelectionFactory;
$this->sonataImageProvider = $sonataImageProvider;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
//sleep(300);
$io = new SymfonyStyle($input, $output);
$io->text("Apidae Update begins...");
// try {
$selections = json_decode(file_get_contents($this->updateDir.'/selections.json'));
foreach ($selections as $selection) {
if (isset($selection->objetsTouristiques)) {
foreach ($selection->objetsTouristiques as $touristicObject) {
if (file_exists(
$this->updateDir . '/objets_modifies/objets_modifies-' . $touristicObject->id . '.json'
)) {
$data = json_decode(
file_get_contents(
$this->updateDir . '/objets_modifies/objets_modifies-' . $touristicObject->id . '.json'
)
);
if (in_array($touristicObject->type, ApidaeConstants::ARRAY_ACCOMODATION_TYPES)) {
$accommodation = $this->apidaeAccommodationFactory->load($data);
$exp[$selection->id]['accommodationId'] = $touristicObject->id;
$this->entityManager->persist($accommodation);
$this->entityManager->flush();
} elseif (in_array($touristicObject->type, ApidaeConstants::ARRAY_HOST_TYPES)) {
$host = $this->apidaeHostFactory->load($data);
$this->entityManager->persist($host);
$this->entityManager->flush();
} elseif (in_array($touristicObject->type, ApidaeConstants::ARRAY_POI_TYPES)) {
$activity = $this->apidaeActivityFactory->loadWithMediaManager(
$data,
$this->getMediaManager()
);
$exp[$selection->id]['activityIds'][] = $touristicObject->id;
$this->entityManager->persist($activity);
$this->entityManager->flush();
// This line is mandatory, to avoid memory leak
$this->sonataImageProvider->getFilesystem()->clearFileRegister();
} else {
$this->eventDispatcher->dispatch(
ApidaeEvent::APIDAE_UPDATE_ERROR,
new ApidaeEvent(sprintf("Touristic Object type not found : %s ", $touristicObject->type), 1)
);
}
$io->text(
sprintf("Apidae Selection #%s Object #%s updated", $selection->id, $touristicObject->id)
);
}
}
$selectionEntity = $this->apidaeSelectionFactory->load($selection);
$this->entityManager->persist($selectionEntity);
$this->entityManager->flush();
$this->entityManager->clear();
}
}
// } catch (\Exception $e) {
// throw new ApidaeUpdateException($this->eventDispatcher, $e->getMessage(), $e->getCode());
// }
$this->eventDispatcher->dispatch(
ApidaeEvent::APIDAE_UPDATE_INFO,
new ApidaeEvent('Selection Update succeed', 0)
);
$io->success('Update succeeded.');
}
}