Follow.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\FollowNotFoundException;
  4. use app\Interfaces\IDatabaseObject;
  5. use app\Hajeebtok;
  6. use app\Logger;
  7. use app\Exceptions\SecurityFaultException;
  8. class Follow implements IDatabaseObject
  9. {
  10. public private(set) ?int $follower_id;
  11. public private(set) ?int $followee_id;
  12. public function __construct(?int $follower_id = null, ?int $followee_id = null)
  13. {
  14. $this->follower_id = $follower_id;
  15. $this->followee_id = $followee_id;
  16. }
  17. /**
  18. * Creates the table for the object type in the database.
  19. */
  20. public static function CreateTable(): void
  21. {
  22. throw new SecurityFaultException("Attempt to create table on follow object.");
  23. }
  24. /**
  25. * Drops the table for the object type from the database.
  26. */
  27. public static function DropTable(): void
  28. {
  29. throw new SecurityFaultException("Attempt to drop table on follow object.");
  30. }
  31. /**
  32. * Saves the object to the database.
  33. */
  34. public function Save()
  35. {
  36. Hajeebtok::$Database->Query("INSERT INTO follows (follower_id, followee_id) VALUES (:follower_id, :followee_id)", [
  37. "follower_id" => $this->follower_id,
  38. "followee_id" => $this->followee_id
  39. ]);
  40. $id = Hajeebtok::$Database->LastInsertId();
  41. Logger::Debug("Saved account id ($id).");
  42. }
  43. /**
  44. * Deletes the object from the database.
  45. */
  46. public function Delete()
  47. {
  48. if(empty($this->follower_id) || empty($this->followee_id)) throw new FollowNotFoundException(0, 404);
  49. Hajeebtok::$Database->Query("DELETE FROM follows WHERE follower_id = :follower_id AND followee_id = :followee_id", [
  50. "follower_id" => $this->follower_id,
  51. "followee_id" => $this->followee_id
  52. ]);
  53. }
  54. /**
  55. * Loads the object from the database.
  56. */
  57. public function Load()
  58. {
  59. // unimplemented
  60. }
  61. public function LoadMany(): array {
  62. if(!empty($this->follower_id) && empty($this->followee_id)) {
  63. $command = "SELECT * FROM follows WHERE follower_id = :follower_id";
  64. $array = ["follower_id" => $this->follower_id];
  65. } else if(empty($this->follower_id) && !empty($this->followee_id)) {
  66. $command = "SELECT * FROM follows WHERE followee_id = :followee_id";
  67. $array = ["followee_id" => $this->followee_id];
  68. } else {
  69. throw new FollowNotFoundException(0, 404);
  70. }
  71. return Hajeebtok::$Database->Query($command, $array);
  72. }
  73. }