Commit 309a2da5 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Module Import: Amélioration du logging en mode console (utilisation d'un Logger)

parent be94269b
Loading
Loading
Loading
Loading
Loading
+5
Changes for module/Import/src/Import/Controller/Factory/ImportControllerFactory.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Import\Controller\Factory;

use Application\Service\These\TheseService;
use Doctrine\ORM\EntityManager;
use Import\Controller\ImportController;
use Import\Service\ImportService;
@@ -21,9 +22,13 @@ class ImportControllerFactory
        /** @var ImportService $importService */
        $importService = $parentLocator->get(ImportService::class);

        /** @var TheseService $theseService */
        $theseService = $parentLocator->get('TheseService');

        $controller = new ImportController();
        $controller->setEntityManager($entityManager);
        $controller->setImportService($importService);
        $controller->setTheseService($theseService);

        return $controller;
    }
+46 −9
Changes for module/Import/src/Import/Controller/ImportController.php: 46 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@ use Import\Service\Traits\ImportServiceAwareTrait;
use UnicaenApp\Exception\LogicException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use Zend\Log\Filter\Priority;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

@@ -41,12 +44,12 @@ class ImportController extends AbstractActionController
        $data = $result->fetch();

        $last_time = $data["REQ_END_DATE"];
        $last_number = explode(" ", $data["REQ_RESPONSE"])[0];
        $message = $data["REQ_RESPONSE"];

        return new ViewModel([
            'query'     => $etablissement . ' | ' . $table,
            "last_time" => $last_time,
            "last_number" => $last_number,
            "message"   => $message,
        ]);
    }

@@ -69,8 +72,14 @@ class ImportController extends AbstractActionController

        $queryParams = $this->params()->fromQuery();

        $stream = fopen('php://memory','r+');
        $this->setLoggerStream($stream);

        $this->importService->import($service, $etablissement, $sourceCode, $queryParams);
        $logs = $this->importService->getLogs();

        rewind($stream);
        $logs = stream_get_contents($stream);
        fclose($stream);

        return new ViewModel([
            'service'       => $service,
@@ -90,8 +99,14 @@ class ImportController extends AbstractActionController
    {
        $etablissement = $this->params('etablissement');

        $stream = fopen('php://memory','r+');
        $this->setLoggerStream($stream);

        $this->importService->importAll($etablissement);
        $logs = $this->importService->getLogs();

        rewind($stream);
        $logs = stream_get_contents($stream);
        fclose($stream);

        return new ViewModel([
            'service'       => 'Tous',
@@ -122,7 +137,14 @@ class ImportController extends AbstractActionController
            throw new RuntimeException("Aucune thèse trouvée avec ce source code: " . $sourceCodeThese);
        }

        $logs = $this->importService->updateThese($these);
        $stream = fopen('php://memory','r+');
        $this->setLoggerStream($stream);

        $this->importService->updateThese($these);

        rewind($stream);
        $logs = stream_get_contents($stream);
        fclose($stream);

        return new ViewModel([
            'service'       => "these + dépendances",
@@ -138,10 +160,10 @@ class ImportController extends AbstractActionController
        $etablissement = $this->params('etablissement');
        $sourceCode = $this->params('source_code');

        $this->importService->setDebugLevel(1);
        $this->setLoggerStream('php://output');

        $this->importService->import($service, $etablissement, $sourceCode);

        echo $this->importService->getFormattedLogs() . PHP_EOL;
        echo "Importation des données du service '$service' de l'établissement '$etablissement' effectuée." . PHP_EOL;
    }

@@ -149,10 +171,25 @@ class ImportController extends AbstractActionController
    {
        $etablissement = $this->params('etablissement');

        $this->importService->setDebugLevel(1);
        $this->setLoggerStream('php://output');

        $this->importService->importAll($etablissement);

        echo $this->importService->getFormattedLogs() . PHP_EOL;
        echo "Importation de toutes les données de l'établissement '$etablissement' effectuée." . PHP_EOL;
    }

    /**
     * @param string|resource $stream
     */
    private function setLoggerStream($stream)
    {
        $filter = new Priority(Logger::INFO);

        $writer = new Stream($stream);
        $writer->addFilter($filter);

        /** @var Logger $logger */
        $logger = $this->importService->getLogger();
        $logger->addWriter($writer);
    }
}
+3 −3
Changes for module/Import/src/Import/Exception/CallException.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -40,9 +40,9 @@ class CallException extends RuntimeException
    static public function serverError(ServerException $error)
    {
        $message = "Erreur distante rencontrée par le serveur du WS";
        $previous = null;
        if ($error->hasResponse()) {
            $previous = new RuntimeException($error->getResponse()->getBody());
        $previous = $error;
        if ($error->hasResponse() && $error->getResponse()->getBody()->getSize() > 0) {
            $previous = new RuntimeException($error->getResponse()->getBody(), null, $error);
        }

        return new self($message, null, $previous);
+6 −13
Changes for module/Import/src/Import/Service/CallService.php: 6 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -11,12 +11,15 @@ use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use Import\Exception\CallException;
use Zend\Http\Response;
use Zend\Log\LoggerAwareTrait;

/**
 * Service dédié à l'envoi de requêtes au Web Service.
 */
class CallService
{
    use LoggerAwareTrait;

    /**
     * $config est fourni par la factory et permet l'acces à la config
     *
@@ -40,8 +43,6 @@ class CallService
    protected $proxy;
    protected $verify = true;

    protected $debugLevel = 0;

    /**
     * @var array
     */
@@ -58,17 +59,6 @@ class CallService
        return $this;
    }

    /**
     * @param int $debugLevel
     * @return self
     */
    public function setDebugLevel($debugLevel)
    {
        $this->debugLevel = $debugLevel;

        return $this;
    }

    /**
     * @return \stdClass
     */
@@ -179,7 +169,10 @@ class CallService

        $client = new Client($options);
        try {
            $_debut = microtime(true);
            $response = $client->request('GET', $uri);
            $_fin = microtime(true);
            $this->logger->debug("Interrogation du WS : " . ($_fin - $_debut) . " secondes.");
        } catch (ClientException $e) {
            throw CallException::clientError($e);
        } catch (ServerException $e) {
+4
Changes for module/Import/src/Import/Service/Factory/ImportServiceFactory.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManager;
use Import\Service\FetcherService;
use Import\Service\ImportService;
use Import\Service\SynchroService;
use Zend\Log\Logger;
use Zend\ServiceManager\ServiceLocatorInterface as ContainerInterface;

class ImportServiceFactory
@@ -25,11 +26,14 @@ class ImportServiceFactory
        /** @var EtablissementService $etbalissementService */
        $etbalissementService = $container->get(EtablissementService::class);

        $logger = new Logger();

        $service = new ImportService();
        $service->setFetcherService($fetcherService);
        $service->setEntityManager($entityManager);
        $service->setSynchroService($synchroService);
        $service->setEtablissementService($etbalissementService);
        $service->setLogger($logger);

        return $service;
    }
Loading