Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Controller
  5. *
  6. * class handles POST requests and redirects
  7. * the client after processing
  8. * - demo of singleton pattern
  9. */
  10. class Controller extends BaseObject {
  11. // static strings used in views
  12.  
  13. const ACTION = 'action';
  14. const METHOD_POST = 'POST';
  15. const PAGE = 'page';
  16. const CC_NAME = 'nameOnCard';
  17. const CC_NUMBER = 'cardNumber';
  18. const ACTION_ADD = 'addToCart';
  19. const ACTION_REMOVE = 'removeFromCart';
  20. const ACTION_ORDER = 'placeOrder';
  21. const ACTION_LOGIN = 'login';
  22. const USR_NAME = 'userName';
  23. const USR_PASSWORD = 'password';
  24. const ACTION_LOGOUT = 'logout';
  25.  
  26. private static $instance = false;
  27.  
  28. /**
  29. *
  30. * @return Controller
  31. */
  32. public static function getInstance() {
  33.  
  34. if (!self::$instance) {
  35. self::$instance = new Controller();
  36. }
  37. return self::$instance;
  38. }
  39.  
  40. private function __construct() {
  41.  
  42. }
  43.  
  44. /**
  45. *
  46. * processes POST requests and redirects client depending on selected
  47. * action
  48. *
  49. * @return bool
  50. * @throws Exception
  51. */
  52. public function invokePostAction() {
  53.  
  54. if ($_SERVER['REQUEST_METHOD'] != self::METHOD_POST) {
  55. throw new Exception('Controller can only handle POST requests.');
  56. return null;
  57. } elseif (!isset($_REQUEST[self::ACTION])) {
  58. throw new Exception('Action not specified.');
  59. return null;
  60. }
  61.  
  62.  
  63. // now process the assigned action
  64. $action = $_REQUEST[self::ACTION];
  65.  
  66. switch ($action) {
  67.  
  68. case self::ACTION_ADD :
  69. ShoppingCart::add((int) $_REQUEST['bookId']);
  70. Util::redirect();
  71. break;
  72.  
  73. case self::ACTION_REMOVE :
  74. ShoppingCart::remove((int) $_REQUEST['bookId']);
  75. Util::redirect();
  76. break;
  77.  
  78. case self::ACTION_ORDER :
  79. $user = AuthenticationManager::getAuthenticatedUser();
  80. // abort
  81. if ($user == null) {
  82. $this->forwardRequest(array('Not logged in.'));
  83. break;
  84. }
  85. // else
  86. if ($this->processCheckout($_POST[self::CC_NAME], $_POST[self::CC_NUMBER]))
  87. break;
  88. else
  89. return null;
  90.  
  91. case self::ACTION_LOGIN :
  92. //try to authenticate the given user
  93. if (!AuthenticationManager::authenticate($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD])) {
  94. $this->forwardRequest(array('Invalid user name or password.'));
  95. }
  96. Util::redirect();
  97. break;
  98.  
  99. case self::ACTION_LOGOUT :
  100. //sign out current user
  101. AuthenticationManager::signOut();
  102. Util::redirect();
  103. break;
  104.  
  105. default : throw new Exception('Unknown controller action: ' . $action);
  106. }
  107. }
  108.  
  109. /**
  110. *
  111. * @param string $nameOnCard
  112. * @param integer $cardNumber
  113. * @return bool
  114. */
  115. protected function processCheckout($nameOnCard = null, $cardNumber = null) {
  116.  
  117. $errors = array();
  118. $nameOnCard = trim($nameOnCard);
  119. if ($nameOnCard == null || strlen($nameOnCard) == 0) {
  120. $errors[] = 'Invalid name on card.';
  121. }
  122. if ($cardNumber == null || strlen($cardNumber) != 16 || !ctype_digit($cardNumber)) {
  123. $errors[] = 'Invalid card number. Card number must be sixteen digits.';
  124. }
  125.  
  126. if (count($errors) > 0) {
  127. $this->forwardRequest($errors);
  128. return false;
  129. }
  130.  
  131. //check cart
  132. if (ShoppingCart::size() == 0) {
  133. $this->forwardRequest(array('No items in cart.'));
  134. return false;
  135. }
  136.  
  137. //try to place a new order
  138. $user = AuthenticationManager::getAuthenticatedUser();
  139. $orderId = DataManager::createOrder($user->getId(), ShoppingCart::getAll(), $nameOnCard, $cardNumber);
  140. if (!$orderId) {
  141. $this->forwardRequest(array('Could not create order.'));
  142. return false;
  143. }
  144. //clear shopping card and redirect to success page
  145. ShoppingCart::clear();
  146. Util::redirect('index.php?view=success&orderId=' . rawurlencode($orderId));
  147.  
  148. return true;
  149. }
  150.  
  151. /**
  152. *
  153. * @param array $errors : optional assign it to
  154. * @param string $target : url for redirect of the request
  155. */
  156. protected function forwardRequest(array $errors = null, $target = null) {
  157. //check for given target and try to fall back to previous page if needed
  158. if ($target == null) {
  159. if (!isset($_REQUEST[self::PAGE])) {
  160. throw new Exception('Missing target for forward.');
  161. }
  162. $target = $_REQUEST[self::PAGE];
  163. }
  164. //forward request to target
  165. // optional - add errors to redirect and process them in view
  166. if (count($errors) > 0)
  167. $target .= '&errors=' . urlencode(serialize($errors));
  168. header('location: ' . $target);
  169. exit();
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement