File size: 2,974 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

namespace Mautic\WebhookBundle\Notificator;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\EmailBundle\Helper\MailHelper;
use Mautic\UserBundle\Entity\User;
use Mautic\WebhookBundle\Entity\Webhook;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class WebhookKillNotificator
{
    public function __construct(
        private TranslatorInterface $translator,
        private Router $router,
        private NotificationModel $notificationModel,
        private EntityManager $entityManager,
        private MailHelper $mailer,
        private CoreParametersHelper $coreParametersHelper
    ) {
    }

    /**
     * @param string $reason Translatable key
     */
    public function send(Webhook $webhook, $reason): void
    {
        $subject = $this->translator->trans('mautic.webhook.stopped');
        $reason  = $this->translator->trans($reason);
        $htmlUrl = '<a href="'.$this->router->generate(
            'mautic_webhook_action',
            ['objectAction' => 'view', 'objectId' => $webhook->getId()],
            UrlGeneratorInterface::ABSOLUTE_URL
        ).'" data-toggle="ajax">'.$webhook->getName().'</a>';

        $details = $this->translator->trans(
            'mautic.webhook.stopped.details',
            [
                '%reason%'  => $reason,
                '%webhook%' => $htmlUrl,
            ]
        );

        /** @var User $owner */
        $owner = $toUser = $this->entityManager->getReference(User::class, $webhook->getCreatedBy());

        $ccToUser = null;

        if (null !== $webhook->getModifiedBy() && $webhook->getCreatedBy() !== $webhook->getModifiedBy()) {
            $modifiedBy = $this->entityManager->getReference(User::class, $webhook->getModifiedBy());

            $toUser   = $modifiedBy; // Send notification to modifier
            $ccToUser = $owner; // And cc e-mail to owner
        }

        // Send notification
        $this->notificationModel->addNotification(
            $details,
            'error',
            false,
            $subject,
            null,
            null,
            $toUser
        );

        // Send e-mail
        $mailer = $this->mailer;

        $sendToAuthor = $this->coreParametersHelper->get('webhook_send_notification_to_author', 1);
        if ($sendToAuthor) {
            $mailer->setTo($toUser->getEmail());
            if ($ccToUser) {
                $mailer->setCc([$ccToUser->getEmail() => null]);
            }
        } else {
            $emailAddresses = array_map('trim', explode(',', $this->coreParametersHelper->get('webhook_notification_email_addresses')));
            $mailer->setTo($emailAddresses);
        }

        $mailer->setSubject($subject);
        $mailer->setBody($details);
        $mailer->send(true);
    }
}