Skip to content

8_refactor_modules #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ca3af23
replace hardcoded currency results into a value object
DevparkAnders Sep 27, 2021
6386b8e
strategy pattern usage.
DevparkAnders Sep 27, 2021
4168be0
strategy pattern usage.
DevparkAnders Sep 27, 2021
d17857e
strategy pattern usage.
DevparkAnders Sep 27, 2021
e467686
code tweaks
DevparkAnders Sep 27, 2021
db9f46e
code tweaks
DevparkAnders Sep 27, 2021
c679d5d
code tweaks
DevparkAnders Sep 27, 2021
b731297
code tweaks
DevparkAnders Sep 27, 2021
89e68ac
code tweaks
DevparkAnders Sep 27, 2021
32277fd
refactoring to decorator
DevparkAnders Sep 27, 2021
1b671b7
refactoring to decorator
DevparkAnders Sep 27, 2021
7f40746
refactoring to decorator
DevparkAnders Sep 27, 2021
78924ea
refactoring to decorator
DevparkAnders Sep 27, 2021
e984a5c
refactoring to decorator
DevparkAnders Sep 27, 2021
cf362f9
small tweaks
DevparkAnders Sep 28, 2021
4e656c6
small tweaks
DevparkAnders Sep 28, 2021
a39ff71
small tweaks of PremiumBox with its price factory
DevparkAnders Sep 28, 2021
92f0ab7
add CustomBox pricing
DevparkAnders Sep 28, 2021
e019d85
add Calc director
DevparkAnders Sep 28, 2021
b88b959
first working version with logic separation by countries
DevparkAnders Sep 28, 2021
0c29892
clean up Price by country classes
DevparkAnders Sep 28, 2021
c52a39d
extract DRY on builders to abstract class
DevparkAnders Sep 28, 2021
2308d16
replace decorator with direct destination methods call
DevparkAnders Sep 28, 2021
b9f6fd1
DRY - remove multiple buiders into one class
DevparkAnders Sep 29, 2021
8bb4f07
make factory more readable
DevparkAnders Sep 29, 2021
fe4e02a
make factory more readable
DevparkAnders Sep 29, 2021
35505a6
add missing method to shipping order interface
DevparkAnders Sep 29, 2021
268817b
extract separate interface for box pricing
DevparkAnders Sep 29, 2021
ae38d48
fix wrong namespaces
DevparkAnders Sep 29, 2021
8c0086f
add new box type for UK
DevparkAnders Sep 29, 2021
a1bdb18
rollback decorator inside builders
DevparkAnders Sep 29, 2021
395c37c
pass boxing properties in a proper way (without friend of my friend r…
DevparkAnders Oct 4, 2021
ba351b0
move abstract calculation builder to proper directory
DevparkAnders Oct 4, 2021
de9d88a
refactor hardcoded values into const variables
DevparkAnders Oct 4, 2021
fee760c
refactor hardcoded values into const variables
DevparkAnders Oct 4, 2021
ef14fca
refactor hardcoded values into const variables
DevparkAnders Oct 4, 2021
dcced5a
DRY - extract defaultBox code
DevparkAnders Oct 4, 2021
57ac040
small tweaks
DevparkAnders Oct 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Contracts/IBoxingCalculation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Contracts;


interface IBoxingCalculation
{
public function calculate(IShippingBox $boxing_properties):IPrice;
}
10 changes: 10 additions & 0 deletions Contracts/ICalculate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Contracts;


interface ICalculate
{
public function calculate():IPrice;
}
10 changes: 10 additions & 0 deletions Contracts/ICalculationsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace App\Contracts;

interface ICalculationsBuilder
{
public function useOrderTotal():ICountryShippingCalc;
public function useShippingDiscounts(ICountryShippingCalc $calculations_component):ICountryShippingCalc;
public function useBoxPricing():IBoxingCalculation;
public function makeCalculations(ICountryShippingCalc $calculations_component, IBoxingCalculation $boxing_component):IPrice;
}
7 changes: 7 additions & 0 deletions Contracts/ICountryShippingCalc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace App\Contracts;

interface ICountryShippingCalc
{
public function calculate(IShippingOrder $order):IPrice;
}
7 changes: 7 additions & 0 deletions Contracts/ICustomBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace App\Contracts;

interface ICustomBox
{
public function setPrice(IPrice $price);
}
10 changes: 10 additions & 0 deletions Contracts/IPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace App\Contracts;

interface IPrice {
public function __construct(float $value);
public function getValue();
public function getCurrencyCode();
public function getFomatedValue();
public function add(IPrice $value):IPrice;
}
10 changes: 10 additions & 0 deletions Contracts/IShippingBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Contracts;


interface IShippingBox
{
public function getType():string;
}
13 changes: 13 additions & 0 deletions Contracts/IShippingOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace App\Contracts;

interface IShippingOrder
{
public function getCountry();
public function getTotalPl();
public function getTotalUk();
public function getTotalUs();
public function getShippingDiscount();

public function getBoxingProperties():IShippingBox;
}
14 changes: 14 additions & 0 deletions Countries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace App;


class Countries
{
public const PL = "PL";
public const UK = "UK";
public const US = "US";
public const NIGERIA = "NIGERIA";

}
13 changes: 13 additions & 0 deletions Currencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace App;


class Currencies
{
public const PLN = "PLN";
public const GBP = "GBP";
public const US = "$";
public const ETH = "ETH";
}
28 changes: 25 additions & 3 deletions Main.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
<?php
namespace App;

use App\Contracts\IShippingBox;
use App\Contracts\IShippingOrder;
use App\Orders\BoxingPropertiesAdapter;
use App\Orders\Order;
use App\Orders\ShippingOrderAdapter;
use App\Shipping\CountryCalculators\CountryCalcFactory;
use App\Shipping\ShippingCostCalculator;
use App\Contracts\IPrice;

class Main
{
public function start($country_code, $total)
public function start($country_code, $total, $discount = 0, $box_type = 'DEFAULT')
{
$order = new Order($country_code, $total);
return $shipping_cost = (new ShippingCostCalculator())->calculate($order);
$immutable_order = $this->getOrder($country_code, $total, $discount, $box_type);
$order_adapter = new ShippingOrderAdapter($immutable_order);
$boxing_properties = new BoxingPropertiesAdapter($immutable_order);

$shipping_cost = $this->makeShippingCalcultions($order_adapter, $boxing_properties);

return $shipping_cost->getFomatedValue();
}

private function getOrder($country_code, $total, $discount = 0, $premium_box = false):Order
{
return new Order($country_code, $total, $discount, $premium_box);
}

private function makeShippingCalcultions(IShippingOrder $order_adapter, IShippingBox $boxing_properties):IPrice
{
$calculator_facade = new ShippingCostCalculator();
return $calculator_facade->calculate($order_adapter, $boxing_properties);
}
}
24 changes: 24 additions & 0 deletions Orders/BoxingPropertiesAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php


namespace App\Orders;


use App\Contracts\IShippingBox;

class BoxingPropertiesAdapter implements IShippingBox
{
private Order $order;

public function __construct(Order $order)
{
$this->order = $order;
}

public function getType(): string
{
//remember we are working with dummy data
//probably in real situtation you will get the type from database using the order_id
return $this->order->boxType();
}
}
15 changes: 13 additions & 2 deletions Orders/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ class Order
{
private $country = "PL";
private $total = 50;
private $shipping_discount = 0;
private $box_type = "DEFAULT";

public function __construct($country_code, $total)
public function __construct($country_code, $total, $shipping_discount, $box_type)
{
$this->country = $country_code;
$this->total = $total;
$this->shipping_discount = $shipping_discount;
$this->box_type = $box_type;
}

public function getCountry()
Expand Down Expand Up @@ -48,7 +52,7 @@ public function getClientDiscount(){

public function getClientShippingDiscountPL()
{
return 10;
return $this->shipping_discount;
}

public function getClientShippingDiscountEU()
Expand All @@ -60,4 +64,11 @@ public function getClientShippingDiscountWORLD()
{
return 0;
}

public function boxType()
{
return $this->box_type;
}
//please assume that there will be a lot more of code inside of this class
//imagine the worst code which you ever seen... this one is worse
}
61 changes: 61 additions & 0 deletions Orders/ShippingOrderAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php


namespace App\Orders;


use App\Contracts\IShippingBox;
use App\Contracts\IShippingClient;
use App\Contracts\IShippingOrder;

class ShippingOrderAdapter implements IShippingOrder
{
private Order $order;
private IShippingBox $boxing;

public function __construct($order)
{
$this->order = $order;
$this->boxing = new BoxingPropertiesAdapter($order);
}

public function getCountry()
{
return $this->order->getCountry();
}

public function getTotalPl()
{
return $this->order->getTotalPl();
}

public function getTotalUk()
{
return $this->order->getTotalUk();
}

public function getTotalUs()
{
return $this->order->getTotalUs();
}

public function getShippingDiscount()
{
switch($this->order->getCountry())
{
case "PL":
return $this->order->getClientShippingDiscountPL();
case "UK":
return $this->order->getClientShippingDiscountEU();
case "US":
return $this->order->getClientShippingDiscountWORLD();
default:
return $this->order->getClientShippingDiscountWORLD();
}
}

public function getBoxingProperties(): IShippingBox
{
return $this->boxing;
}
}
33 changes: 33 additions & 0 deletions Shipping/CalculationsDirector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace App\Shipping;


use App\Contracts\ICalculate;
use App\Contracts\ICalculationsBuilder;
use App\Contracts\IPrice;

class CalculationsDirector implements ICalculate
{
private ICalculationsBuilder $calc_builder;

public function __construct(ICalculationsBuilder $calc_builder)
{
$this->calc_builder = $calc_builder;
}

public function calculate():IPrice
{
//this is the main shipping cost calculation
$calculator = $this->calc_builder->useOrderTotal();

//these has to be always calculated as first after total order value
$discount_decorations = $this->calc_builder->useShippingDiscounts($calculator);

//other calculations
$boxing_decorations = $this->calc_builder->useBoxPricing();

return $this->calc_builder->makeCalculations($discount_decorations, $boxing_decorations);
}
}
30 changes: 30 additions & 0 deletions Shipping/CommonCalculations/AdditionalCalc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace App\Shipping\CommonCalculations;
use \App\Contracts\ICountryShippingCalc;

use App\Contracts\IPrice;
use App\Contracts\IShippingOrder;
use App\Shipping\PriceFactory;

abstract class AdditionalCalc implements ICountryShippingCalc
{

protected ICountryShippingCalc $calc;
protected $price_factory ;

public function __construct(ICountryShippingCalc $calculations_component)
{
$this->calc = $calculations_component;
$this->price_factory = new PriceFactory();
}

public function calculate(IShippingOrder $order): IPrice
{
$shipping_cost = $this->calc->calculate($order);
return $this->decorate($shipping_cost, $order);
}

abstract protected function decorate(IPrice $shipping_cost, IShippingOrder $order): IPrice;
}
29 changes: 29 additions & 0 deletions Shipping/CommonCalculations/BoxPricing/DefaultBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace App\Shipping\CommonCalculations\BoxPricing;

use App\Contracts\IBoxingCalculation;
use App\Contracts\IPrice;
use App\Contracts\IShippingBox;
use App\Shipping\PriceFactory;

abstract class DefaultBox implements IBoxingCalculation
{
protected $price_factory ;

abstract protected function getCountryPrice();
abstract protected function getCurrency();

public function __construct()
{
$this->price_factory = new PriceFactory();
}

public function calculate(IShippingBox $boxing_properties): IPrice
{
return $this->price_factory->create($this->getCurrency(), $this->getCountryPrice());
}


}
Loading