EventHandler.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Comments;
  24. use OCA\Comments\Activity\Listener as ActivityListener;
  25. use OCA\Comments\AppInfo\Application;
  26. use OCA\Comments\Notification\Listener as NotificationListener;
  27. use OCP\Comments\CommentsEvent;
  28. use OCP\Comments\ICommentsEventHandler;
  29. /**
  30. * Class EventHandler
  31. *
  32. * @package OCA\Comments
  33. */
  34. class EventHandler implements ICommentsEventHandler {
  35. /** @var ActivityListener */
  36. private $activityListener;
  37. /** @var NotificationListener */
  38. private $notificationListener;
  39. public function __construct(ActivityListener $activityListener, NotificationListener $notificationListener) {
  40. $this->activityListener = $activityListener;
  41. $this->notificationListener = $notificationListener;
  42. }
  43. /**
  44. * @param CommentsEvent $event
  45. */
  46. public function handle(CommentsEvent $event) {
  47. if($event->getComment()->getObjectType() !== 'files') {
  48. // this is a 'files'-specific Handler
  49. return;
  50. }
  51. $eventType = $event->getEvent();
  52. if( $eventType === CommentsEvent::EVENT_ADD
  53. ) {
  54. $this->notificationHandler($event);
  55. $this->activityHandler($event);
  56. return;
  57. }
  58. $applicableEvents = [
  59. CommentsEvent::EVENT_PRE_UPDATE,
  60. CommentsEvent::EVENT_UPDATE,
  61. CommentsEvent::EVENT_DELETE,
  62. ];
  63. if(in_array($eventType, $applicableEvents)) {
  64. $this->notificationHandler($event);
  65. return;
  66. }
  67. }
  68. /**
  69. * @param CommentsEvent $event
  70. */
  71. private function activityHandler(CommentsEvent $event) {
  72. $this->activityListener->commentEvent($event);
  73. }
  74. /**
  75. * @param CommentsEvent $event
  76. */
  77. private function notificationHandler(CommentsEvent $event) {
  78. $this->notificationListener->evaluate($event);
  79. }
  80. }