I have this class containing a method that performs a few processes for displaying the payment page.
class Payment extends Controller
{
public function displayPaymentPage()
{
$session = new Session();
if(is_null($session->getSessionKey('userId'))) {
header('Location: /login');
} else {
$cart = new Cart();
if($cart->doesCartExistForCurrentUser()) {
$session = new Session();
$cartId = $session->getSessionKey('cartId');
$connection = new PDOConnection();
$cartTable = new CartTable($connection);
$cartItemTable = new CartItemTable($connection);
$data['cartAmountDetails'] = $cartTable->getCartTotals($cartId);
$data['cartItems'] = $cartItemTable->getCartItemsByCartId($cartId);
$this->viewPresenter->display('basic', '/payment/payment', $data, 'Payment');
} else {
header('Location: /cart');
}
}
}
}
And this other class for the Shipping entity:
class Shipping extends Controller
{
public function displayShippingForm()
{
$session = new Session();
if(is_null($session->getSessionKey('userId'))) {
// Set flag so user will be redirected to the Shipping page after loggin in
$session->setSessionKey('cartPage', true);
header('Location: /login');
} else {
$cart = new Cart();
$data = $this->getShippingInformation($cart, $session);
$data['errors']['shipping'] = $session->getFlashFormErrors('shipping');
$this->viewPresenter->display('basic', 'shipping/shipping', $data, 'Shipping');
}
}
// Contents of this were originally entirely inside the above method
// Decided to refactor it
// Wasn't sure if it goes into a Service class instead,
// since it depends on different entities
private function getShippingInformation(Cart $cart, Session $session)
{
if($cart->doesCartExistForCurrentUser()) {
$cartId = $session->getSessionKey('cartId');
$connection = new PDOConnection();
$cartTable = new CartTable($connection);
$totalWeight = $cartTable->getCartTotals($cartId)->total_weight;
$shippingTable = new ShippingTable($connection);
$shippingModel = new ShippingModel($shippingTable);
$data['groundShipping'] = $shippingModel->getGroundShippingParticulars($totalWeight);
$data['expeditedShipping'] = $shippingModel->getExpeditedShippingParticulars($totalWeight);
return $data;
} else {
header('Location: /cart');
}
}
}
And I have this one service class:
class CartItemService
{
// The contents of this function were originally inside the controller
// But I realized they may be considered one unit of work
// So I decided to group them together
// Is putting them inside the Service class an overkill
// or should I have just refactored inside the Controller?
public function saveCartItem($cartId, CartItem $cartItem, CartItemTable $cartItemTable, Product $product, ProductTable $productTable)
{
$productDetails = $productTable->getProductDetailsByProductId($cartItem->productId);
$productPrice = $product->computeProductPrice($productTable, $cartItem->productId, $cartItem->quantity);
$cartItem->weight = $productDetails->weight;
$cartItem->unitPrice = $productDetails->price;
$cartItem->price = $productPrice;
$cartItem->cartId = $cartId;
$cartItemTable->insertCartItem($cartItem);
}
}
My other question is, aside from those in the comments above, can any of these be considered business logic? I've always been pretty unsure about the scope of business logic.
For example, testing the uniqueness of email addresses, checking if the user is logged in, checking if an entity already exists before proceeding -- are those considered business logic? And whether those checks should be further encapsulated inside their corresponding Models, or if it's okay to perform simple null checks in the controller. What are some examples of logical checks that are allowable inside the Controller?