Skip to content

Commit 0c33245

Browse files
committed
Refactor: Refactor structure and naming of a package and also created some abstractions for future extension possibilities
# Conflicts: # src/MailTransportFactory.php # src/MailboxForLaravelServiceProvider.php
1 parent dc2f37f commit 0c33245

19 files changed

+466
-95
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
"extra": {
6464
"laravel": {
6565
"providers": [
66-
"Redberry\\MailboxForLaravel\\MailboxForLaravelServiceProvider"
66+
"InboxServiceProvider"
6767
],
6868
"aliases": {
69-
"MailboxForLaravel": "Redberry\\MailboxForLaravel\\Facades\\MailboxForLaravel"
69+
"MailboxForLaravel": "Inbox"
7070
}
7171
}
7272
},

config/inbox.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
return [
3+
'store' => [
4+
'driver' => env('INBOX_STORE_DRIVER', 'file'),
5+
'file' => [
6+
'path' => env('INBOX_FILE_PATH', storage_path('app/inbox')),
7+
],
8+
],
9+
'retention' => [
10+
'seconds' => (int) env('INBOX_RETENTION', 60 * 60 * 24),
11+
],
12+
];

config/mailbox-for-laravel.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/CaptureService.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Redberry\MailboxForLaravel;
4+
5+
use Redberry\MailboxForLaravel\Contracts\MessageStore;
6+
7+
class CaptureService
8+
{
9+
public function __construct(protected MessageStore $storage)
10+
{
11+
}
12+
13+
/**
14+
* Persist the raw message and metadata.
15+
*/
16+
public function storeRaw(string $raw): string
17+
{
18+
$key = 'email_' . md5($raw) . '_' . microtime(true);
19+
20+
$this->storage->store($key, [
21+
'timestamp' => time(),
22+
'raw' => $raw,
23+
]);
24+
25+
return $key;
26+
}
27+
}

src/Contracts/MessageStore.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Redberry\MailboxForLaravel\Contracts;
4+
5+
interface MessageStore
6+
{
7+
/**
8+
* Persist an email payload.
9+
*
10+
* @param string $key Unique key (you generate it).
11+
* @param array $value Associative array payload; MUST include at least: ['raw' => string, 'timestamp' => int]
12+
*
13+
* @return void
14+
*/
15+
public function store(string $key, array $value): void;
16+
17+
/**
18+
* Retrieve a single payload by key.
19+
*
20+
* @param string $key Unique key to retrieve the payload.
21+
*
22+
* @return array|null Returns the payload array or null if not found.
23+
*/
24+
public function retrieve(string $key): ?array;
25+
26+
/**
27+
* Retrieve multiple payload keys optionally filtered by a UNIX timestamp (>= since).
28+
*
29+
* @param int|null $since
30+
*
31+
* @return iterable<string> Keys
32+
*/
33+
public function keys(?int $since = null): iterable;
34+
35+
/**
36+
* Delete a payload by key.
37+
*
38+
* @param string $key Unique key to delete the payload.
39+
*/
40+
public function delete(string $key): void;
41+
42+
/**
43+
* Remove payloads older than $seconds (relative to now).
44+
*
45+
* @param int $seconds Number of seconds to keep; older payloads will be purged.
46+
*/
47+
public function purgeOlderThan(int $seconds): void;
48+
}

src/Facades/MailboxForLaravel.php renamed to src/Facades/Inbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
/**
88
* @see \Redberry\MailboxForLaravel\MailboxForLaravel
99
*/
10-
class MailboxForLaravel extends Facade
10+
class Inbox extends Facade
1111
{
1212
protected static function getFacadeAccessor(): string
1313
{
14-
return \Redberry\MailboxForLaravel\MailboxForLaravel::class;
14+
return \Redberry\MailboxForLaravel\InboxTranport::class;
1515
}
1616
}

src/InboxServiceProvider.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Redberry\MailboxForLaravel;
4+
5+
use Illuminate\Mail\MailManager;
6+
use Redberry\MailboxForLaravel\Contracts\MessageStore;
7+
use Redberry\MailboxForLaravel\Transport\InboxTransport;
8+
use Spatie\LaravelPackageTools\Package;
9+
use Spatie\LaravelPackageTools\PackageServiceProvider;
10+
11+
class InboxServiceProvider extends PackageServiceProvider
12+
{
13+
public function configurePackage(Package $package): void
14+
{
15+
$package
16+
->name('mailbox-for-laravel')
17+
->hasConfigFile('inbox');
18+
// keep room for: ->hasViews(), ->hasRoute() later for dashboard
19+
}
20+
21+
public function registeringPackage(): void
22+
{
23+
$this->app->singleton(MessageStore::class, function () {
24+
return (new StoreManager())->create();
25+
});
26+
$this->app->singleton(CaptureService::class, function () {
27+
return new CaptureService(app(MessageStore::class));
28+
});
29+
30+
$this->app->singleton(InboxTransport::class, fn() => new InboxTransport(app(CaptureService::class)));
31+
32+
// Register the mail driver
33+
$this->app->afterResolving(MailManager::class, function (MailManager $manager) {
34+
$manager->extend('inbox', function ($config) {
35+
// Return a Symfony TransportInterface
36+
return app(InboxTransport::class);
37+
});
38+
});
39+
}
40+
}

src/MailTransportFactory.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/MailboxForLaravel.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/MailboxForLaravelServiceProvider.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)