ViewTracker.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\ViewTrackerNotFoundException;
  5. use app\Interfaces\IDatabaseObject;
  6. use app\Hajeebtok;
  7. use app\Logger;
  8. use app\Exceptions\SecurityFaultException;
  9. class ViewTracker implements IDatabaseObject
  10. {
  11. public private(set) ?int $id;
  12. public private(set) ?int $video_id;
  13. public private(set) ?int $tracking_start;
  14. public private(set) ?string $ip;
  15. public private(set) ?int $account_id;
  16. public function __construct(?int $id = null, ?int $video_id = null, ?int $tracking_start = null, ?string $ip = null, ?int $account_id = null)
  17. {
  18. $this->id = $id;
  19. $this->video_id = $video_id;
  20. $this->tracking_start = $tracking_start;
  21. $this->ip = $ip;
  22. $this->account_id = $account_id;
  23. }
  24. /**
  25. * Creates the table for the object type in the database.
  26. */
  27. public static function CreateTable(): void
  28. {
  29. throw new SecurityFaultException("Attempt to create table on view tracker object.");
  30. }
  31. /**
  32. * Drops the table for the object type from the database.
  33. */
  34. public static function DropTable(): void
  35. {
  36. throw new SecurityFaultException("Attempt to drop table on view tracker object.");
  37. }
  38. /**
  39. * Saves the object to the database.
  40. */
  41. public function Save(): int
  42. {
  43. Hajeebtok::$Database->Query("INSERT INTO view_tracking (video_id, ip, account_id) VALUES (:video_id, :ip, :account_id)", [
  44. "video_id" => $this->video_id,
  45. "ip" => $this->ip,
  46. "account_id" => $this->account_id
  47. ]);
  48. $id = Hajeebtok::$Database->LastInsertId();
  49. Logger::Info("Saving view tracker for account Id $this->account_id of IP address $this->ip, for video Id $this->video_id.");
  50. return $id;
  51. }
  52. /**
  53. * Deletes the object from the database.
  54. */
  55. public function Delete()
  56. {
  57. Hajeebtok::$Database->Query("DELETE FROM view_tracking WHERE id = :id", ["id" => $this->id]);
  58. }
  59. /**
  60. * Loads the object from the database.
  61. */
  62. public function Load()
  63. {
  64. if (empty($this->ip)) throw new ViewTrackerNotFoundException(0, 404);
  65. $data = Hajeebtok::$Database->Row("SELECT * FROM view_tracking WHERE ip = :ip", ["ip" => $this->ip]);
  66. if (empty($data)) throw new ViewTrackerNotFoundException($this->ip, 404);
  67. $this->id = $data["id"];
  68. $this->video_id = $data["video_id"];
  69. $this->tracking_start = strtotime($data["tracking_start"]);
  70. $this->ip = $data["ip"];
  71. $this->account_id = $data["account_id"];
  72. }
  73. public function LoadMany() {
  74. // not implemented
  75. }
  76. public function Exists(): bool
  77. {
  78. if (empty($this->ip)) throw new ViewTrackerNotFoundException(0, 404);
  79. $data = Hajeebtok::$Database->Row("SELECT * FROM view_tracking WHERE ip = :ip", ["ip" => $this->ip]);
  80. return !empty($data);
  81. }
  82. }