/** * Deletes the package with given id. * * @param string $id * * @throws NonExistentIdApiException */ public function delete($id) { $result = $this->connectToStorageInternalWorker->connect()->remove(array('id' => $id)); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * Updates the package with given id. * * @param string $id * @param string $name * @param int $amount * @param int $price * * @throws NonExistentIdApiException */ public function update($id, $name, $amount, $price) { $result = $this->connectToStorageInternalWorker->connect()->update(array('id' => $id), array('$set' => array('name' => $name, 'amount' => (int) $amount, 'price' => (int) $price))); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * Picks the package with given id. * * @param string $id * * @return array An array with the following keys: * id, name, amount and price. * * @throws NonExistentIdInternalException */ public function pick($id) { $package = $this->connectToStorageInternalWorker->connect()->findOne(['id' => $id], ['_id' => 0]); if (!$package) { throw new NonExistentIdInternalException(); } return $package; }
/** * Collect packages, sorted ascending by amount. * * @return \Iterator An array of packages with the following keys: * id, name, amount and price. */ public function collect() { return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0])->sort(['amount' => 1]); }
/** * Creates a package. * * @param string $id * @param string $name * @param int $amount * @param int $price */ public function create($id, $name, $amount, $price) { $this->connectToStorageInternalWorker->connect()->insert(array('id' => $id, 'name' => $name, 'amount' => (int) $amount, 'price' => (int) $price)); }