Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FlyCutSharedListener | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| updateFlyshare | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Listeners\User; |
| 4 | |
| 5 | use App\Events\User\FlyCutSharedEvent; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use App\Http\Models\FlyShare; |
| 8 | use App\Http\Services\FlyShareService; |
| 9 | use App\Mail\FlyCutSharedMail; |
| 10 | use App\Services\Email\EmailService; |
| 11 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 12 | |
| 13 | class FlyCutSharedListener implements ShouldQueue |
| 14 | { |
| 15 | /** |
| 16 | * The flyshare service implementation. |
| 17 | * |
| 18 | * @var FlyShareService |
| 19 | */ |
| 20 | protected $flyshareService; |
| 21 | |
| 22 | /** |
| 23 | * Create the event listener. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __construct( |
| 28 | FlyShareService $flyshareService, |
| 29 | private readonly EmailService $emailService |
| 30 | ) { |
| 31 | $this->flyshareService = $flyshareService; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Handle the event. |
| 36 | */ |
| 37 | public function handle(FlyCutSharedEvent $event): void |
| 38 | { |
| 39 | $flyshare = $event->flyshare; |
| 40 | |
| 41 | $this->flyshareService->cloneShortcuts($flyshare->shortcut_ids, $flyshare->_id); |
| 42 | |
| 43 | $shortcutPreview = $this->flyshareService->fullTextToPreview($flyshare->clonedSharedShortcuts()->first()->text); |
| 44 | |
| 45 | try { |
| 46 | foreach ($event->recipientEmails as $recipient) { |
| 47 | $this->emailService->send( |
| 48 | $recipient, |
| 49 | new FlyCutSharedMail( |
| 50 | $flyshare->id, |
| 51 | $event->firstName, |
| 52 | $event->lastName, |
| 53 | $shortcutPreview, |
| 54 | $event->message, |
| 55 | $flyshare->clonedSharedShortcuts->count() |
| 56 | ), |
| 57 | 'fly_cut_shared_email' |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | $this->updateFlyshare($flyshare, $event->recipientEmails); |
| 62 | } catch (\Throwable $th) { |
| 63 | throw $th; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * update flyshare record with users' email where shortcut invite link was sent to |
| 69 | */ |
| 70 | private function updateFlyshare(FlyShare $flyshare, array $recipientEmails): void |
| 71 | { |
| 72 | $users = User::whereIn('email', $recipientEmails)->get(['_id', 'email']); |
| 73 | |
| 74 | $emails = array_map(function ($email) use ($users) { |
| 75 | $registered = $users->contains('email', $email); |
| 76 | |
| 77 | return [ |
| 78 | 'email' => $email, |
| 79 | 'status' => FlyShare::FLYSHARE_INVITE_STATUS_PENDING, |
| 80 | 'registered' => $registered, |
| 81 | 'user_id' => $registered ? $users->firstWhere('email', $email)->id : null, |
| 82 | ]; |
| 83 | }, $recipientEmails); |
| 84 | |
| 85 | $flyshare->shareHistory()->createMany($emails); |
| 86 | } |
| 87 | } |