Skip to content

Commit

Permalink
Merge pull request #8 from Safemood/events-tracking
Browse files Browse the repository at this point in the history
Events tracking
  • Loading branch information
Safemood committed Jan 26, 2024
2 parents 0161218 + 0b2ec68 commit 1627d13
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/discountify.php
Expand Up @@ -9,4 +9,5 @@
],
'global_discount' => 0,
'global_tax_rate' => 0,
'fire_events' => env('DISCOUNTIFY_FIRE_EVENTS', true),
];
5 changes: 5 additions & 0 deletions src/Discountify.php
Expand Up @@ -5,6 +5,7 @@
use Safemood\Discountify\Concerns\HasCalculations;
use Safemood\Discountify\Concerns\HasDynamicFields;
use Safemood\Discountify\Contracts\DiscountifyInterface;
use Safemood\Discountify\Events\DiscountAppliedEvent;

/**
* Class Discountify
Expand Down Expand Up @@ -66,6 +67,10 @@ public function evaluateConditions(): float
function ($discount, $condition) {
$result = is_callable($condition['condition']) ? $condition['condition']($this->items) : $condition['condition'];

if (config('discountify.fire_events')) {
event(new DiscountAppliedEvent($condition['slug'], $condition['discount'], $condition['condition']));
}

return $discount + match (true) {
$result === true => $this->calculateSubtotal() * ($condition['discount'] / 100),
default => $condition['discount'],
Expand Down
13 changes: 13 additions & 0 deletions src/Events/DiscountAppliedEvent.php
@@ -0,0 +1,13 @@
<?php

namespace Safemood\Discountify\Events;

class DiscountAppliedEvent
{
public function __construct(
public string $slug,
public int $discount,
public mixed $condition
) {
}
}
30 changes: 30 additions & 0 deletions tests/DiscountifyTest.php
@@ -1,9 +1,11 @@
<?php

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Safemood\Discountify\ConditionManager;
use Safemood\Discountify\Discountify;
use Safemood\Discountify\Events\DiscountAppliedEvent;
use Safemood\Discountify\Exceptions\DuplicateSlugException;
use Safemood\Discountify\Facades\Condition;
use Safemood\Discountify\Facades\Discountify as DiscountifyFacade;
Expand Down Expand Up @@ -332,3 +334,31 @@

$this->assertFalse(File::exists($filePath));
});

it('fires the DiscountAppliedEvent when conditions are met and event firing is enabled', function () {
Event::fake();

Config::set('discountify.fire_events', true);

Condition::define('test_condition', fn () => true, 10);

DiscountifyFacade::setItems($this->items);

DiscountifyFacade::total();

Event::assertDispatched(DiscountAppliedEvent::class);
});

it('does not fire the DiscountAppliedEvent when event firing is disabled', function () {
Event::fake();

Config::set('discountify.fire_events', false);

Condition::define('test_condition', fn () => true, 10);

DiscountifyFacade::setItems($this->items);

DiscountifyFacade::total();

Event::assertNotDispatched(DiscountAppliedEvent::class);
});

0 comments on commit 1627d13

Please sign in to comment.