/** * Consumes given card, increasing the credit balance of given uniqueness * and marking the card as consumed. * * @param string $uniqueness * @param string $card * * @throws NonExistentCodeApiException * @throws NonExistentUniquenessApiException * @throws AlreadyConsumedApiException */ public function consume($uniqueness, $card) { // Check card code try { $card = $this->pickCardInternalWorker->pick($card); } catch (NonExistentCodeInternalException $e) { throw new NonExistentCodeApiException(); } // Check uniqueness try { $this->pickProfileApiWorker->pick($uniqueness); } catch (NonExistentUniquenessApiException $e) { throw $e; } // Check card status if ($card['consumed']) { throw new AlreadyConsumedApiException(); } // Get category $category = $this->pickCategoryInternalWorker->pick($card['category']); // Increase credit balance $this->increaseCreditBalanceSharedWorker->increase($uniqueness, $category['utility'], sprintf("Recarga de saldo con tarjeta de recarga \"%s\"", $card['code'])); // Mark card as consumed $this->punchCardInternalWorker->punch($card['code']); }
/** * Picks the profile with given uniqueness. * * @param string $uniqueness * * @return array An array with the following keys: * uniqueness, debt and cards. * * @throws NonExistentUniquenessApiException */ public function pick($uniqueness) { $profile = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['_id' => 0]); if (!$profile) { throw new NonExistentUniquenessApiException(); } foreach ($profile['cards'] as $i => $card) { $profile['cards'][$i] = $this->pickCardInternalWorker->pick($card); } return $profile; }