src/EventSubscriber/FreematicaSubscriber.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Psr\Log\LogLevel;
  4. use App\Entity\Beneficiary;
  5. use Psr\Log\LoggerInterface;
  6. use App\Entity\FreematicaReport;
  7. use App\Entity\PotentialBeneficiary;
  8. use App\Event\Freematica\ErrorObjectEvent;
  9. use App\Event\Freematica\SavedObjectEvent;
  10. use App\Event\Freematica\ObjectExistsEvent;
  11. use App\Event\Freematica\ConnectionErrorEvent;
  12. use App\Event\Freematica\EmptyCollectionEvent;
  13. use App\Event\Freematica\DuplicatedObjectEvent;
  14. use App\Event\Freematica\CreateReportErrorEvent;
  15. use App\Event\Freematica\ImportClientErrorEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class FreematicaSubscriber implements EventSubscriberInterface
  18. {
  19.     // Gender object messages
  20.     public const MALE 'el';
  21.     public const FEMALE 'la';
  22.     // Object names
  23.     public const BENEFICIARY_NAME 'beneficiario';
  24.     public const POTENTIAL_BENEFICIARY_NAME 'beneficiario potencial';
  25.     public const FREEMATICA_REPORT_NAME 'parte';
  26.     public const NAMES = [
  27.         self::BENEFICIARY_NAME,
  28.         self::POTENTIAL_BENEFICIARY_NAME,
  29.         self::FREEMATICA_REPORT_NAME
  30.     ];
  31.     public const PLURALS = [
  32.         self::BENEFICIARY_NAME => 'beneficiarios',
  33.         self::FREEMATICA_REPORT_NAME => 'partes',
  34.     ];
  35.     public const GENDER_NAMES = [
  36.         self::BENEFICIARY_NAME => self::MALE,
  37.         self::POTENTIAL_BENEFICIARY_NAME => self::MALE,
  38.         self::FREEMATICA_REPORT_NAME => self::MALE,
  39.     ];
  40.     // Actions
  41.     public const ERROR 0;
  42.     public const SUCCESS 1;
  43.     public const CREATE_ACTION = [self::ERROR => 'insertar'self::SUCCESS => 'insertado'];
  44.     public const UPDATE_ACTION = [self::ERROR => 'actualizar'self::SUCCESS => 'actualizado'];
  45.     /** @var LoggerInterface $beneficiaryLogger */
  46.     private $beneficiaryLogger;
  47.     /** @var LoggerInterface $beneficiaryLogger */
  48.     private $clientLogger;
  49.     /** @var LoggerInterface $reportLogger */
  50.     private $reportLogger;
  51.     /** @var LoggerInterface $errorLogger */
  52.     private $errorLogger;
  53.     /**
  54.      * FreematicaSubscriber constructor.
  55.      * @param LoggerInterface $freematicaBeneficiaryLogger
  56.      * @param LoggerInterface $freematicaClientLogger
  57.      * @param LoggerInterface $freematicaReportLogger
  58.      * @param LoggerInterface $freematicaErrorLogger
  59.      */
  60.     public function __construct(
  61.         LoggerInterface $freematicaBeneficiaryLogger,
  62.         LoggerInterface $freematicaClientLogger,
  63.         LoggerInterface $freematicaReportLogger,
  64.         LoggerInterface $freematicaErrorLogger
  65.     ) {
  66.         $this->beneficiaryLogger $freematicaBeneficiaryLogger;
  67.         $this->clientLogger $freematicaClientLogger;
  68.         $this->reportLogger $freematicaReportLogger;
  69.         $this->errorLogger $freematicaErrorLogger;
  70.     }
  71.     /**
  72.      * @return array
  73.      */
  74.     public static function getSubscribedEvents(): array
  75.     {
  76.         return [
  77.             ObjectExistsEvent::class => 'onObjectExists',
  78.             DuplicatedObjectEvent::class => 'onDuplicatedObject',
  79.             ErrorObjectEvent::class => 'onErrorObject',
  80.             SavedObjectEvent::class => 'onSavedObject',
  81.             ConnectionErrorEvent::class => 'onConnectionError',
  82.             EmptyCollectionEvent::class => 'onEmptyCollection',
  83.             CreateReportErrorEvent::class => 'onCreateReportError',
  84.             ImportClientErrorEvent::class => 'onImportClientError'
  85.         ];
  86.     }
  87.     /**
  88.      * Dispatch when object exists in our database
  89.      *
  90.      * @param ObjectExistsEvent $event
  91.      */
  92.     public function onObjectExists(ObjectExistsEvent $event)
  93.     {
  94.         $object $event->getObject();
  95.         $code $event->getCode();
  96.         $name $this->getObjectName($object);
  97.         if (!$event->isUpdate()) {
  98.             $message ucfirst($name) . " con código de Freematica $code ya existe";
  99.             $this->writeNotInfoLog($objectLogLevel::WARNING$message);
  100.         }
  101.     }
  102.     /**
  103.      * Dispatch when object is duplicated in Freematica
  104.      *
  105.      * @param DuplicatedObjectEvent $event
  106.      */
  107.     public function onDuplicatedObject(DuplicatedObjectEvent $event)
  108.     {
  109.         $name $event->getName();
  110.         $id $event->getId();
  111.         $identifier $event->getIdentifier();
  112.         $logger $this->getLoggerByName($name);
  113.         $message ucfirst($name) . ' ' $id ' duplicado (' $identifier ')';
  114.         $logger->log(LogLevel::WARNING$message);
  115.     }
  116.     /**
  117.      * @param ErrorObjectEvent $event
  118.      */
  119.     public function onErrorObject(ErrorObjectEvent $event)
  120.     {
  121.         $object $event->getObject();
  122.         $name $this->getObjectName($object);
  123.         $code $event->getCode();
  124.         $exceptionMessage $event->getMessage();
  125.         $action $this->getAction($event->isUpdate());
  126.         $message "No se ha podido " $action[self::ERROR] . $name con código de Freematica " .
  127.             $code ": " $exceptionMessage;
  128.         $this->writeNotInfoLog($objectLogLevel::ERROR$message);
  129.     }
  130.     /**
  131.      * @param SavedObjectEvent $event
  132.      */
  133.     public function onSavedObject(SavedObjectEvent $event)
  134.     {
  135.         $object $event->getObject();
  136.         $code $event->getCode();
  137.         $name $this->getObjectName($object);
  138.         $action $this->getAction($event->isUpdate());
  139.         if ($this->isInfoLoggeableObject($object)) {
  140.             $message "Se ha " $action[self::SUCCESS] . $name " $code;
  141.             $logger $this->getLogger($object);
  142.             $logger->info($message);
  143.         }
  144.     }
  145.     /**
  146.      * @param ConnectionErrorEvent $event
  147.      */
  148.     public function onConnectionError(ConnectionErrorEvent $event)
  149.     {
  150.         $this->errorLogger->error($event->getMessage());
  151.     }
  152.     /**
  153.      * @param EmptyCollectionEvent $event
  154.      */
  155.     public function onEmptyCollection(EmptyCollectionEvent $event)
  156.     {
  157.         if (!$event->getSize()) {
  158.             $logger $this->getLoggerByName($event->getClassName());
  159.             $collection $this->getObjectPluralName($event->getClassName());
  160.             $logger->warning('No hay ' $collection ' para importar');
  161.         }
  162.     }
  163.     /**
  164.      * @param CreateReportErrorEvent $event
  165.      * @return void
  166.      */
  167.     public function onCreateReportError(CreateReportErrorEvent $event)
  168.     {
  169.         $this->reportLogger->warning($event->getErrorMessage());
  170.     }
  171.     /**
  172.      * @param ImportClientErrorEvent $event
  173.      * @return void
  174.      */
  175.     public function onImportClientError(ImportClientErrorEvent $event)
  176.     {
  177.         $this->clientLogger->warning($event->getErrorMessage());
  178.     }
  179.     /**
  180.      * @param object $object
  181.      * @return bool
  182.      */
  183.     private function isNotInfoLoggeableObject(object $object): bool
  184.     {
  185.         switch (true) {
  186.             case $object instanceof Beneficiary:
  187.             case $object instanceof PotentialBeneficiary:
  188.             case $object instanceof FreematicaReport:
  189.                 return true;
  190.         }
  191.         return false;
  192.     }
  193.     /**
  194.      * @param object $object
  195.      * @return bool
  196.      */
  197.     private function isInfoLoggeableObject(object $object): bool
  198.     {
  199.         switch (true) {
  200.             case $object instanceof PotentialBeneficiary:
  201.                 return true;
  202.         }
  203.         return false;
  204.     }
  205.     /**
  206.      * @param object $object
  207.      * @return string|null
  208.      */
  209.     private function getObjectName(object $object): ?string
  210.     {
  211.         switch (true) {
  212.             case $object instanceof Beneficiary:
  213.                 return $this->getConcatName(self::BENEFICIARY_NAME);
  214.             case $object instanceof PotentialBeneficiary:
  215.                 return $this->getConcatName(self::POTENTIAL_BENEFICIARY_NAME);
  216.             case $object instanceof FreematicaReport:
  217.                 return $this->getConcatName(self::FREEMATICA_REPORT_NAME);
  218.         }
  219.         return null;
  220.     }
  221.     /**
  222.      * @param string $value
  223.      * @return string
  224.      */
  225.     private function getConcatName(string $value): string
  226.     {
  227.         return self::GENDER_NAMES[$value] . ' ' $value;
  228.     }
  229.     /**
  230.      * @param string $className
  231.      * @return string|null
  232.      */
  233.     private function getObjectPluralName(string $className): ?string
  234.     {
  235.         switch ($className) {
  236.             case Beneficiary::class:
  237.                 return $this->getPlural(self::BENEFICIARY_NAME);
  238.             case FreematicaReport::class:
  239.                 return $this->getPlural(self::FREEMATICA_REPORT_NAME);
  240.         }
  241.         return null;
  242.     }
  243.     /**
  244.      * @param string $value
  245.      * @return string
  246.      */
  247.     private function getPlural(string $value): string
  248.     {
  249.         return self::PLURALS[$value];
  250.     }
  251.     /**
  252.      * @param object $object
  253.      * @return LoggerInterface|null
  254.      */
  255.     private function getLogger(object $object): ?LoggerInterface
  256.     {
  257.         switch (true) {
  258.             case $object instanceof Beneficiary:
  259.             case $object instanceof PotentialBeneficiary:
  260.                 return $this->beneficiaryLogger;
  261.             case $object instanceof FreematicaReport:
  262.                 return $this->reportLogger;
  263.         }
  264.         return null;
  265.     }
  266.     /**
  267.      * @param string $name
  268.      * @return LoggerInterface|null
  269.      */
  270.     private function getLoggerByName(string $name): ?LoggerInterface
  271.     {
  272.         switch ($name) {
  273.             case self::BENEFICIARY_NAME:
  274.             case self::POTENTIAL_BENEFICIARY_NAME:
  275.                 return $this->beneficiaryLogger;
  276.             case self::FREEMATICA_REPORT_NAME:
  277.                 return $this->reportLogger;
  278.         }
  279.         return null;
  280.     }
  281.     /**
  282.      * @param object $object
  283.      * @param string $type
  284.      * @param string $message
  285.      */
  286.     private function writeNotInfoLog(object $objectstring $typestring $message): void
  287.     {
  288.         if ($message && $this->isNotInfoLoggeableObject($object)) {
  289.             $logger $this->getLogger($object);
  290.             $logger->log($type$message);
  291.         }
  292.     }
  293.     /**
  294.      * @param bool $update
  295.      * @return string[]
  296.      */
  297.     private function getAction(bool $update): array
  298.     {
  299.         return $update self::UPDATE_ACTION self::CREATE_ACTION;
  300.     }
  301. }