<?php
namespace App\Library\Subscribers;
use App\Library\Database;
use App\Library\Event\GrapperProcessEvent;
use App\Library\Event\GrapperQueueEvent;
use App\models\acount;
use App\models\eadr;
use Doctrine\DBAL\Exception as DbalException;
use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddressSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents():array
{
return [
GrapperQueueEvent::EVENT_FETCH=>[
['getCustomer', 1000,],
['getAllAddresses', 800],
],
GrapperProcessEvent::EVENT_FETCH=>[
['getCustomer', 1000,],
['geteadr', 500],
]
];
}
/**
* @param Database $database
*/
public function __construct(private readonly Database $database){
}
/**
* @param GrapperProcessEvent $event
* @return void
*/
public function geteadr(GrapperProcessEvent $event):void{
$eadr = $this->database->getData(eadr::class, eadr::SELECT_ADDRESSE, [$event->getAddressId()]);
if($eadr===null){
$event->stopPropagation();
}
$event->setAddress($eadr);
}
/**
* @param GrapperQueueEvent|GrapperProcessEvent $event
* @return void
* @throws Exception
*/
public function getCustomer(GrapperQueueEvent|GrapperProcessEvent $event):void{
if($event instanceof GrapperQueueEvent){
if($event->getAcount() === null){
file_put_contents(__DIR__.'/../../../var/log/error.log', date('d.m.Y H:i:s '). PHP_EOL.var_export($event, true).PHP_EOL.'=============='.PHP_EOL, FILE_APPEND);
$event->stopPropagation();
return;
}
$uid = $event->getAcount()->getId();
}else{
$uid = $event->getCustomerId();
}
$customer = $this->database->getData(acount::class, acount::GET_SPECIFIC_GRAPPER_CUSTOMERS, [0=>$uid]);
if($customer===null){
$event->stopPropagation();
}
$event->setAcount($customer);
}
/**
* @param GrapperQueueEvent $event
* @return void
* @throws DbalException
*/
public function getAllAddresses(GrapperQueueEvent $event):void{
$event->addVerboseOutput('getting Addresses');
$stmt = $this->database->getConnection()->prepare(eadr::SELECT_ADDRESSES);
$stmt->bindValue(1, $event->getAcount()->getId());
$result = $stmt->executeQuery();
$rowsInserted=0;
$maxCount = $event->getAcount()->getLizenz()->getRemainingMails();
if($maxCount > $event->getAcount()->getMailsPerBatch()){
$maxCount = $event->getAcount()->getMailsPerBatch();
}
if($result->rowCount() > 0){
while($row = $result->fetchAssociative()){
$obj = new eadr();
$obj->init($row);;
$event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail());
if($obj->isSendingAllowed($event->getAcount()->getInterval())){
$event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail().' - added to queue');
$event->getAddresses()->add($obj);
}else{
$event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Address Id: '.$obj->getId().' email: '.$obj->getEmail().' - not sent');
}
}
}
$event->addVerboseOutput('Customer: '.$event->getAcount()->getId().' Addresses to process: '.$event->getAddresses()->count());
}
}