+
+ Events dispatched during feed generation
+ Events dispatched during feed generation
+ Events dispatched during feed generation
+
+
+
+
+ Feeds dispatch two events when running the command:
+
+
+
+
+ The FeedStartingEvent is dispatched immediately before the draft file is created.
+
+
+ The FeedFinishedEvent is dispatched right after the feed file generation has completed.
+
+
+
diff --git a/src/Events/FeedFinishedEvent.php b/src/Events/FeedFinishedEvent.php
new file mode 100644
index 0000000..6b81138
--- /dev/null
+++ b/src/Events/FeedFinishedEvent.php
@@ -0,0 +1,22 @@
+ $feed Reference to the feed class
+ * @param string $path Path to the generated feed file
+ * @return void
+ */
+ public function __construct(
+ public string $feed,
+ public string $path,
+ ) {}
+}
diff --git a/src/Events/FeedStartingEvent.php b/src/Events/FeedStartingEvent.php
new file mode 100644
index 0000000..ef33e02
--- /dev/null
+++ b/src/Events/FeedStartingEvent.php
@@ -0,0 +1,20 @@
+ $feed Reference to the feed class
+ * @return void
+ */
+ public function __construct(
+ public string $feed,
+ ) {}
+}
diff --git a/src/Services/GeneratorService.php b/src/Services/GeneratorService.php
index 2438565..ea06574 100644
--- a/src/Services/GeneratorService.php
+++ b/src/Services/GeneratorService.php
@@ -5,6 +5,8 @@
namespace DragonCode\LaravelFeed\Services;
use DragonCode\LaravelFeed\Converters\Converter;
+use DragonCode\LaravelFeed\Events\FeedFinishedEvent;
+use DragonCode\LaravelFeed\Events\FeedStartingEvent;
use DragonCode\LaravelFeed\Exceptions\FeedGenerationException;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Helpers\ConverterHelper;
@@ -15,6 +17,7 @@
use Throwable;
use function blank;
+use function event;
use function get_class;
use function implode;
@@ -28,10 +31,9 @@ public function __construct(
public function feed(Feed $feed, ?OutputStyle $output = null): void
{
- $file = $this->openFile(
- $path = $feed->path()
- );
try {
+ $this->started($feed);
+
$file = $this->openFile(
$path = $feed->path()
);
@@ -46,7 +48,8 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void
$this->release($file, $path);
$this->setLastActivity($feed);
- $this->setLastActivity($feed);
+
+ $this->finished($feed, $path);
} catch (Throwable $e) {
throw new FeedGenerationException(get_class($feed), $e);
}
@@ -162,4 +165,14 @@ protected function progressBar(int $count, ?OutputStyle $output): ?ProgressBar
{
return $output?->createProgressBar($count);
}
+
+ protected function started(Feed $feed): void
+ {
+ event(new FeedStartingEvent(get_class($feed)));
+ }
+
+ protected function finished(Feed $feed, string $path): void
+ {
+ event(new FeedFinishedEvent(get_class($feed), $path));
+ }
}
diff --git a/tests/Feature/Events/SuccessTest.php b/tests/Feature/Events/SuccessTest.php
new file mode 100644
index 0000000..9abaac2
--- /dev/null
+++ b/tests/Feature/Events/SuccessTest.php
@@ -0,0 +1,30 @@
+assertSuccessful()
+ ->run();
+
+ getAllFeeds()->each(function (Feed $feed) {
+ Event::assertDispatched(FeedStartingEvent::class, static function (FeedStartingEvent $event) use ($feed) {
+ return $event->feed === $feed->class;
+ });
+
+ Event::assertDispatched(FeedFinishedEvent::class, static function (FeedFinishedEvent $event) use ($feed) {
+ return $event->feed === $feed->class
+ && $event->path === app($feed->class)->path();
+ });
+ });
+});
diff --git a/tests/Unit/Architecture/EventTest.php b/tests/Unit/Architecture/EventTest.php
new file mode 100644
index 0000000..69bd9a0
--- /dev/null
+++ b/tests/Unit/Architecture/EventTest.php
@@ -0,0 +1,7 @@
+expect('DragonCode\LaravelFeed\Events')
+ ->toHaveSuffix('Event');