File: D:/HostingSpaces/SBogers10/helder.komma.pro/app/Komma/Shop/Discounts/Actions/ModifyPriceAction.php
<?php
namespace App\Komma\Shop\Discounts\Actions;
class ModifyPriceAction extends AbstractDiscountAction
{
public const None = 0;
public const MethodPercentageWise = 1;
public const MethodAbsolute = 2;
public const MethodAbsoluteForQuantity = 3;
/**
* @var int $method The method to use. This must be one of the Method constant values.
*/
protected $method;
/**
* @var string $modificationValue The value for the method to modify the price.
*/
private $modificationValue;
public function __construct($value,$modificationMethod)
{
$actionSettings = $value.','.$modificationMethod;
parent::__construct($actionSettings);
}
/**
* Create a new instance from the settings from a discount model
*
* @param string $discountActionSettings
* @return ModifyPriceAction
*/
public static function newInstanceFromDiscountSettings(string $discountActionSettings):AbstractDiscountAction
{
$settings = ModifyPriceAction::returnDissectedValueIfValid($discountActionSettings);
return new self($settings[0], $settings[1]);
}
/**
* Dissect a value that comes from a discount into something that the DiscountAction understands
*
* @param string $discountValue
* @return bool
*/
protected function dissectDiscountValue(string $discountValue): bool
{
$discountValueParts = ModifyPriceAction::returnDissectedValueIfValid($discountValue);
$this->assignMethodIfValid($discountValueParts[1]);
$this->assignModificationValueIfValid($discountValueParts[0]);
return false;
}
/**
* Checks if the value delimited string is correctly defined and returns the separate values
*
* @param $discountValue
* @return array
*/
private static function returnDissectedValueIfValid($discountValue): array
{
$discountValueParts = explode(',', $discountValue);
if (!count($discountValueParts) == 2) throw new \InvalidArgumentException('The value for an ModifyPriceAction must be comma separated like this: <methodValue>,<ModifyPriceAction::methodConstant>. Got: ' . $discountValue);
return $discountValueParts;
}
/**
* Checks if the method really is a valid method and if so stores the value in the method variable. If not throws an exception.
*
* @param $value
*/
private function assignMethodIfValid($value)
{
if (!ModifyPriceAction::MethodAbsolute && !ModifyPriceAction::MethodPercentageWise) throw new \InvalidArgumentException('The method must be one of the method constant values of the ModifyPriceAction: ' . gettype($value));
$this->method = $value;
}
/**
* Checks if the method really is a valid method and if so stores the value in the method variable. If not throws an exception.
*
* @param $value
*/
private function assignModificationValueIfValid($value)
{
if (!is_numeric($value)) throw new \InvalidArgumentException('The method value must be numeric but was: ' . gettype($value));
$this->modificationValue = $value;
}
/**
* Gets the value to modify the price with.
*
*/
public function getModificationValue()
{
return $this->modificationValue;
}
/**
* Receive data from outside, do the action on it, return the modified data.
* For example: Get a price from a shopping cart, modify the price according to what this action represents and return the price to the shopping cart.
*
* @param array $parameters . May be specified as an array or argument1,argument2,argument3
* @return mixed
*/
public function do(...$parameters)
{
if(count($parameters) > 2) throw new \InvalidArgumentException('The ModifyPriceAction::do method only accepts 2 parameters. This must be the price that needs to be modified and optionally the amount of times the action must be executed');
$price = $parameters[0];
$amount = (count($parameters) == 2) ? $parameters[1] : 1; //How many times the price should be modified. Only for the ModifyPriceAction::MethodAbsoluteForQuantity case
/** @var $action ModifyPriceAction */
switch ($this->method)
{
case ModifyPriceAction::MethodAbsolute:
$price -= $this->modificationValue;
break;
case ModifyPriceAction::MethodAbsoluteForQuantity:
for($index = 0; $index < $amount; $index++) $price -= $this->modificationValue;
break;
case ModifyPriceAction::MethodPercentageWise:
$price -= ($price / 100) * $this->modificationValue;
break;
}
return $price;
}
/**
* Returns the method to use for editing the price.
* Use the returned value in combination with the method constants in this class
*/
public function getMethod()
{
return $this->method;
}
/**
* @param int $method
*/
public function setMethod(int $method): void
{
$this->method = $method;
}
/**
* @param string $value
*/
public function setValue(string $value): void
{
$this->value = $value;
}
}