Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FlycutResharedListener | |
0.00% |
0 / 36 |
|
0.00% |
0 / 3 |
56 | |
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 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Listeners\User; |
| 4 | |
| 5 | use App\Events\User\FlycutResharedEvent; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use App\Http\Models\ClonedSharedShortcut; |
| 8 | use App\Http\Models\FlyShare; |
| 9 | use App\Http\Models\ShareHistory; |
| 10 | use App\Http\Services\FlyShareService; |
| 11 | use App\Mail\FlyCutSharedMail; |
| 12 | use App\Services\Email\EmailService; |
| 13 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 14 | use Illuminate\Support\Facades\Mail; |
| 15 | use MongoDB\BSON\UTCDateTime; |
| 16 | |
| 17 | class FlycutResharedListener implements ShouldQueue |
| 18 | { |
| 19 | /** |
| 20 | * The flyshare service implementation. |
| 21 | * |
| 22 | * @var FlyShareService |
| 23 | */ |
| 24 | protected $flyshareService; |
| 25 | |
| 26 | /** |
| 27 | * Create the event listener. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function __construct( |
| 32 | FlyShareService $flyshareService, |
| 33 | private readonly EmailService $emailService |
| 34 | ) { |
| 35 | $this->flyshareService = $flyshareService; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Handle the event. |
| 40 | */ |
| 41 | public function handle(FlycutResharedEvent $event): void |
| 42 | { |
| 43 | $flyshare = $event->flyshare; |
| 44 | $shortcut = ClonedSharedShortcut::find($event->clonedShortcutId); |
| 45 | |
| 46 | $shortcutPreview = $this->flyshareService->fullTextToPreview($shortcut->text); |
| 47 | |
| 48 | try { |
| 49 | $this->emailService->send( |
| 50 | $event->recipientEmail, |
| 51 | new FlyCutSharedMail( |
| 52 | $flyshare->id, |
| 53 | $flyshare->user->first_name, |
| 54 | $flyshare->user->last_name, |
| 55 | $shortcutPreview, |
| 56 | '' |
| 57 | ), |
| 58 | 'cac_invite_user', |
| 59 | false |
| 60 | ); |
| 61 | if (count(Mail::failures()) < 1) { |
| 62 | $this->updateFlyshare($flyshare, $event->recipientEmail); |
| 63 | } |
| 64 | } catch (\Throwable $th) { |
| 65 | throw $th; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Update flyshare record with users' email where shortcut invite link was sent to |
| 71 | */ |
| 72 | private function updateFlyshare(FlyShare $flyshare, string $recipientEmail): void |
| 73 | { |
| 74 | $shareHistory = ShareHistory::where([ |
| 75 | ['flyshare_id', $flyshare->id], |
| 76 | ['email', $recipientEmail], |
| 77 | ])->first(); |
| 78 | |
| 79 | if ($shareHistory) { |
| 80 | $shareHistory->update([ |
| 81 | 'resent_at' => new UTCDateTime(now()), |
| 82 | ]); |
| 83 | |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $user = User::where('email', $recipientEmail)->first(); |
| 88 | |
| 89 | $flyshare->shareHistory()->create([ |
| 90 | 'email' => $recipientEmail, |
| 91 | 'status' => FlyShare::FLYSHARE_INVITE_STATUS_PENDING, |
| 92 | 'registered' => filled($user), |
| 93 | 'user_id' => filled($user) ? $user->id : null, |
| 94 | ]); |
| 95 | } |
| 96 | } |