/** * @param int $productId * @param int $amount amount to add * @throw ResourceNotFoundException if product not found */ public function addItem($productId, $amount = 1) { $product = Product::find($productId); if ($product == null) { throw new ResourceNotFoundException(); } // find existing basket item $item = BasketItem::findOneBy(['product' => $product, 'basket' => $this]); if ($item != null) { $item->amount += 1; } else { $this->items->add(new BasketItem($this, $product, $amount)); } $em = self::getEntityManager(); $em->persist($this); $em->flush(); }
public function rate($id) { $this->respondTo('html', function () use($id) { try { $userSession = UserSession::getInstance(); if (!$userSession->isSignedIn()) { throw new ActionNotAuthorizedException(); } Rating::create($userSession->getUser(), Product::find($id), $this->getRequest()->getParams()); $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ResourceNotFoundException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ValidationException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (DuplicateResourceException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ActionNotAuthorizedException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } }); }