Commit 65fc5642 authored by Jean-Baptiste Oellers's avatar Jean-Baptiste Oellers
Browse files

Add service

parent 8de3d8a9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -697,6 +697,7 @@ return array(
            \Oscar\Service\DocumentFormatterService::class => \Oscar\Service\DocumentFormatterServiceFactory::class,
            \Oscar\Service\Fixture\FixtureService::class => \Oscar\Service\Fixture\FixtureServiceFactory::class,
            \Oscar\Service\GearmanJobLauncherService::class => \Oscar\Service\GearmanJobLauncherServiceFactory::class,
            \Oscar\Service\ImmobilierLocalService::class => \Oscar\Service\ImmobilierLocalServiceFactory::class,
            \Oscar\Service\JsonFormatterService::class => \Oscar\Service\JsonFormatterServiceFactory::class,
            'Logger' => \Oscar\Service\LoggerServiceFactory::class,
            \Oscar\Service\LoggerService::class => \Oscar\Service\LoggerServiceFactory::class,
+71 −0
Original line number Diff line number Diff line
<?php


namespace Oscar\Command;

use Oscar\Exception\OscarDetailsException;
use Oscar\Service\OscarConfigurationService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class OscarImmobilierLocalImportJsonCommand extends OscarCommandAbstract
{
    protected static $defaultName = 'immobilier-local:import-json';

    protected function configure()
    {
        $this->setDescription("Importer un fichier JSON contenant des informations sur les locaux pour alimenter la table immobilierlocal.")
        ->addOption('fichier', 'f', InputOption::VALUE_REQUIRED, 'Fichier JSON');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->addOutputStyle($output);

        /** @var OscarConfigurationService $configuration */
        $configuration = $this->getServicemanager()->get(OscarConfigurationService::class);

        /** @var \Oscar\Service\ImmobilierLocalService $immobilierLocalService */
        $immobilierLocalService = $this->getServicemanager()->get(\Oscar\Service\ImmobilierLocalService::class);

        /** @var SymfonyStyle $io */
        $io = new SymfonyStyle($input, $output);

        $fichier = $input->getOption('fichier');
        if (!$fichier) {
            $io->error("Veuillez préciser le chemin du fichier JSON à importer (ex: -f locaux.json)");
            return self::FAILURE;
        }
        $realpath = realpath($fichier);
        if (!$realpath) {
            $io->error(sprintf("Chemin du fichier invalide : '%s'", $fichier));
            return self::FAILURE;
        }
        if (!is_file($realpath)) {
            $io->error(sprintf("Le chemin ne correspond pas à un fichier : '%s'", $realpath));
            return self::FAILURE;
        }
        if (!is_readable($realpath)) {
            $io->error(sprintf("Le chemin n'est pas accessible en lecture : '%s'", $realpath));
            return self::FAILURE;
        }

        $fileContents = file_get_contents($realpath);
        $data = json_decode($fileContents, true);

        $logs = [];
        try {
            $logs = $immobilierLocalService->importJson($data);
        } catch (OscarDetailsException $e) {
            $logs = $e->details;
        }
        foreach ($logs as $log) {
            $io->writeln($log);
        }

        return self::SUCCESS;
    }
}
+8 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Exception;

class OscarDetailsException extends OscarException
{
    public $details = array();
}
+27 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Service;

use Oscar\Exception\OscarPCRUException;
use Oscar\Traits\UseEntityManager;
use Oscar\Traits\UseEntityManagerTrait;
use Oscar\Traits\UseLoggerService;
use Oscar\Traits\UseLoggerServiceTrait;
use Oscar\Traits\UseOscarConfigurationService;
use Oscar\Traits\UseOscarConfigurationServiceTrait;
use Throwable;

class ImmobilierLocalService implements UseEntityManager, UseLoggerService, UseOscarConfigurationService
{
  use UseEntityManagerTrait, UseLoggerServiceTrait, UseOscarConfigurationServiceTrait;
  
  public function importJson($data)
  {
    $logs = array();


    
    return $logs;
  }

}
+19 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Service;

use Doctrine\ORM\EntityManager;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

class ImmobilierLocalServiceFactory extends OscarServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $s = new ImmobilierLocalService();
        $s->setOscarConfigurationService($container->get(OscarConfigurationService::class));
        $s->setEntityManager($container->get(EntityManager::class));
        $s->setLoggerService($container->get('Logger'));
        return $s;
    }
}