Skip to content

Commit

Permalink
Refactoring Promotion Action Types from Configurable -> Schematized -…
Browse files Browse the repository at this point in the history
… IN PROG
  • Loading branch information
fulopattila122 committed Jul 15, 2024
1 parent aff7240 commit 1765953
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 45 deletions.
36 changes: 7 additions & 29 deletions src/Promotion/Actions/CartFixedDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@

class CartFixedDiscount implements PromotionActionType
{
private ?array $configuration = null;
public const DEFAULT_ID = 'cart_fixed_discount';

public static function getName(): string
{
return __('Cart fixed discount');
return __('Cart Fixed Discount');
}

public static function getID(): string
{
return 'cart_fixed_discount';
}

public function adjust(object $subject): Adjuster
public function getAdjuster(array $configuration): Adjuster
{
if (!$subject instanceof Cart) {
throw new \InvalidArgumentException('Subject must be an instance of ' . Cart::class);
Expand All @@ -35,30 +30,13 @@ public function adjust(object $subject): Adjuster
return new SimpleDiscount($this->getConfiguration()['discount_amount'] / 100 * $subject->total());
}

public function getSchema(): ?Schema
{
return Expect::structure(['discount_amount' => Expect::int(0)->required()]);
}

public function setConfiguration(array $configuration): self
public function getSchema(): Schema
{
if ($this->getSchema()) {
$configuration = (new Processor())->process($this->getSchema(), $configuration);
}

$this->configuration = (array) $configuration;

return $this;
return Expect::structure(['discount_amount' => Expect::float(0)->required()])->castTo('array');
}

public function getConfiguration(): ?array
public function getSchemaSample(array $mergeWith = null): array
{
$configuration = $this->configuration;

if ($this->getSchema()) {
$configuration = (new Processor())->process($this->getSchema(), $configuration);
}

return (array) $configuration;
return ['discount_amount' => 19.99];
}
}
2 changes: 1 addition & 1 deletion src/Promotion/Contracts/PromotionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface PromotionAction extends Configurable
{
public function getActionType(): PromotionActionType;

public function executeActionType(object $subject): Adjustable;
public function execute(object $subject): Adjustable;
}
15 changes: 4 additions & 11 deletions src/Promotion/Contracts/PromotionActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@

namespace Vanilo\Promotion\Contracts;

use Nette\Schema\Schema;
use Konekt\Extend\Contracts\Registerable;
use Vanilo\Adjustments\Contracts\Adjuster;
use Vanilo\Contracts\Schematized;

interface PromotionActionType
interface PromotionActionType extends Schematized, Registerable
{
public static function getName(): string;

public static function getID(): string;

public function adjust(object $subject): Adjuster;

public function getSchema(): ?Schema;

public function setConfiguration(array $configuration): self;

public function getConfiguration(): ?array;
public function getAdjuster(array $configuration): Adjuster;
}
3 changes: 2 additions & 1 deletion src/Promotion/Models/PromotionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Nette\Schema\Schema;
use Vanilo\Adjustments\Contracts\Adjustable;
use Vanilo\Promotion\Contracts\Promotion;
use Vanilo\Promotion\Contracts\PromotionAction as PromotionActionContract;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function getActionType(): PromotionActionType
// TODO: Implement getActionType() method.
}

public function executeActionType(object $subject): Adjustable
public function execute(object $subject): Adjustable
{
// TODO: Implement executeActionType() method.
}
Expand Down
6 changes: 3 additions & 3 deletions src/Promotion/PromotionActionTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public static function register(string $id, string $class)

public static function make(string $id, array $parameters = []): PromotionActionType
{
$gwClass = self::getClassOf($id);
$class = self::getClassOf($id);

if (null === $gwClass) {
if (null === $class) {
throw new InexistentPromotionActionException(
"No action is registered with the id `$id`."
);
}

return app()->make($gwClass);
return app()->make($class, $parameters);
}
}

0 comments on commit 1765953

Please sign in to comment.