Skip to content

Commit d534d8d

Browse files
Added FeedStartingEvent and FeedFinishedEvent
1 parent ffbc775 commit d534d8d

File tree

7 files changed

+126
-4
lines changed

7 files changed

+126
-4
lines changed

docs/laravel-feeds.tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<toc-element topic="advanced-usage.topic" />
2121
<toc-element topic="supported-formats.topic" />
2222
</toc-element>
23+
<toc-element toc-title="Digging Deeper">
24+
<toc-element topic="events.topic" />
25+
</toc-element>
2326
<toc-element toc-title="Receipts">
2427
<toc-element topic="receipt-sitemap.topic" />
2528
<toc-element topic="receipt-instagram.topic" />

docs/topics/events.topic

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE topic
3+
SYSTEM "https://resources.jetbrains.com/writerside/1.0/xhtml-entities.dtd">
4+
<topic
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd"
7+
title="Events" id="events">
8+
9+
<link-summary>Events dispatched during feed generation</link-summary>
10+
<card-summary>Events dispatched during feed generation</card-summary>
11+
<web-summary>Events dispatched during feed generation</web-summary>
12+
13+
<show-structure depth="3" />
14+
15+
<p>
16+
Feeds dispatch two events when running the command:
17+
</p>
18+
19+
<deflist>
20+
<def title="DragonCode\LaravelFeed\Events\FeedStartingEvent">
21+
The <code>FeedStartingEvent</code> is dispatched immediately before the draft file is created.
22+
</def>
23+
<def title="DragonCode\LaravelFeed\Events\FeedFinishedEvent">
24+
The <code>FeedFinishedEvent</code> is dispatched right after the feed file generation has completed.
25+
</def>
26+
</deflist>
27+
</topic>

src/Events/FeedFinishedEvent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Events;
6+
7+
use DragonCode\LaravelFeed\Feeds\Feed;
8+
9+
class FeedFinishedEvent
10+
{
11+
/**
12+
* Create a new event instance.
13+
*
14+
* @param class-string<Feed> $feed Reference to the feed class
15+
* @param string $path Path to the generated feed file
16+
* @return void
17+
*/
18+
public function __construct(
19+
public string $feed,
20+
public string $path,
21+
) {}
22+
}

src/Events/FeedStartingEvent.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Events;
6+
7+
use DragonCode\LaravelFeed\Feeds\Feed;
8+
9+
class FeedStartingEvent
10+
{
11+
/**
12+
* Create a new event instance.
13+
*
14+
* @param class-string<Feed> $feed Reference to the feed class
15+
* @return void
16+
*/
17+
public function __construct(
18+
public string $feed,
19+
) {}
20+
}

src/Services/GeneratorService.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace DragonCode\LaravelFeed\Services;
66

77
use DragonCode\LaravelFeed\Converters\Converter;
8+
use DragonCode\LaravelFeed\Events\FeedFinishedEvent;
9+
use DragonCode\LaravelFeed\Events\FeedStartingEvent;
810
use DragonCode\LaravelFeed\Exceptions\FeedGenerationException;
911
use DragonCode\LaravelFeed\Feeds\Feed;
1012
use DragonCode\LaravelFeed\Helpers\ConverterHelper;
@@ -15,6 +17,7 @@
1517
use Throwable;
1618

1719
use function blank;
20+
use function event;
1821
use function get_class;
1922
use function implode;
2023

@@ -28,10 +31,9 @@ public function __construct(
2831

2932
public function feed(Feed $feed, ?OutputStyle $output = null): void
3033
{
31-
$file = $this->openFile(
32-
$path = $feed->path()
33-
);
3434
try {
35+
$this->started($feed);
36+
3537
$file = $this->openFile(
3638
$path = $feed->path()
3739
);
@@ -45,8 +47,9 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void
4547

4648
$this->release($file, $path);
4749

48-
$this->setLastActivity($feed);
4950
$this->setLastActivity($feed);
51+
52+
$this->finished($feed, $path);
5053
} catch (Throwable $e) {
5154
throw new FeedGenerationException(get_class($feed), $e);
5255
}
@@ -162,4 +165,14 @@ protected function progressBar(int $count, ?OutputStyle $output): ?ProgressBar
162165
{
163166
return $output?->createProgressBar($count);
164167
}
168+
169+
protected function started(Feed $feed): void
170+
{
171+
event(new FeedStartingEvent(get_class($feed)));
172+
}
173+
174+
protected function finished(Feed $feed, string $path): void
175+
{
176+
event(new FeedFinishedEvent(get_class($feed), $path));
177+
}
165178
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Commands\FeedGenerateCommand;
6+
use DragonCode\LaravelFeed\Events\FeedFinishedEvent;
7+
use DragonCode\LaravelFeed\Events\FeedStartingEvent;
8+
use DragonCode\LaravelFeed\Models\Feed;
9+
use Illuminate\Support\Facades\Event;
10+
11+
use function Pest\Laravel\artisan;
12+
13+
test('success', function () {
14+
Event::fake();
15+
16+
artisan(FeedGenerateCommand::class)
17+
->assertSuccessful()
18+
->run();
19+
20+
getAllFeeds()->each(function (Feed $feed) {
21+
Event::assertDispatched(FeedStartingEvent::class, static function (FeedStartingEvent $event) use ($feed) {
22+
return $event->feed === $feed->class;
23+
});
24+
25+
Event::assertDispatched(FeedFinishedEvent::class, static function (FeedFinishedEvent $event) use ($feed) {
26+
return $event->feed === $feed->class
27+
&& $event->path === app($feed->class)->path();
28+
});
29+
});
30+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
arch()
6+
->expect('DragonCode\LaravelFeed\Events')
7+
->toHaveSuffix('Event');

0 commit comments

Comments
 (0)