StatusEntityLexer.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use Cache;
  4. use App\{
  5. Hashtag,
  6. Media,
  7. Status,
  8. StatusHashtag
  9. };
  10. use App\Util\Lexer\Hashtag as HashtagLexer;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Foundation\Bus\Dispatchable;
  16. class StatusEntityLexer implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $status;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct(Status $status)
  26. {
  27. $this->status = $status;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. $status = $this->status;
  37. $this->parseHashtags();
  38. }
  39. public function parseHashtags()
  40. {
  41. $status = $this->status;
  42. $text = $status->caption;
  43. $tags = HashtagLexer::getHashtags($text);
  44. $rendered = $text;
  45. if(count($tags) > 0) {
  46. $rendered = HashtagLexer::replaceHashtagsWithLinks($text);
  47. }
  48. $status->rendered = $rendered;
  49. $status->save();
  50. Cache::forever('post.' . $status->id, $status);
  51. foreach($tags as $tag) {
  52. $slug = str_slug($tag);
  53. $htag = Hashtag::firstOrCreate(
  54. ['name' => $tag],
  55. ['slug' => $slug]
  56. );
  57. $stag = new StatusHashtag;
  58. $stag->status_id = $status->id;
  59. $stag->hashtag_id = $htag->id;
  60. $stag->save();
  61. }
  62. }
  63. }