for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace MichaelRubel\Couponables\Traits\Concerns;
use MichaelRubel\Couponables\Exceptions\InvalidCouponTypeException;
trait CalculatesCosts
{
/**
* Calculate the output value based on the coupon type.
*
* @param float $value
* @return float
* @throws InvalidCouponTypeException
*/
public function calc(float $value): float
return match ($this->{static::$bindable->getTypeColumn()}) {
static::TYPE_SUBTRACTION => $this->subtract($value),
MichaelRubel\Couponables...Costs::TYPE_SUBTRACTION
static::TYPE_PERCENTAGE => $this->percentage($value),
MichaelRubel\Couponables...sCosts::TYPE_PERCENTAGE
static::TYPE_FIXED => $this->fixedPrice(),
MichaelRubel\Couponables...ulatesCosts::TYPE_FIXED
default => throw new InvalidCouponTypeException,
};
}
* Apply the "Subtraction" calculation strategy.
* @param float $cost
private function subtract(float $cost): float
return $cost - $this->{static::$bindable->getValueColumn()};
* Apply the "Percentage" calculation strategy.
private function percentage(float $value): float
return ($this->{static::$bindable->getValueColumn()} / 100) * $value;
* Apply the "Fixed Price" calculation strategy.
private function fixedPrice(): float
return (float) $this->{static::$bindable->getValueColumn()};