Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed May 6, 2023
1 parent c7e9ee2 commit 041913d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"\/Users\/nasrulhazim\/Projects\/2023\/mail-history\/package\/tests\/MailHistoryTest.php::it":1},"times":{"\/Users\/nasrulhazim\/Projects\/2023\/mail-history\/package\/tests\/MailHistoryTest.php::it":0.007}}
{"version":1,"defects":{"\/Users\/nasrulhazim\/Projects\/2023\/mail-history\/package\/tests\/MailHistoryTest.php::it":1},"times":{"\/Users\/nasrulhazim\/Projects\/2023\/mail-history\/package\/tests\/MailHistoryTest.php::it":0.01}}
19 changes: 14 additions & 5 deletions config/mailhistory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?php

use CleaniqueCoders\MailHistory\Actions\HashGenerator;
use CleaniqueCoders\MailHistory\Listeners\StoreMessageSending;
use CleaniqueCoders\MailHistory\Listeners\StoreMessageSent;
use CleaniqueCoders\MailHistory\Models\MailHistory;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;

return [
'enabled' => env('MAILHISTORY_ENABLED', true),

'model' => \CleaniqueCoders\MailHistory\Models\MailHistory::class,
'model' => MailHistory::class,

'hash-generator' => HashGenerator::class,

'events' => [
\Illuminate\Mail\Events\MessageSending::class => [
\CleaniqueCoders\MailHistory\Listeners\StoreMessageSending::class,
MessageSending::class => [
StoreMessageSending::class,
],
\Illuminate\Mail\Events\MessageSent::class => [
\CleaniqueCoders\MailHistory\Listeners\StoreMessageSent::class,
MessageSent::class => [
StoreMessageSent::class,
],
],
];
Empty file removed resources/views/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions src/Actions/HashGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CleaniqueCoders\MailHistory\Actions;

use CleaniqueCoders\MailHistory\Contracts\HashContract;
use Symfony\Component\Mime\Email;

class HashGenerator implements HashContract
{
/**
* \Symfony\Component\Mime\Email $email
*/
public static function generateHashValue(Email $email): string
{
return md5(
implode('.', $email->getTo()).
implode('.', $email->getFrom()).
implode('.', $email->getSubject()).
date('Y-m-d')
);
}
}
22 changes: 0 additions & 22 deletions src/Concerns/InteractsWithHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,9 @@
namespace CleaniqueCoders\MailHistory\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

trait InteractsWithHash
{
public static function bootInteractsWithHash()
{
static::creating(function (Model $model) {
if (Schema::hasColumn($model->getTable(), $model->getHashColumnName()) && is_null($model->{$model->getHashColumnName()})) {
$model->{$model->getHashColumnName()} = self::generateHashValue();
}
});
}

/**
* Generate Hash Value
*/
public static function generateHashValue(array $value = []): string
{
return md5(
count($value) == 0 ? Str::random(32) : implode('.', $value)
);
}

/**
* Get Hash Column Name.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Contracts/HashContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace CleaniqueCoders\MailHistory\Contracts;

use Symfony\Component\Mime\Email;

interface HashContract
{
public static function generateHashValue(array $value = []): string;
/**
* \Symfony\Component\Mime\Email $email
*/
public static function generateHashValue(Email $email): string;
}
4 changes: 1 addition & 3 deletions src/Listeners/StoreMessageSending.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public function handle(MessageSending $event): void

config('mailhistory.model')::create([
'uuid' => Str::uuid(),
'hash' => config('mailhistory.model')::generateHashValue(
$event->message->getHeaders()->toArray()
),
'hash' => config('mailhistory.hash-generator')::generateHashValue($event->message),
'status' => 'Sending',
'headers' => $event->message->getHeaders()->toArray(),
'body' => $event->message->getBody()->bodyToString(),
Expand Down
4 changes: 1 addition & 3 deletions src/Listeners/StoreMessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public function handle(MessageSent $event): void
MailHistoryException::throwIfHashContractMissing();

config('mailhistory.model')::whereHash(
config('mailhistory.model')::generateHashValue(
$event->message->getHeaders()->toArray()
)
config('mailhistory.hash-generator')::generateHashValue($event->message)
)->update(['status' => 'Sent']);
}
}
3 changes: 1 addition & 2 deletions src/Models/MailHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use CleaniqueCoders\MailHistory\Concerns\InteractsWithHash;
use CleaniqueCoders\MailHistory\Concerns\InteractsWithUuid;
use CleaniqueCoders\MailHistory\Contracts\HashContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MailHistory extends Model implements HashContract
class MailHistory extends Model
{
use HasFactory;
use InteractsWithUuid;
Expand Down
2 changes: 1 addition & 1 deletion tests/MailHistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
);

$this->assertTrue(
in_array(HashContract::class, class_implements(config('mailhistory.model')))
in_array(HashContract::class, class_implements(config('mailhistory.hash-generator')))
);

$this->assertTrue(
Expand Down

0 comments on commit 041913d

Please sign in to comment.