src/EventSubscriber/RedmineEntryCreationNotifier.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright ©️ 2020-2021 Seidemann Web GmbH <info@seidemann-web.com> - All Rights Reserved.
  5.  *
  6.  * Unauthorized copying and/or distribution of this file, via any medium, is strictly prohibited.
  7.  *
  8.  * Proprietary and confidential.
  9.  */
  10. namespace Seidemann\Hanagud\EventSubscriber;
  11. use Flagception\Manager\FeatureManagerInterface;
  12. use Redmine\Api\Issue;
  13. use Seidemann\Hanagud\Entries\Events\EntryCreatedEvent;
  14. use Seidemann\Hanagud\Services\RedmineAPIProvider;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. use Twig\Environment;
  18. class RedmineEntryCreationNotifier implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         protected RedmineAPIProvider $redmine,
  22.         protected Security $security,
  23.         protected Environment $twig,
  24.         protected FeatureManagerInterface $featureManager
  25.     ) {
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             EntryCreatedEvent::NAME => 'onEntryCreated',
  31.         ];
  32.     }
  33.     public function onEntryCreated(EntryCreatedEvent $event)
  34.     {
  35.         if (!$this->featureManager->isActive('redmine_notification')) {
  36.             // do nothing if redmine_notification is off
  37.             return;
  38.         }
  39.         $entry $event->getData();
  40.         // validate this is a valid redmine issue first
  41.         $redmineIssueID $entry->getRedmineIssueID();
  42.         if (null === $redmineIssueID) {
  43.             return;
  44.         }
  45.         // ignore special internal issue 100, see https://gitlab.seidemann-web.com/kunden/seidemann/hanagud/-/issues/50
  46.         if (100 === $redmineIssueID) {
  47.             return;
  48.         }
  49.         /** @var Issue $issuesApi */
  50.         $issuesApi $this->redmine->getIssueAPI();
  51.         $redmineIssue $issuesApi->show($redmineIssueID);
  52.         if (empty($redmineIssue) || false === $redmineIssue) {
  53.             return; // could not find the redmine issue for this, ignoring
  54.         }
  55.         $body $this->twig->render('text/redmine/issue_comment.md.twig', [
  56.             'entry' => $entry,
  57.             'issue' => $redmineIssue,
  58.             'user' => $this->security->getUser(),
  59.             'isUserRedmineAPIKey' => $this->redmine->isUserRedmineAPIKey(),
  60.         ]);
  61.         $issuesApi->update($redmineIssueID, [
  62.             'notes' => $body,
  63.             'private_notes' => true,
  64.         ]);
  65.         // could not send notification, fail safe abort (EntriesController try-catches this and removes the BW entry)
  66.         $this->redmine->throwOnInvalidResponseCode();
  67.     }
  68. }