ManageComments.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class ManageComments {
  19. private static $Instance = null;
  20. private FirebaseCon $Connection;
  21. private string $SpecificPath;
  22. private function __construct(FirebaseCon $Connection, string $SpecificPath) {
  23. $this->Connection = $Connection;
  24. $this->SpecificPath = $SpecificPath;
  25. }
  26. public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
  27. if (self::$Instance === null) {
  28. self::$Instance = new ManageComments($Connection, $SpecificPath);
  29. }
  30. return self::$Instance;
  31. }
  32. public function GetComments() {
  33. $Result = $this->Connection->GetList($this->SpecificPath);
  34. if (isset($Result['error'])) {
  35. return 'General error while requesting';
  36. } else if (count($Result) === 0) {
  37. return 'No comments found';
  38. } else {
  39. $CommentsList = [];
  40. foreach ($Result as $CommentData) {
  41. $Comment = new Comment($CommentData['id'], $CommentData['user_id'],
  42. $CommentData['summary'], $CommentData['date'],
  43. $CommentData['content']);
  44. array_push($CommentsList, $Comment);
  45. }
  46. return $CommentsList;
  47. }
  48. }
  49. public function GetComment(string $Id) {
  50. $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);
  51. if (isset($Result['error'])) {
  52. return 'General error while requesting';
  53. } else if (count($Result) === 0) {
  54. return 'Comment does not exist';
  55. } else {
  56. $Comment = new Comment($Result['id'], $Result['user_id'],
  57. $Result['summary'], $Result['date'],
  58. $Result['content']);
  59. return $Comment;
  60. }
  61. }
  62. public function AddComment(Comment $Comment): string {
  63. $CommentArray = [
  64. 'id' => $Comment->GetId(),
  65. 'user_id' => $Comment->GetUserId(),
  66. 'summary' => $Comment->GetSummary(),
  67. 'date' => $Comment->GetDate(),
  68. 'content' => $Comment->GetContent()
  69. ];
  70. if ($this->Connection->AddListElement($this->SpecificPath, $CommentArray)) {
  71. return 'Comment added successfully';
  72. } else {
  73. return 'Comment already exists';
  74. }
  75. }
  76. public function RemoveComment(string $Id): string {
  77. if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
  78. return 'Comment removed successfully';
  79. } else {
  80. return 'Comment does not exist';
  81. }
  82. }
  83. }