public function addToCart(User $user, Item $item, $quantity) { $idItem = intval($item->getId()); if (ctype_digit($quantity)) { if ($quantity > 0) { if ($quantity <= $item->getStock()) { $quantity = intval($quantity); } else { $quantity = intval($item->getStock()); } if ($quantity) { if (isset($_SESSION['id'])) { $idCart = intval($user->getCart()->getId()); $query = "INSERT INTO order (id_cart, id_item, quantity) VALUES (" . $idCart . ", " . $idItem . ", " . $quantity . ")"; $result = $this->database->exec($query); if ($result) { $id = $this->database->lastInsertId(); if ($id) { return $user->getCart(); } else { throw new Exception("Catastrophe serveur."); } } else { throw new Exception("Catastrophe base de données."); } } else { throw new Exception("Erreur vraiment bizarre, là, je peux pas aider."); } } else { throw new Exception("Pas de quantité, sérieusement ? ON ENVOIE AU HASARD ?"); } } else { throw new Exception("La quantité doit être supérieure à 0, vilain violeur de poules."); } } else { throw new Exception("La quantité doit être un nombre, vilain lutin violeur de lapins."); } }
function testReservation() { $item = new Item('Perfect PHP', 3600, '2010/11', 2); $this->assertFalse($item->reservation('1')); $this->assertFalse($item->reservation(1.1)); $this->assertTrue($item->reservation(1)); $this->assertIdentical($item->getStock(), 1); $item = new Item('Perfect PHP', 3600, '2010/11', 2); $this->assertTrue($item->reservation(2)); $this->assertIdentical($item->getStock(), 0); $item = new Item('Perfect PHP', 3600, '2010/11', 2); $this->assertFalse($item->reservation(3)); $this->assertIdentical($item->getStock(), 2); }
/** * @param Subcategory $subcategory * @param $name * @param $descr * @param $short_descr * @param $price * @param $stock * @return array * @throws Exception */ public function create(Subcategory $subcategory, $name, $descr, $short_descr, $price, $stock) { $errors = array(); $item = new Item($this->db); try { $item->setName($name); $item->setDescription($descr); $item->setShortDescription($short_descr); $item->setPrice($price); $item->setStock($stock); } catch (Exception $e) { $errors[] = $e->getMessage(); } if (count($errors) == 0) { $name = $this->db->quote($item->getName()); $description = $this->db->quote($item->getDescription()); $shortDescription = $this->db->quote($item->getShortDescription()); $price = $this->db->quote($item->getPrice()); $stock = $this->db->quote($item->getStock()); $query = " INSERT INTO item(id_subcategory, name, descr, short_descr, price, stock)\n VALUES(" . $subcategory->getId() . ", " . $name . ", " . $description . ", " . $shortDescription . ", " . $price . ", " . $stock . ")"; $data = $this->db->exec($query); if ($data) { $id = $this->db->lastInsertId(); if ($id) { try { return $this->findById($id); } catch (Exception $e) { $errors[] = $e->getMessage(); return $errors; } } else { throw new Exception('Last insert error'); } } else { throw new Exception('Db error'); } } else { return $errors; } }
public function update(Item $item) { $idCategory = $item->getCategory()->getId(); $id = intval($id); $name = $this->db->quote($item->getName()); $price = $this->db->quote($item->getPrice()); $stock = $this->db->quote($item->getStock()); $image = $this->db->quote($item->getImage()); $description = $this->db->quote($item->getDescription()); $query = ' UPDATE item SET name=' . $name . ', price=' . $price . ', stock=' . $stock . ', image=' . $image . ', description=' . $description . ', id_category=' . $idCategory . ' WHERE id=' . $id; $res = $this->db->exec($query); if ($res) { $id = $this->db->lastInsertId(); if ($id) { return $this->findByID($id); } else { throw new Exception('Internal server Error'); } } }