src/Products/NotificationsBundle/Form/Forms/Messages/ScriptChangeDetectionListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Form\Forms\Messages;
  3. use Products\NotificationsBundle\Entity\Notifications\Message;
  4. use Products\NotificationsBundle\Entity\Recording;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Form\FormError;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. class ScriptChangeDetectionListener implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents()
  12.     {
  13.         return [
  14.             FormEvents::PRE_SUBMIT => 'onPreSubmit',
  15.         ];
  16.     }
  17.     public function onPreSubmit(FormEvent $event)
  18.     {
  19.         $message $event->getForm()->getData();
  20.         if ( ! ($message instanceof Message)) {
  21.             return;
  22.         }
  23.         $originalRecording $message->getRecording();
  24.         if (! ($originalRecording instanceof Recording)) {
  25.             return;
  26.         }
  27.         $formData $event->getData();
  28.         $originalScript $message->getScript();
  29.         $currentScript $formData['script'] ?? '';
  30.         $strippedOriginalScript trim(preg_replace('/\s+/'' '$originalScript));
  31.         $strippedCurrentScript trim(preg_replace('/\s+/'' '$currentScript));
  32.         $isScriptModified = ($strippedOriginalScript !== $strippedCurrentScript);
  33.         $currentRecordingJson $formData['recording'] ?? '';
  34.         $currentRecordingData json_decode($currentRecordingJsontrue);
  35.         $isRecordingModified = (
  36.             (json_last_error() === JSON_ERROR_NONE) &&
  37.             isset($currentRecordingData['id']) &&
  38.             ($originalRecording->getId() !== $currentRecordingData['id'])
  39.         );
  40.         if ($isScriptModified && ! $isRecordingModified) {
  41.             $event->getForm()->addError(new FormError('Voice transcript has changed. Click "Convert text to speech".'));
  42.         }
  43.     }
  44. }