/** * @param string $mobile * @param string $uniqueness * @param string $alias * @param string[] $topics * @param string $resellPackage * * @throws NonExistentUniquenessApiException * @throws InvalidMobileApiException * @throws BlankAliasApiException * @throws ExistentMobileApiException * @throws NoTopicsApiException * @throws NonExistentTopicApiException * @throws NoResellPackageApiException * @throws NonExistentResellPackageApiException * @throws TrialNotAcceptedApiException * @throws InsufficientBalanceApiException */ public function create($mobile, $uniqueness, $alias, $topics, $resellPackage) { try { $this->pickProfileApiWorker->pick($uniqueness); } catch (NonExistentUniquenessApiException $e) { throw $e; } try { $this->validateMobileAndAliasInternalWorker->validate($mobile, $alias); } catch (InvalidMobileInternalException $e) { throw new InvalidMobileApiException(); } catch (BlankAliasInternalException $e) { throw new BlankAliasApiException(); } catch (ExistentMobileInternalException $e) { throw new ExistentMobileApiException(); } try { $this->validateTopicsInternalWorker->validate($topics); } catch (NoTopicsInternalException $e) { throw new NoTopicsApiException(); } catch (NonExistentTopicInternalException $e) { throw new NonExistentTopicApiException(); } $mobile = PhoneNumberFixer::fix($mobile); if (!$resellPackage) { throw new NoResellPackageApiException(); } try { $resellPackage = $this->pickResellPackageInternalWorker->pick($resellPackage); } catch (NonExistentResellPackageInternalException $e) { throw new NonExistentResellPackageApiException(); } if ($resellPackage['price'] == 0) { try { $this->createTrialSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, 10); } catch (TrialNotAcceptedInternalException $e) { throw new TrialNotAcceptedApiException(); } } else { try { $this->createPaidSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, $resellPackage); } catch (InsufficientBalanceInternalException $e) { throw new InsufficientBalanceApiException(); } } }
/** * @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 } }