<?php
declare(strict_types=1);
/*
* Copyright ©️ 2020-2021 Seidemann Web GmbH <info@seidemann-web.com> - All Rights Reserved.
*
* Unauthorized copying and/or distribution of this file, via any medium, is strictly prohibited.
*
* Proprietary and confidential.
*/
namespace Seidemann\Hanagud\EventSubscriber;
use Flagception\Manager\FeatureManagerInterface;
use Redmine\Api\Issue;
use Seidemann\Hanagud\Entries\Events\EntryCreatedEvent;
use Seidemann\Hanagud\Services\RedmineAPIProvider;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class RedmineEntryCreationNotifier implements EventSubscriberInterface
{
public function __construct(
protected RedmineAPIProvider $redmine,
protected Security $security,
protected Environment $twig,
protected FeatureManagerInterface $featureManager
) {
}
public static function getSubscribedEvents()
{
return [
EntryCreatedEvent::NAME => 'onEntryCreated',
];
}
public function onEntryCreated(EntryCreatedEvent $event)
{
if (!$this->featureManager->isActive('redmine_notification')) {
// do nothing if redmine_notification is off
return;
}
$entry = $event->getData();
// validate this is a valid redmine issue first
$redmineIssueID = $entry->getRedmineIssueID();
if (null === $redmineIssueID) {
return;
}
// ignore special internal issue 100, see https://gitlab.seidemann-web.com/kunden/seidemann/hanagud/-/issues/50
if (100 === $redmineIssueID) {
return;
}
/** @var Issue $issuesApi */
$issuesApi = $this->redmine->getIssueAPI();
$redmineIssue = $issuesApi->show($redmineIssueID);
if (empty($redmineIssue) || false === $redmineIssue) {
return; // could not find the redmine issue for this, ignoring
}
$body = $this->twig->render('text/redmine/issue_comment.md.twig', [
'entry' => $entry,
'issue' => $redmineIssue,
'user' => $this->security->getUser(),
'isUserRedmineAPIKey' => $this->redmine->isUserRedmineAPIKey(),
]);
$issuesApi->update($redmineIssueID, [
'notes' => $body,
'private_notes' => true,
]);
// could not send notification, fail safe abort (EntriesController try-catches this and removes the BW entry)
$this->redmine->throwOnInvalidResponseCode();
}
}