Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/laravel-feeds.tree
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<toc-element topic="advanced-usage.topic" />
<toc-element topic="supported-formats.topic" />
</toc-element>
<toc-element toc-title="Digging Deeper">
<toc-element topic="events.topic" />
</toc-element>
<toc-element toc-title="Receipts">
<toc-element topic="receipt-sitemap.topic" />
<toc-element topic="receipt-instagram.topic" />
Expand Down
27 changes: 27 additions & 0 deletions docs/topics/events.topic
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
SYSTEM "https://resources.jetbrains.com/writerside/1.0/xhtml-entities.dtd">
<topic
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd"
title="Events" id="events">

<link-summary>Events dispatched during feed generation</link-summary>
<card-summary>Events dispatched during feed generation</card-summary>
<web-summary>Events dispatched during feed generation</web-summary>

<show-structure depth="3" />

<p>
Feeds dispatch two events when running the command:
</p>

<deflist>
<def title="DragonCode\LaravelFeed\Events\FeedStartingEvent">
The <code>FeedStartingEvent</code> is dispatched immediately before the draft file is created.
</def>
<def title="DragonCode\LaravelFeed\Events\FeedFinishedEvent">
The <code>FeedFinishedEvent</code> is dispatched right after the feed file generation has completed.
</def>
</deflist>
</topic>
22 changes: 22 additions & 0 deletions src/Events/FeedFinishedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Events;

use DragonCode\LaravelFeed\Feeds\Feed;

class FeedFinishedEvent
{
/**
* Create a new event instance.
*
* @param class-string<Feed> $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,
) {}
}
20 changes: 20 additions & 0 deletions src/Events/FeedStartingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Events;

use DragonCode\LaravelFeed\Feeds\Feed;

class FeedStartingEvent
{
/**
* Create a new event instance.
*
* @param class-string<Feed> $feed Reference to the feed class
* @return void
*/
public function __construct(
public string $feed,
) {}
}
21 changes: 17 additions & 4 deletions src/Services/GeneratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +17,7 @@
use Throwable;

use function blank;
use function event;
use function get_class;
use function implode;

Expand All @@ -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()
);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
}
}
30 changes: 30 additions & 0 deletions tests/Feature/Events/SuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelFeed\Commands\FeedGenerateCommand;
use DragonCode\LaravelFeed\Events\FeedFinishedEvent;
use DragonCode\LaravelFeed\Events\FeedStartingEvent;
use DragonCode\LaravelFeed\Models\Feed;
use Illuminate\Support\Facades\Event;

use function Pest\Laravel\artisan;

test('success', function () {
Event::fake();

artisan(FeedGenerateCommand::class)
->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();
});
});
});
7 changes: 7 additions & 0 deletions tests/Unit/Architecture/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

arch()
->expect('DragonCode\LaravelFeed\Events')
->toHaveSuffix('Event');