Просмотр файла app/Repositories/ReadRepository.php

Размер файла: 960B
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Repositories;
  6.  
  7. use App\Models\Read;
  8. use App\Models\Story;
  9.  
  10. class ReadRepository implements RepositoryInterface
  11. {
  12. /**
  13. * Exists read
  14. *
  15. * @param int $storyId
  16. * @param string $ip
  17. *
  18. * @return bool
  19. */
  20. public function existsRead(int $storyId, string $ip): bool
  21. {
  22. return Read::query()->where('story_id', $storyId)->where('ip', $ip)->exists();
  23. }
  24.  
  25. /**
  26. * Create read
  27. *
  28. * @param Story $story
  29. * @param string $ip
  30. *
  31. * @return void
  32. */
  33. public function createRead(Story $story, string $ip): void
  34. {
  35. if ($this->existsRead($story->id, $ip)) {
  36. return;
  37. }
  38.  
  39. $story->update([
  40. 'reads' => $story->reads + 1,
  41. ]);
  42.  
  43. Read::query()->create([
  44. 'story_id' => $story->id,
  45. 'ip' => $ip,
  46. 'created_at' => time(),
  47. ]);
  48. }
  49. }