Skip to content

Commit

Permalink
finish rewar points calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 committed Apr 5, 2024
1 parent 83c4622 commit 55d8a03
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config/rewards.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Specify which class to use for calculating reward points.
|
*/
'reward_point_calculator' => \Dystcz\LunarRewards\Domain\Rewards\Actions\RewardPointsCalculator::class,
'reward_point_calculator' => \Dystcz\LunarRewards\Domain\Rewards\Calculators\RewardPointsCalculator::class,

/*
|--------------------------------------------------------------------------
Expand Down
44 changes: 0 additions & 44 deletions src/Domain/Rewards/Actions/RewardPointsCalculator.php

This file was deleted.

79 changes: 79 additions & 0 deletions src/Domain/Rewards/Calculators/RewardPointsCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Dystcz\LunarRewards\Domain\Rewards\Calculators;

use Dystcz\LunarRewards\Domain\Rewards\Contracts\RewardPointsCalculator as RewardPointsCalculatorContract;
use Illuminate\Support\Facades\Config;
use Lunar\Models\Cart;
use Lunar\Models\Order;

class RewardPointsCalculator implements RewardPointsCalculatorContract
{
protected int|float $coefficient;

protected Order|Cart $model;

/**
* {@inheritDoc}
*/
public function __construct(Order|Cart $model)
{
$this->model = $model;

$this->setCoefficient(Config::get('lunar-rewards.reward_point_coefficient', 1));
}

/**
* {@inheritDoc}
*/
public function calculate(): int
{
if (! $this->getModel()) {
throw new \Exception('No model set for calculating reward points.');
}

$currency = $this->getModel()->currency;
$exchangeRate = $currency->exchange_rate;
$subTotal = $this->getModel()->sub_total ?? $this->getModel()->subTotal ?? null;
$subTotalValue = $subTotal ? $subTotal->value : 0;
$coefficient = $this->getCoefficient();

$points = (int) round($subTotalValue * $exchangeRate * $coefficient / 100);

return $points;
}

/**
* Create a new instance of the calculator for the given model.
*/
public static function for(Order|Cart $model): self
{
return new static($model);
}

/**
* Get the model on which the reward points are calculated.
*/
public function getModel(): Order|Cart|null
{
return $this->model;
}

/**
* Set the coefficient for calculating reward points.
*/
public function setCoefficient(int|float $coefficient): self
{
$this->coefficient = (float) $coefficient;

return $this;
}

/**
* Get the coefficient used for calculating reward points.
*/
public function getCoefficient(): float
{
return $this->coefficient;
}
}
19 changes: 19 additions & 0 deletions src/Domain/Rewards/Contracts/RewardPointsCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Dystcz\LunarRewards\Domain\Rewards\Contracts;

use Lunar\Models\Cart;
use Lunar\Models\Order;

interface RewardPointsCalculator
{
/**
* Set the model on which the reward points are calculated.
*/
public function __construct(Order|Cart $model);

/**
* Calculate the reward points for the given order.
*/
public function calculate(): int;
}
8 changes: 8 additions & 0 deletions src/LunarRewardsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function register(): void
'lunar-rewards',
fn () => new LunarRewards,
);

$this->app->singleton(
\Dystcz\LunarRewards\Domain\Rewards\Contracts\RewardPointsCalculator::class,
fn () => new (Config::get(
'lunar-rewards.reward_point_calculator',
\Dystcz\LunarRewards\Domain\Rewards\Calculators\RewardPointsCalculator::class,
)),
);
}

/**
Expand Down

0 comments on commit 55d8a03

Please sign in to comment.