src/Salesforce/EventListener/SalesForceComplaintSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Salesforce\EventListener;
  3. use App\Entity\CustomerComplaint;
  4. use App\Event\CustomerComplaintEvent;
  5. use App\Salesforce\Api;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class SalesForceComplaintSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly Api\ClientInterface $sfClient, private readonly LoggerInterface $logger)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             CustomerComplaintEvent::NAME => 'onNewCustomerComplaint',
  17.         ];
  18.     }
  19.     public function onNewCustomerComplaint(CustomerComplaintEvent $event): void
  20.     {
  21.         $complaint $event->getEntity();
  22.         if (!($complaint instanceof CustomerComplaint)) {
  23.             return;
  24.         }
  25.         $this->sfClient->initiate();
  26.         if (null !== $complaint->getUser()->getSalesman()) {
  27.             $data $this->sfClient->findOne(['Id'], 'User', ['Email' => $complaint->getUser()->getSalesman()->getEmail()]);
  28.             $owner $data->Id ?? null;
  29.         }
  30.         if (false !== $this->sfClient->exists('Adjudication__c', ['ExternalId__c' => $complaint->getReference()])) {
  31.             $upSCase = (object) array_filter([
  32.                 'Adjudication__r' => (object) ['ExternalId__c' => $complaint->getReference()],
  33.                 'AccountId' => $complaint->getUser()->getSfAccountId(),
  34.                 'num_dossier__c' => $complaint->getReference(),
  35.                 'ExternalId__c' => $complaint->getReference(),
  36.                 'Origin' => 'Web',
  37.                 'Subject' => 'RECLAMATION',
  38.                 'Motif_de_la_reclamation__c' => $complaint->getSubject(),
  39.                 'Pr_cisez_votre_demande__c' => $complaint->getDescription(),
  40.                 'OwnerId' => $owner,
  41.             ]);
  42.             foreach ($complaint->getFiles() as $key => $file) {
  43.                 $upSCase->{'Piece_justificative_'.++$key.'__c'} = $file;
  44.             }
  45.             $this->sfClient->upsert('ExternalId__c'$upSCase'Case');
  46.             return;
  47.         }
  48.         $this->logger->error(sprintf('Adjudication cannot be found for reference %s'$complaint->getReference()));
  49.     }
  50. }