<?php
/**
* Percentage Discount
*
* (c) Bertin van den Ham <b.vandenham@dappr.nl>
*/
namespace VisualMedia\DiscountBundle\Discount\Factory;
use VisualMedia\DiscountBundle\Discount\DiscountFactory;
use VisualMedia\DiscountBundle\Discount\DiscountFactoryInterface;
use VisualMedia\OrderBundle\Service\Cart;
/**
* Percentage Discount
*
* Percentage Discount on the order.
* @example 10% off the order total
*/
class PercentageDiscount extends DiscountFactory implements DiscountFactoryInterface
{
public const NAME = 'percentage_discount';
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return static::NAME;
}
/**
* {@inheritDoc}
*/
public function getDiscount(Cart $cart = null): float
{
if (null === $order = $cart->getOrder()) {
return 0;
}
if (null === $discount = $order->getDiscount()) {
return 0;
}
$cartSubTotal = $cart->getSubtotal();
/** @example: 100 * (10/100 = 0,10) = 10 */
$discountTotal = round($cartSubTotal * ($discount->getValue() / 100), 2);
return $discountTotal;
}
}