/** * Creates a package. * * @param string $id * @param string $name * @param string $category * @param int $amount * @param int $price * * @throws NonExistentCategoryInternalException */ public function create($id, $name, $category, $amount, $price) { try { $this->pickCategoryInternalWorker->pick($category); } catch (NonExistentCategoryInternalException $e) { throw $e; } $this->connectToStorageInternalWorker->connect()->insert(array('id' => $id, 'name' => $name, 'category' => $category, 'amount' => (int) $amount, 'price' => (int) $price)); }
/** * Updates the package with given code. * * @param string $id * @param string $name * @param string $category * @param int $amount * @param int $price * * @throws NonExistentCategoryApiException * @throws NonExistentIdApiException */ public function update($id, $name, $category, $amount, $price) { try { $this->pickCategoryInternalWorker->pick($category); } catch (NonExistentCategoryApiException $e) { throw $e; } $result = $this->connectToStorageInternalWorker->connect()->update(array('id' => $id), array('$set' => array('name' => $name, 'category' => $category, 'amount' => (int) $amount, 'price' => (int) $price))); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * Creates cards. * * @param string $category * @param integer $amount * * @return string[] the already created card codes * * @throws NonExistentCategoryException */ public function create($category, $amount) { try { $this->pickCategoryInternalWorker->pick($category); } catch (NonExistentCategoryException $e) { throw $e; } $codes = []; for ($i = 1; $i <= $amount; $i++) { $code = $this->generateCode(); $this->connectToStorageInternalWorker->connect()->insert(array('code' => $code, 'category' => $category, 'consumed' => false)); $codes[] = $code; } return $codes; }
/** * 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']); }
/** * @param string $uniqueness * @param string $package * * @throws NonExistentUniquenessApiException * @throws NonExistentIdApiException */ public function lend($uniqueness, $package) { // Verify uniqueness try { $this->pickProfileApiWorker->pick($uniqueness); } catch (NonExistentUniquenessApiException $e) { throw $e; } // Get package try { $package = $this->pickPackageInternalWorker->pick($package); } catch (NonExistentIdInternalException $e) { throw new NonExistentIdApiException(); } // Generate cards $cards = $this->createCardsInternalWorker->create($package['category'], $package['amount']); // Assign cards $this->assignCardsInternalWorker->assign($uniqueness, $cards); // Increase debt $category = $this->pickCategoryInternalWorker->pick($package['category']); $this->increaseDebtInternalWorker->increase($uniqueness, $package['price'], sprintf("Préstamo de %s tarjetas de categoría \"%s\"", $package['amount'], $category['name'])); }