/**
  * @param string   $mobile
  * @param string   $uniqueness
  * @param string   $alias
  * @param string[] $topics
  * @param int      $amount
  *
  * @throws TrialNotAcceptedInternalException
  * @throws ExistentMobileInternalException
  */
 public function create($mobile, $uniqueness, $alias, $topics, $amount)
 {
     if ($this->checkOperationInternalWorker->checkTrial($mobile)) {
         throw new TrialNotAcceptedInternalException();
     }
     try {
         $this->createSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, $amount, 0, true);
     } catch (ExistentMobileInternalException $e) {
         throw $e;
     }
     $this->enqueueMessageApiWorker->enqueue($mobile, sprintf("Tu telefono se ha subscrito con %s sms gratis para recibir noticias %s que seleccionaste.", 10, count($topics) == 1 ? "del topico" : sprintf("de los %s topicos", count($topics))));
     $this->logOperationInternalWorker->logTrial($mobile, $uniqueness, $topics, time());
 }
Example #2
0
 /**
  * Processes infos.
  */
 public function process()
 {
     while (true) {
         try {
             // Pops oldest info from list
             $info = $this->popInfoInternalWorker->pop();
             // Finds subscriptions with given topics
             $subscriptions = $this->collectSubscriptionsByTopicsInternalWorker->collect($info['topics']);
             $total = 0;
             // Iterate over subscriptions
             foreach ($subscriptions as $subscription) {
                 try {
                     $this->decreaseSubscriptionBalanceInternalWorker->decrease($subscription['mobile']);
                 } catch (InactiveInternalException $e) {
                     // Ignore the subscription and continue with the next one
                     continue;
                 } catch (InsufficientBalanceInternalException $e) {
                     // Ignore the subscription and continue with the next one
                     continue;
                 }
                 // Create message
                 $message = $this->enqueueMessageApiWorker->enqueue($subscription['mobile'], $info['body']);
                 $this->createLinkInternalWorker->create($message, $info['id'], $subscription['mobile']);
                 $total++;
             }
             $this->createStatInternalWorker->create($info, $total, time());
         } catch (EmptyQueueInternalException $e) {
             return;
         }
     }
 }
 /**
  * @param string   $mobile
  * @param string   $uniqueness
  * @param string   $alias
  * @param string[] $topics
  * @param array    $resellPackage
  *
  * @throws InsufficientBalanceInternalException
  * @throws ExistentMobileInternalException
  */
 public function create($mobile, $uniqueness, $alias, $topics, $resellPackage)
 {
     if (!$this->checkBalanceInternalWorker->check($uniqueness, $resellPackage['amount'])) {
         throw new InsufficientBalanceInternalException();
     }
     try {
         $this->createSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, 0, $resellPackage['amount'], true);
     } catch (ExistentMobileInternalException $e) {
         throw $e;
     }
     $this->decreaseBalanceInternalWorker->decrease($uniqueness, $resellPackage['amount']);
     if (!$this->checkOperationInternalWorker->checkTrial($mobile) && !$this->checkOperationInternalWorker->checkCreate($mobile)) {
         $this->enqueueMessageApiWorker->enqueue($mobile, sprintf("Tu telefono se ha subscrito con %s sms para recibir noticias %s que seleccionaste.", $resellPackage['amount'], count($topics) == 1 ? "del topico" : sprintf("de los %s topicos", count($topics))));
     }
     $this->logOperationInternalWorker->logCreate($mobile, $uniqueness, $topics, $resellPackage['amount'], time());
 }
 /**
  * @param string   $mobile
  * @param string   $uniqueness
  * @param string[] $topics
  * @param string   $resellPackage
  *
  * @throws NonExistentMobileAndUniquenessApiException
  * @throws NoTopicsApiException
  * @throws NonExistentTopicApiException
  * @throws NoResellPackageApiException
  * @throws NonExistentResellPackageInternalException
  * @throws TrialNotAcceptedApiException
  * @throws InsufficientBalanceApiException
  */
 public function recharge($mobile, $uniqueness, $topics, $resellPackage)
 {
     $subscription = $this->connectToStorageInternalWorker->connect()->findOne(['mobile' => $mobile, 'uniqueness' => $uniqueness]);
     if (!$subscription) {
         throw new NonExistentMobileAndUniquenessApiException();
     }
     if (count($topics) == 0) {
         throw new NoTopicsApiException();
     }
     foreach ($topics as $topic) {
         try {
             $this->pickTopicApiWorker->pick($topic);
         } catch (NonExistentIdApiException $e) {
             throw new NonExistentTopicApiException();
         }
     }
     if (!$resellPackage) {
         throw new NoResellPackageApiException();
     }
     // Pick resell package to use amount
     try {
         $resellPackage = $this->pickResellPackageInternalWorker->pick($resellPackage);
     } catch (NonExistentResellPackageInternalException $e) {
         throw $e;
     }
     if ($resellPackage['price'] == 0) {
         throw new TrialNotAcceptedApiException();
     }
     if (!$this->checkBalanceInternalWorker->check($uniqueness, $resellPackage['amount'])) {
         throw new InsufficientBalanceApiException();
     }
     $this->connectToStorageInternalWorker->connect()->update(['mobile' => $mobile, 'uniqueness' => $uniqueness], ['$set' => ['topics' => $topics], '$inc' => ['balance' => $resellPackage['amount']]]);
     $this->decreaseBalanceInternalWorker->decrease($uniqueness, $resellPackage['amount']);
     $this->enqueueMessageApiWorker->enqueue($mobile, sprintf("Tu telefono se ha recargado con %s sms para seguir recibiendo noticias.", $resellPackage['amount']));
     $this->logOperationInternalWorker->logRecharge($mobile, $uniqueness, $topics, $subscription['trial'], $subscription['balance'], $resellPackage['amount'], time());
     try {
         $this->deleteLowBalanceReminderLogInternalWorker->delete($mobile);
     } catch (NonExistentMobileInternalException $e) {
         // This subscription did not have a low balance reminder log
     }
 }