src/EventSubscriber/EasyAdminSubscriber.php line 50
<?phpnamespace App\EventSubscriber;use App\Entity\ImportFile;use App\Entity\AggregatedTrueUp;use App\Entity\ManualPayment;use Doctrine\ORM\EntityManagerInterface;use Vich\UploaderBundle\Storage\StorageInterface;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class EasyAdminSubscriber implements EventSubscriberInterface{public function __construct(private EntityManagerInterface $em,private StorageInterface $storage){}public static function getSubscribedEvents(){return [# BeforeEntityPersistedEvent::class => ['setManualPaymentAggregatedTrueUp'],BeforeEntityPersistedEvent::class => ['prePersist'],#AfterEntityPersistedEvent::class => ['prePersist']];}public function setManualPaymentAggregatedTrueUp(BeforeEntityPersistedEvent $event){$entity = $event->getEntityInstance();// easy admin only passed the entity to this event and not the original form dataif (!($entity instanceof ManualPayment)) {return;}if (isset($_POST['ManualPayment']['closeOutAggregatedTrueUp']) && ($_POST['ManualPayment']['closeOutAggregatedTrueUp'])) {$lookup = $this->em->getRepository(AggregatedTrueUp::class)->findOneBy(['user' => $entity->getUserPaymentPeriod()->getUser(),'amtInPennies' => $entity->getAmtInPennies(),]);if (!$lookup) {throw new \Exception('this user has no aggregated true ups!');}$entity->setAggregatedTrueUp($lookup);}}public function prePersist(BeforeEntityPersistedEvent $event): void{$entity = $event->getEntityInstance();if (!$entity instanceof ImportFile) {return;}$importFilePath = $entity->getUploadedFile()->getRealPath();if ($importFilePath) {$lineCount = 0;$insideQuotes = false;// Open the file in read mode$handle = fopen($importFilePath, 'r');// Loop through each character until end of filewhile (!feof($handle)) {$char = fgetc($handle);// If it's a newline character and not inside quotes, increment line countif ($char === "\n" && !$insideQuotes) {$lineCount++;}// Toggle $insideQuotes when encountering a quote characterif ($char === '"') {$insideQuotes = !$insideQuotes;}}fclose($handle);// Set the total number of rows (excluding the header)$entity->setTotal($lineCount - 1);$this->em->persist($entity);$this->em->flush();}}}