StatusDelete.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\{Media, Notification, StatusHashtag, Status};
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. class StatusDelete implements ShouldQueue
  10. {
  11. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. protected $status;
  13. /**
  14. * Create a new job instance.
  15. *
  16. * @return void
  17. */
  18. public function __construct(Status $status)
  19. {
  20. $this->status = $status;
  21. }
  22. /**
  23. * Execute the job.
  24. *
  25. * @return void
  26. */
  27. public function handle()
  28. {
  29. $status = $this->status;
  30. $this->unlinkRemoveMedia($status);
  31. }
  32. public function unlinkRemoveMedia($status)
  33. {
  34. if($status->media()->count() == 0) {
  35. return;
  36. }
  37. foreach($status->media as $media) {
  38. $thumbnail = storage_path("app/{$media->thumbnail_path}");
  39. $photo = storage_path("app/{$media->media_path}");
  40. try {
  41. if(is_file($thumbnail)) {
  42. unlink($thumbnail);
  43. }
  44. if(is_file($photo)) {
  45. unlink($photo);
  46. }
  47. $media->delete();
  48. } catch (Exception $e) {
  49. }
  50. }
  51. $comments = Status::where('in_reply_to_id', $status->id)->get();
  52. foreach($comments as $comment) {
  53. $comment->in_reply_to_id = null;
  54. $comment->save();
  55. Notification::whereItemType('App\Status')
  56. ->whereItemId($comment->id)
  57. ->delete();
  58. }
  59. $status->likes()->delete();
  60. Notification::whereItemType('App\Status')
  61. ->whereItemId($status->id)
  62. ->delete();
  63. StatusHashtag::whereStatusId($status->id)->delete();
  64. $status->delete();
  65. return true;
  66. }
  67. }