| Total Complexity | 58 |
| Total Lines | 408 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ShoppingCartFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShoppingCartFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ShoppingCartFactory |
||
| 26 | { |
||
| 27 | use Injectable; |
||
| 28 | use Configurable; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Name of the test cookie used to check if cookies are allowed |
||
| 32 | */ |
||
| 33 | const TEST_COOKIE = "ShoppingCartFactoryTest"; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Name of Cookie/Session used to track cart access key |
||
| 37 | */ |
||
| 38 | const COOKIE_NAME = "ShoppingCart.Key"; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The default class that is used by the factroy |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private static $model = ShoppingCartModel::class; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The default class that is used by the factroy |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private static $controller = ShoppingCartController::class; |
||
|
|
|||
| 53 | |||
| 54 | /** |
||
| 55 | * Should the cart globally check for stock levels on items added? |
||
| 56 | * Using this setting will ignore individual "Stocked" settings |
||
| 57 | * on Shopping Cart Items. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private static $check_stock_levels = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * whether or not the cleaning task should be left to a cron job |
||
| 65 | * |
||
| 66 | * @var boolean |
||
| 67 | * @config |
||
| 68 | */ |
||
| 69 | private static $cron_cleaner = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The current shopping cart |
||
| 73 | * |
||
| 74 | * @var ShoppingCart |
||
| 75 | */ |
||
| 76 | protected $current; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Setup the shopping cart and return an instance |
||
| 80 | * |
||
| 81 | * @return ShoppingCart |
||
| 82 | **/ |
||
| 83 | public function __construct() |
||
| 84 | { |
||
| 85 | $cookies = $this->cookiesSupported(); |
||
| 86 | $member = Security::getCurrentUser(); |
||
| 87 | |||
| 88 | $cart = $this->findOrMakeCart(); |
||
| 89 | |||
| 90 | // If we don't have any discounts, a user is logged in and he has |
||
| 91 | // access to discounts through a group, add the discount here |
||
| 92 | if (!$cart->getDiscount()->exists() && $member && $member->getDiscount()) { |
||
| 93 | $cart->DiscountCode = $member->getDiscount()->Code; |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!$this->config()->cron_cleaner) { |
||
| 97 | $this->cleanOld(); |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->current = $cart; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the current session from the current request |
||
| 105 | * |
||
| 106 | * @return Session |
||
| 107 | */ |
||
| 108 | public function getSession() |
||
| 109 | { |
||
| 110 | $request = Injector::inst()->get(HTTPRequest::class); |
||
| 111 | return $request->getSession(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Either find an existing cart, or create a new one. |
||
| 116 | * |
||
| 117 | * @return ShoppingCartModel |
||
| 118 | */ |
||
| 119 | public function findOrMakeCart() |
||
| 120 | { |
||
| 121 | $cookies = $this->cookiesSupported(); |
||
| 122 | $session = $this->getSession(); |
||
| 123 | $classname = self::config()->model; |
||
| 124 | $cart = null; |
||
| 125 | $write = false; |
||
| 126 | $member = Security::getCurrentUser(); |
||
| 127 | |||
| 128 | if ($cookies) { |
||
| 129 | $cart_id = Cookie::get(self::COOKIE_NAME); |
||
| 130 | } else { |
||
| 131 | $cart_id = $session->get(self::COOKIE_NAME); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Try to get a cart from the the DB |
||
| 135 | if (isset($cart_id)) { |
||
| 136 | $cart = $classname::get()->find('AccessKey', $cart_id); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Does the current member have a cart? |
||
| 140 | if (empty($cart) && isset($member) && $member->Cart()->exists()) { |
||
| 141 | $cart = $member->Cart(); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Finally, if nothing is set, create a new instance to return |
||
| 145 | if (empty($cart)) { |
||
| 146 | $cart = $classname::create(); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $cart; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Run the task to clean old shopping carts |
||
| 154 | * |
||
| 155 | * @return null |
||
| 156 | */ |
||
| 157 | public function cleanOld() |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Test to see if the current user supports cookies |
||
| 174 | * |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function cookiesSupported() |
||
| 178 | { |
||
| 179 | Cookie::set(self::TEST_COOKIE, 1); |
||
| 180 | $cookie = Cookie::get(self::TEST_COOKIE); |
||
| 181 | Cookie::force_expiry(self::TEST_COOKIE); |
||
| 182 | |||
| 183 | return (empty($cookie)) ? false : true; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the current shopping cart |
||
| 188 | * |
||
| 189 | * @return ShoppingCart |
||
| 190 | */ |
||
| 191 | public function getCurrent() |
||
| 192 | { |
||
| 193 | return $this->current; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add an item to the shopping cart. By default this should be a |
||
| 198 | * line item, but this method will determine if the correct object |
||
| 199 | * has been provided before attempting to add. |
||
| 200 | * |
||
| 201 | * @param array $item The item to add (defaults to @link LineItem) |
||
| 202 | * @param array $customisations (A list of @LineItemCustomisations customisations to provide) |
||
| 203 | * |
||
| 204 | * @throws ValidationException |
||
| 205 | * @return self |
||
| 206 | */ |
||
| 207 | public function addItem($item, $customisations = []) |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Find an existing item and update its quantity |
||
| 303 | * |
||
| 304 | * @param LineItem $item the item in the cart to update |
||
| 305 | * @param int $quantity the new quantity |
||
| 306 | * |
||
| 307 | * @throws ValidationException |
||
| 308 | * @return self |
||
| 309 | */ |
||
| 310 | public function updateItem($item, $quantity) |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Remove a LineItem from ShoppingCart |
||
| 348 | * |
||
| 349 | * @param LineItem $item The item to remove |
||
| 350 | * |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | public function removeItem($item) |
||
| 366 | } |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Destroy current shopping cart |
||
| 371 | * |
||
| 372 | * @return self |
||
| 373 | */ |
||
| 374 | public function delete() |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Save the current shopping cart, by writing it to the DB and |
||
| 395 | * generating a cookie/session (if user not logged in). |
||
| 396 | * |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | public function save() |
||
| 433 | } |
||
| 434 | } |