src/Service/ActionMatrix.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Controller\SystemActionsInterface;
  4. use App\Entity\Sms;
  5. use App\Entity\SmsSetup;
  6. use App\Entity\User;
  7. use App\Entity\WayBill;
  8. use App\Messages\SMSMessage;
  9. use DateTime;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Doctrine\Persistence\ObjectManager;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. class ActionMatrix implements SystemActionsInterface {
  14.     private MessageBusInterface $messageBus;
  15.     private ObjectManager $em;
  16.     private BuildSMSMessage $buildSMSMessage;
  17.     private WayBill $waybill;
  18.     public function __construct(MessageBusInterface $messageBus,ObjectManager $objectManagerWayBill $waybill)
  19.     {
  20.         $this->messageBus $messageBus;
  21.         $this->em $objectManager;
  22.         $this->waybill $waybill;
  23.     }
  24.     function registeringAction(User $user): void
  25.     {
  26.         $action 'registering';
  27.         $this->buildHelper($user,$action);
  28.     }
  29.     function payingAction(User $user,string $paymentMode): void
  30.     {
  31.         $action 'paying';
  32.         $this->buildHelper($user,$action);
  33.     }
  34.     function loadingAction(User $user): void
  35.     {
  36.         $action 'loading';
  37.         $this->buildHelper($user,$action);
  38.     }
  39.     function receivingAction(User $user): void
  40.     {
  41.         $action 'receiving';
  42.         $this->buildHelper($user,$action);
  43.     }
  44.     function collectingAction(User $user): void
  45.     {
  46.         $action 'collecting';
  47.         $this->buildHelper($user,$action);
  48.     }
  49.     private function buildHelper(User $user,$action): void
  50.     {
  51.         $messageTemplates $this->em->getRepository(SmsSetup::class)->findBy([
  52.             'action' => $action
  53.         ]);
  54.         $receiverPhone  str_replace(' '''$this->waybill->getReceiverPhoneNumber());
  55.         $senderPhone  str_replace(' '''$this->waybill->getSenderPhoneNumber());
  56.         $buildSms = new BuildSMSMessage($this->waybill);
  57.         foreach ($messageTemplates as $index => $messageTemplate) {
  58.             $smsMessage $buildSms->build($messageTemplate->getSampleMessage());
  59.             $sendTo '+254'.substr($senderPhone,1);
  60.             if($messageTemplate->getSendTo() == 'receiver'){
  61.                 $sendTo '+254'.substr($receiverPhone,1);
  62.             }
  63.             $availableSms $this->em->getRepository(Sms::class)->findBy([
  64.                 'transaction' => $this->waybill->getTransaction(),
  65.                 'recepient' => $sendTo,
  66.                 'action' => $action
  67.             ]);
  68.             if(!$availableSms){
  69.                 $sms = new Sms();
  70.                 $sms->setSms($smsMessage);
  71.                 $sms->setTransaction($this->waybill->getTransaction());
  72.                 $sms->setRecepient($sendTo);
  73.                 $sms->setCreatedAt(new DateTime());
  74.                 $sms->setCreatedBy($user);
  75.                 $sms->setAmount(0);
  76.                 $sms->setAction($action);
  77.                 $this->em->persist($sms);
  78.                 $this->em->flush();
  79.                 $this->messageBus->dispatch(new SMSMessage($sms->getId()));
  80.             }
  81.         }
  82.     }
  83.     function resendAction(): void {
  84.         $smses $this->em->getRepository(WayBill::class)->findFailedSmsByInserficient();
  85.         foreach ($smses as $index => $sms) {
  86.             $this->messageBus->dispatch(new SMSMessage($sms['id']));
  87.        }
  88.     }
  89. }