src/Library/Subscribers/AddressSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Library\Subscribers;
  3. use App\Library\Database;
  4. use App\Library\Event\GrapperProcessEvent;
  5. use App\Library\Event\GrapperQueueEvent;
  6. use App\models\acount;
  7. use App\models\eadr;
  8. use Doctrine\DBAL\Exception as DbalException;
  9. use Exception;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class AddressSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents():array
  14.     {
  15.         return [
  16.             GrapperQueueEvent::EVENT_FETCH=>[
  17.                 ['getCustomer'1000,],
  18.                 ['getAllAddresses'800],
  19.             ],
  20.             GrapperProcessEvent::EVENT_FETCH=>[
  21.                 ['getCustomer'1000,],
  22.                 ['geteadr'500],
  23.             ]
  24.         ];
  25.     }
  26.     /**
  27.      * @param Database $database
  28.      */
  29.     public function __construct(private readonly Database $database){
  30.     }
  31.     /**
  32.      * @param GrapperProcessEvent $event
  33.      * @return void
  34.      */
  35.     public function geteadr(GrapperProcessEvent $event):void{
  36.         $eadr $this->database->getData(eadr::class, eadr::SELECT_ADDRESSE, [$event->getAddressId()]);
  37.         if($eadr===null){
  38.             $event->stopPropagation();
  39.         }
  40.         $event->setAddress($eadr);
  41.     }
  42.     /**
  43.      * @param GrapperQueueEvent|GrapperProcessEvent $event
  44.      * @return void
  45.      * @throws Exception
  46.      */
  47.     public function getCustomer(GrapperQueueEvent|GrapperProcessEvent $event):void{
  48.         if($event instanceof GrapperQueueEvent){
  49.         if($event->getAcount() === null){
  50.                 file_put_contents(__DIR__.'/../../../var/log/error.log'date('d.m.Y H:i:s '). PHP_EOL.var_export($eventtrue).PHP_EOL.'=============='.PHP_EOLFILE_APPEND);
  51.                 $event->stopPropagation();
  52.                 return;
  53.             }
  54.             $uid $event->getAcount()->getId();
  55.         }else{
  56.             $uid $event->getCustomerId();
  57.         }
  58.         $customer $this->database->getData(acount::class, acount::GET_SPECIFIC_GRAPPER_CUSTOMERS, [0=>$uid]);
  59.         if($customer===null){
  60.             $event->stopPropagation();
  61.         }
  62.         $event->setAcount($customer);
  63.     }
  64.     /**
  65.      * @param GrapperQueueEvent $event
  66.      * @return void
  67.      * @throws DbalException
  68.      */
  69.     public function getAllAddresses(GrapperQueueEvent $event):void{
  70.         $event->addVerboseOutput('getting Addresses');
  71.         $stmt $this->database->getConnection()->prepare(eadr::SELECT_ADDRESSES);
  72.         $stmt->bindValue(1$event->getAcount()->getId());
  73.         $result $stmt->executeQuery();
  74.         $rowsInserted=0;
  75.         $maxCount $event->getAcount()->getLizenz()->getRemainingMails();
  76.         if($maxCount $event->getAcount()->getMailsPerBatch()){
  77.             $maxCount $event->getAcount()->getMailsPerBatch();
  78.         }
  79.         if($result->rowCount() > 0){
  80.             while($row $result->fetchAssociative()){
  81.                 $obj = new eadr();
  82.                 $obj->init($row);;
  83.                 $event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail());
  84.                 if($obj->isSendingAllowed($event->getAcount()->getInterval())){
  85.                     $event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail().' - added to queue');
  86.                     $event->getAddresses()->add($obj);
  87.                 }else{
  88.                     $event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail().' - not sent');
  89.                 }
  90.             }
  91.         }
  92.         $event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Addresses to process: '.$event->getAddresses()->count());
  93.     }
  94. }