From 041913d7a9d36ba0c9b01f19e3db1d2ebf6a9302 Mon Sep 17 00:00:00 2001 From: Nasrul Hazim Bin Mohamad Date: Sat, 6 May 2023 11:00:57 +0800 Subject: [PATCH] Refactor --- .phpunit.result.cache | 2 +- config/mailhistory.php | 19 ++++++++++++++----- resources/views/.gitkeep | 0 src/Actions/HashGenerator.php | 22 ++++++++++++++++++++++ src/Concerns/InteractsWithHash.php | 22 ---------------------- src/Contracts/HashContract.php | 7 ++++++- src/Listeners/StoreMessageSending.php | 4 +--- src/Listeners/StoreMessageSent.php | 4 +--- src/Models/MailHistory.php | 3 +-- tests/MailHistoryTest.php | 2 +- 10 files changed, 47 insertions(+), 38 deletions(-) delete mode 100644 resources/views/.gitkeep create mode 100644 src/Actions/HashGenerator.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache index 3e45d10..039c202 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -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}} \ No newline at end of file +{"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}} \ No newline at end of file diff --git a/config/mailhistory.php b/config/mailhistory.php index 7f12265..b5939cd 100644 --- a/config/mailhistory.php +++ b/config/mailhistory.php @@ -1,16 +1,25 @@ 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, ], ], ]; diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Actions/HashGenerator.php b/src/Actions/HashGenerator.php new file mode 100644 index 0000000..3fb1ed2 --- /dev/null +++ b/src/Actions/HashGenerator.php @@ -0,0 +1,22 @@ +getTo()). + implode('.', $email->getFrom()). + implode('.', $email->getSubject()). + date('Y-m-d') + ); + } +} diff --git a/src/Concerns/InteractsWithHash.php b/src/Concerns/InteractsWithHash.php index 3a9439d..c8160e7 100644 --- a/src/Concerns/InteractsWithHash.php +++ b/src/Concerns/InteractsWithHash.php @@ -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. */ diff --git a/src/Contracts/HashContract.php b/src/Contracts/HashContract.php index 64c3733..24cb245 100644 --- a/src/Contracts/HashContract.php +++ b/src/Contracts/HashContract.php @@ -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; } diff --git a/src/Listeners/StoreMessageSending.php b/src/Listeners/StoreMessageSending.php index c094564..45b9d9b 100644 --- a/src/Listeners/StoreMessageSending.php +++ b/src/Listeners/StoreMessageSending.php @@ -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(), diff --git a/src/Listeners/StoreMessageSent.php b/src/Listeners/StoreMessageSent.php index 7126a93..d7d6a45 100644 --- a/src/Listeners/StoreMessageSent.php +++ b/src/Listeners/StoreMessageSent.php @@ -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']); } } diff --git a/src/Models/MailHistory.php b/src/Models/MailHistory.php index 8f19eb5..dfcb725 100644 --- a/src/Models/MailHistory.php +++ b/src/Models/MailHistory.php @@ -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; diff --git a/tests/MailHistoryTest.php b/tests/MailHistoryTest.php index 77f1dc6..018ba3a 100644 --- a/tests/MailHistoryTest.php +++ b/tests/MailHistoryTest.php @@ -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(