src/EventSubscriber/EasyAdminSubscriber.php line 50

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ImportFile;
  4. use App\Entity\AggregatedTrueUp;
  5. use App\Entity\ManualPayment;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Vich\UploaderBundle\Storage\StorageInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class EasyAdminSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private EntityManagerInterface $em,
  15.         private StorageInterface $storage
  16.     )
  17.     {
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.            # BeforeEntityPersistedEvent::class => ['setManualPaymentAggregatedTrueUp'],
  23.             BeforeEntityPersistedEvent::class => ['prePersist'],
  24.             #AfterEntityPersistedEvent::class => ['prePersist']
  25.         ];
  26.     }
  27.     public function setManualPaymentAggregatedTrueUp(BeforeEntityPersistedEvent $event)
  28.         {
  29.             $entity $event->getEntityInstance();
  30.             // easy admin only passed the entity to this event and not the original form data
  31.             if (!($entity instanceof ManualPayment)) {
  32.                 return;
  33.             }
  34.             if (isset($_POST['ManualPayment']['closeOutAggregatedTrueUp']) && ($_POST['ManualPayment']['closeOutAggregatedTrueUp'])) {
  35.                 $lookup $this->em->getRepository(AggregatedTrueUp::class)->findOneBy([
  36.                     'user' => $entity->getUserPaymentPeriod()->getUser(),
  37.                     'amtInPennies' => $entity->getAmtInPennies(),
  38.                 ]);
  39.                 if (!$lookup) {
  40.                     throw new \Exception('this user has no aggregated true ups!');
  41.                 }
  42.                 $entity->setAggregatedTrueUp($lookup);
  43.             }
  44.         }
  45.     public function prePersist(BeforeEntityPersistedEvent $event): void
  46.     {
  47.         $entity $event->getEntityInstance();
  48.         if (!$entity instanceof ImportFile) {
  49.             return;
  50.         }
  51.         $importFilePath $entity->getUploadedFile()->getRealPath();
  52.         if ($importFilePath) {
  53.             $lineCount 0;
  54.             $insideQuotes false;
  55.             // Open the file in read mode
  56.             $handle fopen($importFilePath'r');
  57.             // Loop through each character until end of file
  58.             while (!feof($handle)) {
  59.                 $char fgetc($handle);
  60.                 // If it's a newline character and not inside quotes, increment line count
  61.                 if ($char === "\n" && !$insideQuotes) {
  62.                     $lineCount++;
  63.                 }
  64.                 // Toggle $insideQuotes when encountering a quote character
  65.                 if ($char === '"') {
  66.                     $insideQuotes = !$insideQuotes;
  67.                 }
  68.             }
  69.             fclose($handle);
  70.             // Set the total number of rows (excluding the header)
  71.             $entity->setTotal($lineCount 1);
  72.             $this->em->persist($entity);
  73.             $this->em->flush();
  74.         }
  75.     }
  76. }