src/Salesforce/EventListener/SalesForceImportListener.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Salesforce\EventListener;
  3. use App\Api\Event\ImportEvent;
  4. use App\Api\Event\ImportEvents;
  5. use App\Entity\Sale;
  6. use App\Entity\Vehicle;
  7. use App\Repository\SaleEventRepository;
  8. use App\Repository\VehicleRepository;
  9. use App\Salesforce\Api\ClientInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * SalesForce Event listener for the Import events.
  13.  */
  14. class SalesForceImportListener implements EventSubscriberInterface
  15. {
  16.     public function __construct(private readonly ClientInterface $sfClient, private readonly VehicleRepository $vehicleRepository, private readonly SaleEventRepository $saleEventRepository)
  17.     {
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             ImportEvents::DATA_LAST => 'onLastData',
  23.         ];
  24.     }
  25.     /**
  26.      * DO NOT call $sale->getEvents() or any other form to get the events here, it seems Doctrine isn't fully done with them
  27.      * Use queries from the repository, that works.
  28.      */
  29.     public function onLastData(ImportEvent $event): void
  30.     {
  31.         $object $event->getObject();
  32.         if (!$object instanceof Vehicle) {
  33.             return;
  34.         }
  35.         $sale $object->getSale();
  36.         $events '';
  37.         if (Sale::TYPE_PHYSICAL === $sale->getType()) {
  38.             $events join(' / ',
  39.                 array_map(
  40.                     fn ($e) => $e->getStartTime().' - '.$e->getTitle()->getName(),
  41.                     $this->saleEventRepository->findAllInSales([$sale])
  42.                 )
  43.             );
  44.         }
  45.         $sSale array_filter([
  46.             'ExternalId__c' => $sale->getId(),
  47.             'NbrVehiculesProposes__c' => $this->vehicleRepository->countAllActiveBySale($sale),
  48.             'Sous_vente_physique_V2__c' => $events,
  49.         ]);
  50.         $this->sfClient->initiate()->upsert('ExternalId__c', (object) $sSale'SessionVente__c');
  51.     }
  52. }