/** * Creates an info. * * @param string $body * @param string[] $topics * * @throws BlankBodyApiException * @throws NoTopicsApiException * @throws NonExistentTopicApiException */ public function create($body, $topics) { if ($body === '') { throw new BlankBodyApiException(); } if (count($topics) == 0) { throw new NoTopicsApiException(); } foreach ($topics as $topic) { try { $this->pickTopicApiWorker->pick($topic); } catch (NonExistentIdApiException $e) { throw new NonExistentTopicApiException(); } } $this->connectToStorageInternalWorker->connect()->insert(array('id' => uniqid(), 'body' => $body, 'topics' => $topics, 'created' => time())); }
/** * Updates the subscription with given mobile and uniqueness. * * @param string $mobile * @param string $uniqueness * @param string $alias * @param string[] $topics * @param boolean $active * * @throws BlankAliasApiException * @throws NoTopicsApiException * @throws NonExistentIdApiException * @throws NonExistentMobileAndUniquenessApiException */ public function update($mobile, $uniqueness, $alias, $topics, $active) { if ($alias === '') { throw new BlankAliasApiException(); } if (count($topics) == 0) { throw new NoTopicsApiException(); } foreach ($topics as $topic) { try { $this->pickTopicApiWorker->pick($topic); } catch (NonExistentIdApiException $e) { throw $e; } } $result = $this->connectToStorageInternalWorker->connect()->update(['mobile' => $mobile, 'uniqueness' => $uniqueness], ['$set' => ['alias' => $alias, 'topics' => $topics, 'active' => $active, 'order' => time()]]); if ($result['n'] == 0) { throw new NonExistentMobileAndUniquenessApiException(); } }
/** * Updates the info with given id. * * @param string $id * @param string $body * @param string[] $topics * * @throws BlankBodyApiException * @throws NoTopicsApiException * @throws NonExistentTopicApiException * @throws NonExistentIdApiException */ public function update($id, $body, $topics) { if ($body === '') { throw new BlankBodyApiException(); } if (count($topics) == 0) { throw new NoTopicsApiException(); } foreach ($topics as $topic) { try { $this->pickTopicApiWorker->pick($topic); } catch (NonExistentIdApiException $e) { throw new NonExistentTopicApiException(); } } $result = $this->connectToStorageInternalWorker->connect()->update(array('id' => $id), array('$set' => array('body' => $body, 'topics' => $topics, 'created' => time()))); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * @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 } }
/** * @Req\Route("/info-sms/pick-topic/{topic}") * @Req\Method({"GET"}) * * @param string $topic * * @return JsonResponse */ public function pickAction($topic) { return new JsonResponse($this->pickTopicApiWorker->pick($topic)); }