src/EventSubscriber/ConnectorSynchronizationSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use App\Connector\DriverRegistry;
  13. use App\Event\ConnectorSynchronization;
  14. use App\Repository\ConnectorRepository;
  15. use Carbon\Carbon;
  16. use DateTime;
  17. use Exception;
  18. use QuickBooksOnline\API\Exception\ServiceException;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use function Sentry\captureException;
  22. class ConnectorSynchronizationSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var ConnectorRepository
  26.      */
  27.     protected $connectorRepository;
  28.     /**
  29.      * @var DriverRegistry
  30.      */
  31.     protected $driverRegistry;
  32.     public function __construct(DriverRegistry $driverRegistryConnectorRepository $connectorRepository)
  33.     {
  34.         $this->connectorRepository $connectorRepository;
  35.         $this->driverRegistry      $driverRegistry;
  36.     }
  37.     public function onSynchronizing(ConnectorSynchronization $event): void
  38.     {
  39.         $connectors $event->isNotEmpty() ?
  40.             $this->connectorRepository->findBy(['id' => $event->getConnectorsIds()]) :
  41.             // The priority for manual synchronization demands (synchronized => 0).
  42.             $this->connectorRepository->findBy([], ['synchronized' => 'ASC']);
  43.         $output $event->getOutput();
  44.         foreach ($connectors as $connector) {
  45.             $driver $this->driverRegistry->get($connector);
  46.             $connectError false;
  47.             try {
  48.                 $loader $driver->connect($connector);
  49.             } catch (ServiceException $serviceException) {
  50.                 if ((int)$serviceException->getCode() !== 400) {
  51.                     captureException($serviceException);
  52.                 }
  53.                 $this->log($output"CANNOT CONNECT TO {$serviceException->getMessage()}");
  54.                 $this->log($output'=============================');
  55.                 // Update connector "connected" flag
  56.                 $connector->setConnected(false);
  57.                 $connector->setLastConnectedCheckDate(new DateTime());
  58.                 continue;
  59.             }
  60.             $connectorInfos $connector->getName() . '@' $connector->getId();
  61.             $this->log($output"CONNECTING TO $connectorInfos");
  62.             if ($connectError || !$loader->isConnected()) {
  63.                 $this->log($output"CANNOT CONNECT TO $connectorInfos");
  64.                 $this->log($output'=============================');
  65.                 // Update connector "connected" flag
  66.                 $connector->setConnected(false);
  67.                 $connector->setLastConnectedCheckDate(new DateTime());
  68.                 continue;
  69.             }
  70.             // Update connector "connected" flag
  71.             $connector->setConnected(true);
  72.             $connector->setLastConnectedCheckDate(new DateTime());
  73.             $this->connectorRepository->save($connector);
  74.             try {
  75.                 // TODO Radhi : Do not parse 3 time each invoice with sellsy : Use a single method to add, update and delete invoices ?
  76.                 $this->log($output'INVOICES CREATE ' $connectorInfos);
  77.                 $loader->createInvoices();
  78.                 $this->log($output'SYNCHRONISED');
  79.                 // todo Radhi : use sleep only in driver after each call (in Loader ?) Maybe need to redefine sleep value. and do not user specific (sellsy) driver value
  80.                 sleep($driver::SLEEP_BETWEEN_OPERATIONS);
  81.                 $this->log($output'INVOICES UPDATE ' $connectorInfos);
  82.                 $loader->updateInvoices();
  83.                 $this->log($output'SYNCHRONISED');
  84.                 sleep($driver::SLEEP_BETWEEN_OPERATIONS);
  85.                 $this->log($output'INVOICES DELETE ' $connectorInfos);
  86.                 $loader->deleteInvoices();
  87.                 $this->log($output'SYNCHRONISED');
  88.                 $this->log($output'=============================');
  89.                 sleep($driver::SLEEP_BETWEEN_OPERATIONS);
  90.                 if (!$connector->isSynchronized()) {
  91.                     $connector->setSynchronized(true);
  92.                     $this->connectorRepository->save($connector);
  93.                 }
  94.             } catch (Exception $exception) {
  95.                 $this->log($output"EXCEPTION $connectorInfos SKIP: "$exception->getMessage());
  96.                 $this->log($output'=============================');
  97.                 // Capture exception and continue iteration.
  98.                 captureException($exception);
  99.                 continue;
  100.             }
  101.         }
  102.     }
  103.     /**
  104.      * @inheritDoc
  105.      */
  106.     public static function getSubscribedEvents()
  107.     {
  108.         return [
  109.             ConnectorSynchronization::class => 'onSynchronizing'
  110.         ];
  111.     }
  112.     protected function log(?OutputInterface $outputstring $messagestring $level 'info'): void
  113.     {
  114.         if (null !== $output) {
  115.             $output->writeln(
  116.                 sprintf('%s [%s] %s', (new Carbon())->toDateTimeString(), $level$message)
  117.             );
  118.         }
  119.     }
  120. }