zero to solid
TRANSCRIPT
![Page 1: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/1.jpg)
Zero to SOLIDin 45 Minutes
by Vic Metcalfe@v_metcalfe
![Page 2: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/2.jpg)
I made an e-Commercesite in PHP!
![Page 3: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/3.jpg)
Children or those feint of heart are warned to
leave the room…
![Page 4: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/4.jpg)
<?phpsession_start();$connection = new \PDO('mysql:host=localhost;dbname=solid', 'root', '');if (!isset($_SESSION['cart'])) { $connection->exec("INSERT INTO cart () VALUES ()"); $_SESSION['cart'] = $connection->lastInsertId();}
if (isset($_POST['addproduct'])) { $sql = "INSERT INTO cartitem (cart, product, quantity) VALUES (:cart, :product, :quantity) ON DUPLICATE KEY UPDATE quantity = quantity + :quantity"; $parameters = [ 'cart' => $_SESSION['cart'], 'product' => $_POST['addproduct'], 'quantity' => $_POST['quantity'], ]; $statement = $connection->prepare($sql); $statement->execute($parameters);}
if (isset($_POST['update'])) { $sql = "UPDATE cartitem SET quantity=:quantity WHERE cart=:cart and product=:product"; $parameters = [ 'cart' => $_SESSION['cart'], 'product' => $_POST['update'], 'quantity' => $_POST['quantity'], ]; $statement = $connection->prepare($sql); $statement->execute($parameters);}
https://github.com/zymsys/solid/blob/00/cart.php
![Page 5: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/5.jpg)
$statement = $connection->prepare("SELECT * FROM cartitem WHERE cart=:cart AND quantity <> 0");$statement->execute(['cart' => $_SESSION['cart']]);$cartItems = $statement->fetchAll();?><!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>GTA-PHP Gift Shop</title> <link rel="stylesheet" href="site.css"></head><body><div class="container"> <h1>GTA-PHP Gift Shop</h1> <p>Buy our junk to keep our organizers up to date with the latest gadgets.</p> <table class="table"> <tr> <th>Product Name</th> <th>You Pay</th> <th>Group Gets</th> <th><!-- Column for add to cart button --></th> </tr> <?php $products = []; $result = $connection->query("SELECT * FROM product"); foreach ($result as $product) { $products[$product['id']] = $product; ?> <tr> <td><?php echo $product['name']; ?></td>
https://github.com/zymsys/solid/blob/00/cart.php
![Page 6: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/6.jpg)
<td><?php $price = $product['price']; echo number_format($price / 100, 2); ?></td> <td> <?php echo number_format( ($product['price'] - $product['cost']) / 100, 2 ); ?> </td> <td> <form method="post"> <input type="number" name="quantity" value="1" style="width: 3em"> <input type="hidden" name="addproduct" value="<?php echo $product['id']; ?>"> <input class="btn btn-default btn-xs" type="submit" value="Add to Cart"> </form> </td> </tr> <?php } ?> </table> <?php if (count($cartItems) > 0): ?> <?php $total = 0; $taxable = 0;
$provinceCode = isset($_GET['province']) ? $_GET['province'] : 'ON'; //Default to GTA-PHP's home $provinces = [];
https://github.com/zymsys/solid/blob/00/cart.php
![Page 7: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/7.jpg)
$result = $connection->query("SELECT * FROM province ORDER BY name"); foreach ($result as $row) { $provinces[$row['code']] = $row; if ($row['code'] === $provinceCode) { $province = $row; } } ?> <h2>Your Cart:</h2> <table class="table"> <?php foreach ($cartItems as $cartItem): ?> <?php $product = $products[$cartItem['product']]; ?> <tr> <td> <?php echo $product['name']; ?> </td> <td> <form method="post"> Quantity: <input type="hidden" name="update" value="<?php echo $product['id']; ?>"> <input type="number" name="quantity" style="width: 3em" value="<?php echo $cartItem['quantity']; ?>"> <button type="submit">Update</button> </form> </td> <td> <?php echo number_format( $cartItem['quantity'] * $product['price'] / 100, 2 ); $itemTotal = $cartItem['quantity'] * $product['price'];
https://github.com/zymsys/solid/blob/00/cart.php
![Page 8: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/8.jpg)
$total += $itemTotal; $taxable += $product['taxes'] ? $itemTotal : 0; ?> </td> </tr> <?php endforeach; ?> <tr> <td><!-- Name --></td> <td style="text-align: right">Subtotal:</td> <td><?php echo number_format($total / 100, 2); ?></td> </tr> <tr> <td><!-- Name --></td> <td style="text-align: right"> <?php echo $province['name']; ?> taxes at <?php echo $province['taxrate'] ?>%:</td> <td> <?php $taxes = $taxable * $province['taxrate'] / 100; $total += $taxes; echo number_format($taxes / 100, 2); ?> </td> </tr> <tr> <td><!-- Name --></td> <td style="text-align: right">Total:</td> <td><?php echo number_format($total / 100, 2); ?></td> </tr> </table> <form method="get"> Calculate taxes for purchase from: <select name="province">
https://github.com/zymsys/solid/blob/00/cart.php
![Page 9: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/9.jpg)
<?php foreach ($provinces as $province): ?> <?php $selected = $provinceCode === $province['code'] ? 'selected' : ''; ?> <option value="<?php echo $province['code']; ?>" <?php echo $selected; ?>> <?php echo $province['name']; ?> </option> <?php endforeach; ?> </select> <button type="submit" class="btn btn-default btn-xs"> Recalculate</button> </form> <form action="checkout.php" method="post"> <?php foreach ($cartItems as $itemNumber => $cartItem): ?> <?php $product = $products[$cartItem['product']]; ?> <input type="hidden" name="item<?php echo $itemNumber; ?>" value="<?php echo $product['name'] . '|' . number_format($product['price'] / 100, 2); ?>"> <?php endforeach; ?> <input type="hidden" name="item<?php echo count($cartItems); ?>" value="<?php echo 'Tax|' . number_format($taxes / 100, 2); ?>"> <button type="submit" class="btn btn-primary" style="float: right"> Checkout </button> </form> <?php endif; ?></div></body></html>
https://github.com/zymsys/solid/blob/00/cart.php
![Page 10: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/10.jpg)
Is there anything wrong with this?
![Page 11: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/11.jpg)
Step 1:Separate PHP from
HTML
![Page 12: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/12.jpg)
<?phpfunction initialize(){ global $connection;
session_start(); $connection = new \PDO('mysql:host=localhost;dbname=solid', 'root', ''); if (!isset($_SESSION['cart'])) { $connection->exec("INSERT INTO cart () VALUES ()"); $_SESSION['cart'] = $connection->lastInsertId(); }}
function handleAdd(){ global $connection;
if (!isset($_POST['addproduct'])) { return; } $sql = "INSERT INTO cartitem (cart, product, quantity) VALUES (:cart, :product, :quantity) ON DUPLICATE KEY UPDATE quantity = quantity + :quantity"; $parameters = [ 'cart' => $_SESSION['cart'], 'product' => $_POST['addproduct'], 'quantity' => $_POST['quantity'], ]; $statement = $connection->prepare($sql); $statement->execute($parameters);}
https://github.com/zymsys/solid/blob/01/cart.php
![Page 13: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/13.jpg)
function handleUpdate(){ global $connection;
if (!isset($_POST['update'])) { return; } $sql = "UPDATE cartitem SET quantity=:quantity WHERE cart=:cart and product=:product"; $parameters = [ 'cart' => $_SESSION['cart'], 'product' => $_POST['update'], 'quantity' => $_POST['quantity'], ]; $statement = $connection->prepare($sql); $statement->execute($parameters);}
function loadCartItems(){ global $connection, $viewData;
$viewData = []; $statement = $connection->prepare("SELECT * FROM cartitem WHERE cart=:cart AND quantity <> 0"); $statement->execute(['cart' => $_SESSION['cart']]); return $statement->fetchAll();}
https://github.com/zymsys/solid/blob/01/cart.php
![Page 14: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/14.jpg)
function loadProducts(){ global $connection;
$products = []; $result = $connection->query("SELECT * FROM product"); foreach ($result as $product) { $products[$product['id']] = $product; } return $products;}
function loadProvinces(){ global $connection;
$provinces = []; $result = $connection->query("SELECT * FROM province ORDER BY name"); foreach ($result as $row) { $provinces[$row['code']] = $row; } return $provinces;}
https://github.com/zymsys/solid/blob/01/cart.php
![Page 15: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/15.jpg)
function calculateCartSubtotal($cartItems, $products){ $subtotal = 0;
foreach ($cartItems as $cartItem) { $product = $products[$cartItem['product']]; $subtotal += $cartItem['quantity'] * $product['price']; }
return $subtotal;}
function calculateCartTaxes($cartItems, $products, $taxrate){ $taxable = 0;
foreach ($cartItems as $cartItem) { $product = $products[$cartItem['product']]; $taxable += $product['taxes'] ? $cartItem['quantity'] * $product['price'] : 0; } return $taxable * $taxrate / 100;}
https://github.com/zymsys/solid/blob/01/cart.php
![Page 16: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/16.jpg)
function buildViewData(){ $viewData = [ 'cartItems' => loadCartItems(), 'products' => loadProducts(), 'provinces' => loadProvinces(), 'provinceCode' => isset($_GET['province']) ? $_GET['province'] : 'ON', //Default to GTA-PHP's home ];
foreach ($viewData['provinces'] as $province) { if ($province['code'] === $viewData['provinceCode']) { $viewData['province'] = $province; } }
$viewData['subtotal'] = calculateCartSubtotal($viewData['cartItems'], $viewData['products']); $viewData['taxes'] = calculateCartTaxes($viewData['cartItems'], $viewData['products'], $viewData['province']['taxrate']); $viewData['total'] = $viewData['subtotal'] + $viewData['taxes'];
return $viewData;}initialize();handleAdd();handleUpdate();
$viewData = buildViewData();?>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 17: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/17.jpg)
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>GTA-PHP Gift Shop</title> <link rel="stylesheet" href="site.css"></head><body><div class="container"> <h1>GTA-PHP Gift Shop</h1> <p>Buy our junk to keep our organizers up to date with the latest gadgets.</p> <table class="table"> <tr> <th>Product Name</th> <th>You Pay</th> <th>Group Gets</th> <th><!-- Column for add to cart button --></th> </tr> <?php foreach ($viewData['products'] as $product): ?> <tr> <td><?php echo $product['name']; ?></td> <td><?php $price = $product['price']; echo number_format($price / 100, 2); ?></td> <td><?php echo number_format( ($product['price'] - $product['cost']) / 100, 2 ); ?></td>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 18: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/18.jpg)
<td> <form method="post"> <input type="number" name="quantity" value="1" style="width: 3em"> <input type="hidden" name="addproduct" value="<?php echo $product['id']; ?>"> <input class="btn btn-default btn-xs" type="submit" value="Add to Cart"> </form> </td> </tr> <?php endforeach; ?> </table> <?php if (count($viewData['cartItems']) > 0): ?> <h2>Your Cart:</h2> <table class="table"> <?php foreach ($viewData['cartItems'] as $cartItem): ?> <?php $product = $viewData['products'][$cartItem['product']]; ?> <tr> <td> <?php echo $product['name']; ?> </td> <td> <form method="post"> Quantity: <input type="hidden" name="update" value="<?php echo $product['id']; ?>"> <input type="number" name="quantity" style="width: 3em" value="<?php echo $cartItem['quantity']; ?>"> <button type="submit">Update</button> </form> </td>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 19: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/19.jpg)
<td> <?php echo number_format( $cartItem['quantity'] * $product['price'] / 100, 2 ); ?> </td> </tr> <?php endforeach; ?> <tr> <td><!-- Name --></td> <td style="text-align: right">Subtotal:</td> <td><?php echo number_format($viewData['subtotal'] / 100, 2); ?></td> </tr> <tr> <td><!-- Name --></td> <td style="text-align: right"> <?php echo $viewData['province']['name']; ?> taxes at <?php echo $viewData['province']['taxrate'] ?>%:</td> <td> <?php echo number_format($viewData['taxes'] / 100, 2); ?> </td> </tr> <tr> <td><!-- Name --></td> <td style="text-align: right">Total:</td> <td><?php echo number_format($viewData['total'] / 100, 2); ?></td> </tr> </table>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 20: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/20.jpg)
<form method="get"> Calculate taxes for purchase from: <select name="province"> <?php foreach ($viewData['provinces'] as $province): ?> <?php $selected = $viewData['provinceCode'] === $province['code'] ? 'selected' : ''; ?> <option value="<?php echo $province['code']; ?>" <?php echo $selected; ?>> <?php echo $province['name']; ?> </option> <?php endforeach; ?> </select> <button type="submit" class="btn btn-default btn-xs">Recalculate</button> </form> <form action="checkout.php" method="post"> <?php foreach ($viewData['cartItems'] as $itemNumber => $cartItem): ?> <?php $product = $viewData['products'][$cartItem['product']]; ?> <input type="hidden" name="item<?php echo $itemNumber; ?>" value="<?php echo $product['name'] . '|' . number_format($product['price'] / 100, 2); ?>"> <?php endforeach; ?>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 21: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/21.jpg)
<input type="hidden" name="item<?php echo count($viewData['cartItems']); ?>" value="<?php echo 'Tax|' . number_format($viewData['taxes'] / 100, 2); ?>"> <button type="submit" class="btn btn-primary" style="float: right"> Checkout</button> </form> <?php endif; ?></div></body></html>
https://github.com/zymsys/solid/blob/01/cart.php
![Page 22: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/22.jpg)
Any room for improvement now?
![Page 23: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/23.jpg)
ObjectsA very brief introduction
![Page 24: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/24.jpg)
Objects help us to organize our code
![Page 25: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/25.jpg)
function initialize()function handleAdd()function handleUpdate()function loadCartItems()function loadProducts()function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
How might we group these functions?
![Page 26: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/26.jpg)
function initialize()function handleAdd()function handleUpdate()
function loadCartItems()function loadProducts()function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
Invisible stuff that happens on page load
Loads stuff into our HTML
(view)
![Page 27: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/27.jpg)
Objects help us to encapsulate code
![Page 28: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/28.jpg)
function initialize()function handleAdd()function handleUpdate()
function loadCartItems()function loadProducts()function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
Invisible stuff that happens on page load
Loads stuff into our HTML
(view)
![Page 29: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/29.jpg)
function initialize()
function handleAdd()function handleUpdate()
function loadCartItems()function loadProducts()function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
private private
public
private private
public
private private private
![Page 30: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/30.jpg)
class Initializer{ private $connection;
public function __construct(\PDO $connection) { $this->connection = $connection; }
public function initialize() { session_start(); if (!isset($_SESSION['cart'])) { $this->connection->exec("INSERT INTO cart () VALUES ()"); $_SESSION['cart'] = $this->connection->lastInsertId(); } $this->handleAdd(); $this->handleUpdate(); }
private function handleAdd() { … } private function handleUpdate() { … }}
https://github.com/zymsys/solid/blob/02/cart.php
![Page 31: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/31.jpg)
class ViewData { private $connection;
public function __construct(\PDO $connection) { $this->connection = $connection; }
private function loadCartItems() { … } private function loadProducts(){ … } private function loadProvinces(){ … } private function calculateCartSubtotal($cartItems, $products) { … } private function calculateCartTaxes($cartItems, $products, $taxrate) { … } public function buildViewData() { … }}
https://github.com/zymsys/solid/blob/02/cart.php
![Page 32: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/32.jpg)
public function buildViewData(){ $viewData = [ 'cartItems' => $this->loadCartItems(), 'products' => $this->loadProducts(), 'provinces' => $this->loadProvinces(), 'provinceCode' => isset($_GET['province']) ? $_GET['province'] : 'ON', //Default to GTA-PHP's home ];
foreach ($viewData['provinces'] as $province) { if ($province['code'] === $viewData['provinceCode']) { $viewData['province'] = $province; } }
$viewData['subtotal'] = $this->calculateCartSubtotal($viewData['cartItems'], $viewData['products']); $viewData['taxes'] = $this->calculateCartTaxes($viewData['cartItems'], $viewData['products'], $viewData['province']['taxrate']); $viewData['total'] = $viewData['subtotal'] + $viewData['taxes'];
return $viewData;}
https://github.com/zymsys/solid/blob/02/cart.php
![Page 33: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/33.jpg)
SOLIDFewer Gremlins in your Code
![Page 34: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/34.jpg)
SRPSingle Responsibility Principal
![Page 35: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/35.jpg)
Responsibility?
![Page 36: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/36.jpg)
Business Responsibility
not code
![Page 37: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/37.jpg)
function initialize()function handleAdd()function handleUpdate()
function loadCartItems()function loadProducts()function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
Invisible stuff that happens on page load
Loads stuff into our HTML
(view)
![Page 38: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/38.jpg)
function initialize()
function handleAdd()function handleUpdate()function loadCartItems()
function loadProducts()
function loadProvinces()
function calculateCartSubtotal($cartItems, $products)function calculateCartTaxes($cartItems, $products, $taxrate)
function buildViewData()
Sales
Accounting
Inventory
Application / IT
![Page 39: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/39.jpg)
class Application{ private $connection;
public function __construct() { $this->connection = new \PDO('mysql:host=localhost;dbname=solid', 'root', ''); $this->inventory = new Inventory($this->connection); $this->sales = new Sales($this->connection); $this->accounting = new Accounting($this->connection); }
public function initialize() { session_start(); if (!isset($_SESSION['cart'])) { $this->connection->exec("INSERT INTO cart VALUES ()"); $_SESSION['cart'] = $this->connection->lastInsertId(); } $this->handlePost(); }
https://github.com/zymsys/solid/blob/03/cart.php
![Page 40: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/40.jpg)
public function buildViewData() { $viewData = [ 'cartItems' => $this->sales->loadCartItems(), 'products' => $this->inventory->loadProducts(), 'provinces' => $this->accounting->loadProvinces(), 'provinceCode' => isset($_GET['province']) ? $_GET['province'] : 'ON', //Default to GTA-PHP's home ];
foreach ($viewData['provinces'] as $province) { if ($province['code'] === $viewData['provinceCode']) { $viewData['province'] = $province; } }
$viewData['subtotal'] = $this->accounting-> calculateCartSubtotal($viewData['cartItems'], $viewData['products']); $viewData['taxes'] = $this->accounting-> calculateCartTaxes($viewData['cartItems'], $viewData['products'], $viewData['province']['taxrate']); $viewData['total'] = $viewData['subtotal'] + $viewData['taxes'];
return $viewData; }
//Class Application Continued…
https://github.com/zymsys/solid/blob/03/cart.php
![Page 41: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/41.jpg)
private function handlePost() { if (isset($_POST['addproduct'])) { $this->sales->addProductToCart( $_SESSION['cart'], $_POST['addproduct'], $_POST['quantity'] ); } if (isset($_POST['update'])) { $this->sales->modifyProductQuantityInCart( $_SESSION['cart'], $_POST['update'], $_POST['quantity'] ); } }}
//Class Application Continued…
https://github.com/zymsys/solid/blob/03/cart.php
![Page 42: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/42.jpg)
class Inventory { private $connection;
public function __construct(\PDO $connection) { $this->connection = $connection; }
public function loadProducts() { $products = []; $result = $this->connection->query("SELECT * FROM product"); foreach ($result as $product) { $products[$product['id']] = $product; } return $products; }}
https://github.com/zymsys/solid/blob/03/cart.php
![Page 43: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/43.jpg)
class Sales { private $connection;
public function __construct(\PDO $connection) { $this->connection = $connection; }
public function addProductToCart($cartId, $productId, $quantity) { $sql = "INSERT INTO cartitem (cart, product, quantity) VALUES (:cart, :product, :quantity) ON DUPLICATE KEY UPDATE quantity = quantity + :quantity"; $parameters = [ 'cart' => $cartId, 'product' => $productId, 'quantity' => $quantity, ]; $statement = $this->connection->prepare($sql); $statement->execute($parameters); }
https://github.com/zymsys/solid/blob/03/cart.php
![Page 44: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/44.jpg)
public function modifyProductQuantityInCart($cartId, $productId, $quantity) { $sql = "UPDATE cartitem SET quantity=:quantity WHERE cart=:cart and product=:product"; $parameters = [ 'cart' => $cartId, 'product' => $productId, 'quantity' => $quantity, ]; $statement = $this->connection->prepare($sql); $statement->execute($parameters); }
public function loadCartItems() { $statement = $this->connection->prepare("SELECT * FROM cartitem WHERE cart=:cart AND quantity <> 0"); $statement->execute(['cart' => $_SESSION['cart']]); return $statement->fetchAll(); }}
//Class Sales Continued…
https://github.com/zymsys/solid/blob/03/cart.php
![Page 45: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/45.jpg)
class Accounting { private $connection;
public function __construct(\PDO $connection) { $this->connection = $connection; }
public function loadProvinces() { $provinces = []; $result = $this->connection->query("SELECT * FROM province ORDER BY name"); foreach ($result as $row) { $provinces[$row['code']] = $row; } return $provinces; }
public function calculateCartSubtotal($cartItems, $products) { $subtotal = 0;
foreach ($cartItems as $cartItem) { $product = $products[$cartItem['product']]; $subtotal += $cartItem['quantity'] * $product['price']; }
return $subtotal; }
https://github.com/zymsys/solid/blob/03/cart.php
![Page 46: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/46.jpg)
public function calculateCartTaxes($cartItems, $products, $taxrate) { $taxable = 0;
foreach ($cartItems as $cartItem) { $product = $products[$cartItem['product']]; $taxable += $product['taxes'] ? $cartItem['quantity'] * $product['price'] : 0; } return $taxable * $taxrate / 100; }
}
//Class Accounting Continued…
https://github.com/zymsys/solid/blob/03/cart.php
![Page 47: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/47.jpg)
OCPOpen/Closed Principle
![Page 48: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/48.jpg)
Open and Closed?
![Page 49: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/49.jpg)
Open to Extension
Closed to Modification
![Page 50: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/50.jpg)
New requirement:10% off orders over
$100
![Page 51: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/51.jpg)
Where does the code go?
![Page 52: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/52.jpg)
First we need to understand
inheritance and polymorphism
![Page 53: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/53.jpg)
Classes can extend other classes
![Page 54: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/54.jpg)
class AccountingStrategy { private $description;
public function __construct($description) { $this->description = $description; }
public function getAdjustment($cartItems) { return false; }
public function getDescription() { return $this->description; }}
https://github.com/zymsys/solid/blob/04/cart.php
![Page 55: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/55.jpg)
class TaxAccountingStrategy extends AccountingStrategy { private $products; private $taxRate;
public function __construct($products, $province) { parent::__construct($province['name'] . ' taxes at ' . $province['taxrate'] . '%:'); $this->products = $products; $this->taxRate = $province['taxrate']; }
public function getAdjustment($cartItems) { $taxable = 0;
foreach ($cartItems as $cartItem) { $product = $this->products[$cartItem['product']]; $taxable += $product['taxes'] ? $cartItem['quantity'] * $product['price'] : 0; } return $taxable * $this->taxRate / 100; }}
https://github.com/zymsys/solid/blob/04/cart.php
![Page 56: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/56.jpg)
TaxAccountingStrategy is an
AccountingStrategy.
![Page 57: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/57.jpg)
public function initialize(){ session_start(); if (!isset($_SESSION['cart'])) { $this->connection->exec("INSERT INTO cart VALUES ()"); $_SESSION['cart'] = $this->connection->lastInsertId(); } $this->handlePost();
$this->products = $this->inventory->loadProducts(); $provinceRepository = new ProvinceRepository($this->connection, isset($_GET['province']) ? $_GET['province'] : 'ON'); $this->provinces = $provinceRepository->loadProvinces(); $this->selectedProvince = $provinceRepository->getSelectedProvince();
$this->accounting->addStrategy( new TaxAccountingStrategy( $this->products, $provinceRepository->getSelectedProvince() ) );}
public function initialize(){ session_start(); if (!isset($_SESSION['cart'])) { $this->connection->exec("INSERT INTO cart VALUES ()"); $_SESSION['cart'] = $this->connection->lastInsertId(); } $this->handlePost();}
https://github.com/zymsys/solid/blob/04/cart.php
![Page 58: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/58.jpg)
public function buildViewData(){ $viewData = [ 'cartItems' => $this->sales->loadCartItems(), 'products' => $this->inventory->loadProducts(), 'provinces' => $this->accounting->loadProvinces(), 'provinceCode' => isset($_GET['province']) ? $_GET['province'] : 'ON', //Default to GTA-PHP's home ];
public function buildViewData(){ $cartItems = $this->sales->loadCartItems(); $viewData = [ 'cartItems' => $cartItems, 'products' => $this->products, 'provinces' => $this->provinces, 'adjustments' => $this->accounting->applyAdjustments($cartItems), 'provinceCode' => $this->selectedProvince['code'], ];
Done in initialize()
now
Used Twice
New!
Start of buildViewData()
https://github.com/zymsys/solid/blob/04/cart.php
![Page 59: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/59.jpg)
End of buildViewData()
$viewData['subtotal'] = $this->accounting-> calculateCartSubtotal($viewData['cartItems'], $viewData['products']); $viewData['taxes'] = $this->accounting-> calculateCartTaxes($viewData['cartItems'], $viewData['products'], $viewData['province']['taxrate']); $viewData['total'] = $viewData['subtotal'] + $viewData['taxes'];
return $viewData;}
$viewData['subtotal'] = $this->accounting-> calculateCartSubtotal($viewData['cartItems'], $viewData['products']); $viewData['total'] = $viewData['subtotal'] + $this->accounting->getAppliedAdjustmentsTotal();
return $viewData;}
Taxes are handled by adjustmentsand removed as a specific item in
the view’s data.
https://github.com/zymsys/solid/blob/04/cart.php
![Page 60: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/60.jpg)
// loadProvinces used to live in the Accounting classclass ProvinceRepository{ private $connection; private $provinces = null; private $selectedProvince; private $selectedProvinceCode;
public function __construct(\PDO $connection, $selectedProvinceCode) { $this->connection = $connection; $this->selectedProvinceCode = $selectedProvinceCode; }
public function loadProvinces() { … } // Now sets $selectedProvince
public function getProvinces() { return is_null($this->provinces) ? $this->loadProvinces() : $this->provinces; }
public function getSelectedProvince() { return $this->selectedProvince; }}
https://github.com/zymsys/solid/blob/04/cart.php
![Page 61: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/61.jpg)
Remove calculateCartTaxes and add AccountingStrategy
class Accounting { private $connection; private $strategies = []; private $appliedAdjustments = 0;
public function __construct(\PDO $connection) { $this->connection = $connection; }
public function calculateCartSubtotal($cartItems, $products) { … } // No change
public function addStrategy(AccountingStrategy $strategy) { $this->strategies[] = $strategy; }
https://github.com/zymsys/solid/blob/04/cart.php
![Page 62: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/62.jpg)
public function applyAdjustments($cartItems) { $adjustments = []; foreach ($this->strategies as $strategy) { $adjustment = $strategy->getAdjustment($cartItems); if ($adjustment) { $this->appliedAdjustments += $adjustment; $adjustments[] = [ 'description' => $strategy->getDescription(), 'adjustment' => $adjustment, ]; } } return $adjustments; }
public function getAppliedAdjustmentsTotal() { return $this->appliedAdjustments; }}
//Class Accounting Continued…
https://github.com/zymsys/solid/blob/04/cart.php
![Page 63: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/63.jpg)
<?php foreach ($viewData['adjustments'] as $adjustment): ?><tr> <td><!-- Name --></td> <td style="text-align: right"> <?php echo $adjustment['description']; ?> </td> <td> <?php echo number_format($adjustment['adjustment'] / 100, 2); ?> </td></tr><?php endforeach; ?>
![Page 64: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/64.jpg)
<?phpfor ($adjustmentIndex = 0; $adjustmentIndex < count($viewData['adjustments']); $adjustmentIndex += 1): $adjustment = $viewData['adjustments'][$adjustmentIndex];?><input type="hidden" name="item<?php echo $adjustmentIndex; ?>" value="<?php echo $adjustment['description'] . '|' . number_format($adjustment['adjustment'] / 100, 2); ?>"><?php endfor; ?>
![Page 65: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/65.jpg)
Now we can add DiscountAccountingStrategy
without modifying Accounting or the view
![Page 66: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/66.jpg)
class DiscountAccountingStrategy extends AccountingStrategy { private $products;
public function __construct($products) { parent::__construct("Discount for orders over $100"); $this->products = $products; }
public function getAdjustment($cartItems) { $total = array_reduce($cartItems, function ($carry, $item) { $product = $this->products[$item['product']]; return $carry + $item['quantity'] * $product['price']; }, 0); return $total > 10000 ? ($total / -10) : false; }}
https://github.com/zymsys/solid/blob/04/cart.php
![Page 67: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/67.jpg)
In Application::initialize(
)$this->accounting->addStrategy( new DiscountAccountingStrategy($this->products));
https://github.com/zymsys/solid/blob/04/cart.php
![Page 68: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/68.jpg)
LSPLiskov Substitution Principal
![Page 69: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/69.jpg)
If a class is a thing, it should act like that
thing. must
![Page 70: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/70.jpg)
Square is a Rectangle?
![Page 71: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/71.jpg)
Ways to break LSP• Throw a new exception• Add requirements to parameters
• requiring a more specific type• Return an unexpected type
• returning a less specific type• Do anything that would be unexpected if
used as a stand-in for an ancestor
![Page 72: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/72.jpg)
DIPDependency Inversion Principle
![Page 73: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/73.jpg)
Yeah smarty-pants, the D does come
before the I
![Page 74: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/74.jpg)
Classes can implement interfaces,
and can depend on interfaces
![Page 75: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/75.jpg)
Our Dependencies
![Page 76: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/76.jpg)
Dependency Inversion
![Page 77: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/77.jpg)
We can scrap the AccountingStrategy class and
add…interface AccountingStrategyInterface{ public function getDescription(); public function getAdjustment($cartItems);}
https://github.com/zymsys/solid/blob/06/cart.php
![Page 78: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/78.jpg)
Because Application creates concrete classes there’s little to be gained by
adding other interfacespublic function __construct(){ $this->connection = new \PDO('mysql:host=localhost;dbname=solid', 'root', ''); $this->inventory = new Inventory($this->connection); $this->sales = new Sales($this->connection); $this->accounting = new Accounting($this->connection);}
Dependency Injection Containers would solve this, but are a topic for
another talk.
https://github.com/zymsys/solid/blob/06/cart.php
![Page 79: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/79.jpg)
ISPInterface Segregation Principal
![Page 80: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/80.jpg)
If we follow SRP and interfaces describe
classes, what’s left to segregate?
![Page 81: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/81.jpg)
Code Responsibilities
![Page 82: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/82.jpg)
Imagine an interface to our Sales class
interface SalesInterface{
public function addProductToCart($cartId, $productId, $quantity); public function modifyProductQuantityInCart($cartId, $productId, $quantity);
public function loadCartItems();}
![Page 83: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/83.jpg)
Imagine an interface to our Sales class
interface SalesWriterInterface{
public function addProductToCart($cartId, $productId, $quantity); public function modifyProductQuantityInCart($cartId, $productId, $quantity);
public function loadCartItems();
}
interface SalesReaderInterface{
}
![Page 84: Zero to SOLID](https://reader031.vdocuments.net/reader031/viewer/2022011722/587354c21a28ab56378b7117/html5/thumbnails/84.jpg)
Last words
Resist OverengineeringSimplify don’t complicate
Pragmatic not Dogmatic
Questions?