Skip to content

Commit

Permalink
Added AdjusterAliases + mapping of adjuster FQCN <-> aliases at adj…
Browse files Browse the repository at this point in the history
…ustment creation
  • Loading branch information
fulopattila122 committed Mar 5, 2024
1 parent 92ca08a commit d251577
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
- Added the `Merchant` interface
- Added the `DefaultTaxCalculator` class - calculates simply by rate
- Added the `CalculateTaxes` listener to cart update and shipping address change events
- Added the `AdjusterAliases` class that for decoupling FQCNs from the database
- Added automatic mapping of adjuster FQCN <-> aliases when saving an adjustment into the DB and when calling the `getAdjuster()` method
- BC: Added the `?CheckoutSubject` return type to the `getCart()` method of the `Checkout` interface
- BC: Changed `Checkout::getShippingAddress()` return type to be nullable
- BC: Added the void return type to `Checkout::setShippingAddress()`
Expand Down
56 changes: 56 additions & 0 deletions src/Adjustments/Adjusters/AdjusterAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* Contains the AdjusterAliases class.
*
* @copyright Copyright (c) 2024 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2024-03-05
*
*/

namespace Vanilo\Adjustments\Adjusters;

final class AdjusterAliases
{
private static array $map = [
'discount' => SimpleDiscount::class,
'fee' => SimpleFee::class,
'shipping_fee' => SimpleShippingFee::class,
'tax' => SimpleTax::class,
];

public static function add(string $alias, string $fqcn): void
{
self::$map[$alias] = $fqcn;
}

public static function isAnAlias(string $value): bool
{
return isset(self::$map[$value]);
}

public static function isNotAnAlias(string $value): bool
{
return !self::isAnAlias($value);
}

public static function aliasByClass(string $fqcn): ?string
{
foreach (self::$map as $alias => $class) {
if ($class === $fqcn) {
return $alias;
}
}

return null;
}

public static function classByAlias(string $alias): ?string
{
return self::$map[$alias] ?? null;
}
}
2 changes: 2 additions & 0 deletions src/Adjustments/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Added PHP 8.3 Support
- Changed minimum Laravel version to v10.38.2
- Changed minimal Enum requirement to v4.2
- Added the `AdjusterAliases` class that for decoupling FQCNs from the database
- Added automatic mapping of adjuster FQCN <-> aliases when saving an adjustment into the DB and when calling the `getAdjuster()` method
- Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection
- BC: Added the `deleteByType()` method to the `AdjustmentCollection` interface + both implementation
- BC: Changed the behavior of `AdjustmentCollection::total()`:
Expand Down
13 changes: 10 additions & 3 deletions src/Adjustments/Models/Adjustment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Konekt\Enum\Eloquent\CastsEnums;
use Vanilo\Adjustments\Adjusters\AdjusterAliases;
use Vanilo\Adjustments\Contracts\Adjustable;
use Vanilo\Adjustments\Contracts\Adjuster;
use Vanilo\Adjustments\Contracts\Adjustment as AdjustmentContract;
Expand Down Expand Up @@ -72,6 +73,9 @@ public static function boot()
if (null === $model->data) {
$model->data = [];
}
if (AdjusterAliases::isNotAnAlias($model->adjuster) && $alias = AdjusterAliases::aliasByClass($model->adjuster)) {
$model->adjuster = $alias;
}
});
}

Expand All @@ -98,9 +102,12 @@ public function setAdjustable(Adjustable $adjustable): void

public function getAdjuster(): Adjuster
{
$adjusterType = $this->adjuster;
if (class_exists($this->adjuster)) {
return forward_static_call([$adjusterType, 'reproduceFromAdjustment'], $this);
$adjusterClass = match (AdjusterAliases::isAnAlias($this->adjuster)) {
true => AdjusterAliases::classByAlias($this->adjuster),
default => $this->adjuster,
};
if (class_exists($adjusterClass)) {
return forward_static_call([$adjusterClass, 'reproduceFromAdjustment'], $this);
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/Adjustments/Tests/Unit/AdjustmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,33 @@ public function it_is_a_credit_if_it_decreases_the_total()
$this->assertEquals(-20, $adjustment->getAmount());
$this->assertTrue($adjustment->isCredit());
}

/** @test */
public function it_converts_the_adjuster_fqcn_into_its_alias_when_saving_into_the_database_if_an_alias_exists()
{
$adjustment = Adjustment::create([
'type' => AdjustmentType::TAX,
'adjustable_type' => 'order',
'adjustable_id' => 1,
'adjuster' => SimpleShippingFee::class,
'title' => 'Shipping Fee',
])->fresh();

$this->assertEquals('shipping_fee', $adjustment->adjuster);
}

/** @test */
public function the_adjuster_gets_properly_resolved_even_if_it_is_saved_as_an_alias()
{
$adjustment = Adjustment::create([
'type' => AdjustmentType::TAX,
'adjustable_type' => 'order',
'adjustable_id' => 1,
'adjuster' => 'shipping_fee',
'title' => 'Shipping Fee',
])->fresh();

$this->assertEquals('shipping_fee', $adjustment->adjuster);
$this->assertInstanceOf(SimpleShippingFee::class, $adjustment->getAdjuster());
}
}
3 changes: 3 additions & 0 deletions src/Foundation/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Konekt\Address\Contracts\Address as AddressContract;
use Konekt\Concord\BaseBoxServiceProvider;
use Konekt\Customer\Contracts\Customer as CustomerContract;
use Vanilo\Adjustments\Adjusters\AdjusterAliases;
use Vanilo\Cart\Contracts\Cart as CartContract;
use Vanilo\Cart\Contracts\CartItem as CartItemContract;
use Vanilo\Cart\Models\CartProxy;
Expand Down Expand Up @@ -46,6 +47,7 @@
use Vanilo\Foundation\Models\Taxon;
use Vanilo\Foundation\Models\Taxonomy;
use Vanilo\Foundation\Shipping\FlatFeeCalculator;
use Vanilo\Foundation\Shipping\PaymentDependentShippingFee;
use Vanilo\Foundation\Shipping\PaymentDependentShippingFeeCalculator;
use Vanilo\MasterProduct\Contracts\MasterProduct as MasterProductContract;
use Vanilo\MasterProduct\Contracts\MasterProductVariant as MasterProductVariantContract;
Expand Down Expand Up @@ -113,6 +115,7 @@ public function boot()

ShippingFeeCalculators::register(FlatFeeCalculator::ID, FlatFeeCalculator::class);
ShippingFeeCalculators::register(PaymentDependentShippingFeeCalculator::ID, PaymentDependentShippingFeeCalculator::class);
AdjusterAliases::add(PaymentDependentShippingFee::ALIAS, PaymentDependentShippingFee::class);

// Use the foundation's extended order factory
$this->app->bind(OrderFactoryContract::class, OrderFactory::class);
Expand Down
2 changes: 2 additions & 0 deletions src/Foundation/Shipping/PaymentDependentShippingFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

class PaymentDependentShippingFee implements Adjuster
{
public const ALIAS = 'payment_dependent_shipping_fee';

use HasWriteableTitleAndDescription;
use IsLockable;
use IsNotIncluded;
Expand Down

0 comments on commit d251577

Please sign in to comment.