Skip to content

Commit d273900

Browse files
committed
wip
1 parent 4e08d24 commit d273900

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/Console/CleanEmails.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Console;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Console\Command;
7+
use BeyondCode\Mailbox\InboundEmail;
8+
9+
class CleanEmails extends Command
10+
{
11+
protected $signature = 'mailbox:clean';
12+
13+
protected $description = 'Clean up old incoming email logs.';
14+
15+
public function handle()
16+
{
17+
$this->comment('Cleaning WebSocket Statistics...');
18+
19+
$maxAgeInDays = config('mailbox.store_incoming_emails_for_days');
20+
21+
$cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
22+
23+
$amountDeleted = InboundEmail::where('created_at', '<', $cutOffDate)->delete();
24+
25+
$this->info("Deleted {$amountDeleted} record(s) from the Mailbox logs.");
26+
27+
$this->comment('All done!');
28+
}
29+
}

src/MailboxServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function boot()
2626

2727
Route::aliasMiddleware('laravel-mailbox', MailboxBasicAuthentication::class);
2828

29+
$this->commands([
30+
Console\CleanEmails::class
31+
]);
32+
2933
$this->registerDriver();
3034
}
3135

tests/Console/CleanEmailsTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Tests\Console;
4+
5+
use Artisan;
6+
use BeyondCode\Mailbox\InboundEmail;
7+
use BeyondCode\Mailbox\Tests\TestCase;
8+
use Carbon\Carbon;
9+
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Str;
11+
12+
class CleanEmailsTest extends TestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
Carbon::setTestNow(Carbon::create(2018, 1, 1, 00, 00, 00));
19+
20+
$this->app['config']->set('mailbox.store_incoming_emails_for_days', 31);
21+
}
22+
23+
/** @test */
24+
public function it_can_clean_the_statistics()
25+
{
26+
Collection::times(60)->each(function (int $index) {
27+
InboundEmail::forceCreate([
28+
'message' => Str::random(),
29+
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
30+
]);
31+
});
32+
33+
$this->assertCount(60, InboundEmail::all());
34+
35+
Artisan::call('mailbox:clean');
36+
37+
$this->assertCount(31, InboundEmail::all());
38+
39+
$cutOffDate = Carbon::now()->subDays(31)->format('Y-m-d H:i:s');
40+
41+
$this->assertCount(0, InboundEmail::where('created_at', '<', $cutOffDate)->get());
42+
}
43+
}

0 commit comments

Comments
 (0)