Example #1
0
 protected function getCommonData()
 {
     $userSession = UserSession::getInstance();
     $signedIn = $userSession->isSignedIn();
     $replacements = ['user' => $signedIn ? $userSession->getUser() : new User(), 'is_signed_in' => $signedIn, 'basket' => Basket::getInstance($userSession)];
     return $replacements;
 }
 public function buy()
 {
     $this->respondTo('html', function () {
         try {
             $userSession = UserSession::getInstance();
             if (!$userSession->isSignedIn()) {
                 throw new ActionNotAuthorizedException();
             }
             $basket = Basket::getInstance($userSession);
             if ($basket->isEmpty()) {
                 $this->getResponse()->redirect('App\\Store\\Controllers\\BasketController', 'index');
                 return;
             }
             $order = Order::create($basket, $userSession->getUser());
             $basket->clear();
             $data = array_merge($this->getCommonData(), ['order' => $order]);
             $this->render(new TwigView('basket/success.html', $data));
         } catch (ActionNotAuthorizedException $e) {
             $this->getResponse()->redirect('App\\Auth\\Controllers\\SessionController', 'signIn');
         }
     });
 }