Esempio n. 1
0
 public function sendNow(Notification $notification, CtoUser $admin, $broadcast = false)
 {
     if ($broadcast) {
         //   Broadcast
         $iDs = explode(',', $notification->getBroadcastTo());
         if (in_array('-1', $iDs)) {
             $admin = $notification->getUserCto();
             $users = $this->em->getRepository('CTOAppBundle:CtoClient')->clientFilter([], $admin);
             $notification->setBroadcastTo(-1);
         } else {
             $users = $this->em->getRepository('CTOAppBundle:CtoClient')->findBy(['id' => $iDs]);
         }
         /** @var CtoClient $user */
         foreach ($users as $user) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $user->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($user);
                 $report->setPhone($user->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
                 $notification->setStatus(Notification::STATUS_SEND_OK);
             } catch (\Exception $e) {
                 $report = new NotificationReport($user);
                 $report->setPhone($user->getPhone())->setStatus(NotificationReport::REPORT_STATUS_FAILED);
                 $this->em->persist($report);
                 $notification->addReport($report);
                 $notification->setStatus(Notification::STATUS_SEND_FAIL);
             }
         }
         if ($notification->isAdminCopy()) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $admin->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
             } catch (\Exception $e) {
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
             }
         }
         $this->em->flush();
     } else {
         //  Normal
         try {
             $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $notification->getClientCto()->getPhone(), $notification->getDescription());
             $report = new NotificationReport($notification->getClientCto());
             $report->setPhone($notification->getClientCto()->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
             $this->em->persist($report);
             $notification->addReport($report);
             $notification->setStatus(Notification::STATUS_SEND_OK);
         } catch (\Exception $e) {
             $report = new NotificationReport($notification->getClientCto());
             $report->setPhone($notification->getClientCto()->getPhone())->setStatus(NotificationReport::REPORT_STATUS_FAILED);
             $this->em->persist($report);
             $notification->addReport($report);
             $notification->setStatus(Notification::STATUS_SEND_FAIL);
         }
         if ($notification->isAdminCopy()) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $admin->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
             } catch (\Exception $e) {
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_FAILED);
                 $this->em->persist($report);
             }
         }
         $this->em->flush();
     }
 }