<?php
namespace App\Salesforce\EventListener;
use App\Entity\CustomerComplaint;
use App\Event\CustomerComplaintEvent;
use App\Salesforce\Api;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SalesForceComplaintSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly Api\ClientInterface $sfClient, private readonly LoggerInterface $logger)
{
}
public static function getSubscribedEvents()
{
return [
CustomerComplaintEvent::NAME => 'onNewCustomerComplaint',
];
}
public function onNewCustomerComplaint(CustomerComplaintEvent $event): void
{
$complaint = $event->getEntity();
if (!($complaint instanceof CustomerComplaint)) {
return;
}
$this->sfClient->initiate();
if (null !== $complaint->getUser()->getSalesman()) {
$data = $this->sfClient->findOne(['Id'], 'User', ['Email' => $complaint->getUser()->getSalesman()->getEmail()]);
$owner = $data->Id ?? null;
}
if (false !== $this->sfClient->exists('Adjudication__c', ['ExternalId__c' => $complaint->getReference()])) {
$upSCase = (object) array_filter([
'Adjudication__r' => (object) ['ExternalId__c' => $complaint->getReference()],
'AccountId' => $complaint->getUser()->getSfAccountId(),
'num_dossier__c' => $complaint->getReference(),
'ExternalId__c' => $complaint->getReference(),
'Origin' => 'Web',
'Subject' => 'RECLAMATION',
'Motif_de_la_reclamation__c' => $complaint->getSubject(),
'Pr_cisez_votre_demande__c' => $complaint->getDescription(),
'OwnerId' => $owner,
]);
foreach ($complaint->getFiles() as $key => $file) {
$upSCase->{'Piece_justificative_'.++$key.'__c'} = $file;
}
$this->sfClient->upsert('ExternalId__c', $upSCase, 'Case');
return;
}
$this->logger->error(sprintf('Adjudication cannot be found for reference %s', $complaint->getReference()));
}
}