From f861f240d1d19fa194cb8b328debe6fcd3c42ffb Mon Sep 17 00:00:00 2001 From: Antoine Le Gonidec Date: Mon, 22 Jul 2024 18:05:54 +0200 Subject: [PATCH] Log an explicit error when trying to create a Customer without a Mailbox --- .../MMFRestrictedCustomersServiceProvider.php | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Providers/MMFRestrictedCustomersServiceProvider.php b/Providers/MMFRestrictedCustomersServiceProvider.php index be69681..5ce18dc 100644 --- a/Providers/MMFRestrictedCustomersServiceProvider.php +++ b/Providers/MMFRestrictedCustomersServiceProvider.php @@ -6,10 +6,14 @@ namespace Modules\MMFRestrictedCustomers\Providers; -use Illuminate\Support\ServiceProvider; -use Illuminate\Database\Eloquent\Factory; use Eventy; + +use Illuminate\Database\Eloquent\Factory; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\ServiceProvider; + use App\User; + use Modules\MMFRestrictedCustomers\Console\Commands\FetchEmails; class MMFRestrictedCustomersServiceProvider extends ServiceProvider { @@ -57,13 +61,20 @@ class MMFRestrictedCustomersServiceProvider extends ServiceProvider { Eventy::addAction( 'customer.set_data', function($customer, $data, $replace_data) { - if ( isset($data['mailbox']) ) { + if ( array_key_exists('mailbox', $data) ) { // TODO: Check that the current user is allowed to access this Mailbox. $data['mailbox_id'] = $data['mailbox']; unset($data['mailbox']); } - // TODO: Throw an error if the Mailbox is not set. - $customer->mailbox_id = $data['mailbox_id']; + if ( array_key_exists('mailbox_id', $data) ) { + $customer->mailbox_id = $data['mailbox_id']; + } else { + if ( $customer->mailbox_id == null ) { + Log::error('The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.'); + // TODO: Error out more gracefully. + abort(500, 'The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.'); + } + } }, $priority = 20, $arguments = 3