Issues (3627)

EventListener/NotificationSubscriber.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\NotificationBundle\EventListener;
13
14
use Mautic\AssetBundle\Helper\TokenHelper as AssetTokenHelper;
15
use Mautic\CoreBundle\Event\TokenReplacementEvent;
16
use Mautic\CoreBundle\Model\AuditLogModel;
17
use Mautic\LeadBundle\Entity\Lead;
18
use Mautic\LeadBundle\Helper\TokenHelper;
19
use Mautic\NotificationBundle\Event\NotificationEvent;
20
use Mautic\NotificationBundle\NotificationEvents;
21
use Mautic\PageBundle\Entity\Trackable;
22
use Mautic\PageBundle\Helper\TokenHelper as PageTokenHelper;
23
use Mautic\PageBundle\Model\TrackableModel;
24
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25
26
class NotificationSubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var TrackableModel
30
     */
31
    private $trackableModel;
32
33
    /**
34
     * @var PageTokenHelper
35
     */
36
    private $pageTokenHelper;
37
38
    /**
39
     * @var AssetTokenHelper
40
     */
41
    private $assetTokenHelper;
42
43
    /**
44
     * @var AuditLogModel
45
     */
46
    private $auditLogModel;
47
48
    public function __construct(AuditLogModel $auditLogModel, TrackableModel $trackableModel, PageTokenHelper $pageTokenHelper, AssetTokenHelper $assetTokenHelper)
49
    {
50
        $this->auditLogModel    = $auditLogModel;
51
        $this->trackableModel   = $trackableModel;
52
        $this->pageTokenHelper  = $pageTokenHelper;
53
        $this->assetTokenHelper = $assetTokenHelper;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public static function getSubscribedEvents()
60
    {
61
        return [
62
            NotificationEvents::NOTIFICATION_POST_SAVE   => ['onPostSave', 0],
63
            NotificationEvents::NOTIFICATION_POST_DELETE => ['onDelete', 0],
64
            NotificationEvents::TOKEN_REPLACEMENT        => ['onTokenReplacement', 0],
65
        ];
66
    }
67
68
    /**
69
     * Add an entry to the audit log.
70
     */
71
    public function onPostSave(NotificationEvent $event)
72
    {
73
        $entity = $event->getNotification();
74
        if ($details = $event->getChanges()) {
75
            $log = [
76
                'bundle'   => 'notification',
77
                'object'   => 'notification',
78
                'objectId' => $entity->getId(),
79
                'action'   => ($event->isNew()) ? 'create' : 'update',
80
                'details'  => $details,
81
            ];
82
            $this->auditLogModel->writeToLog($log);
83
        }
84
    }
85
86
    /**
87
     * Add a delete entry to the audit log.
88
     */
89
    public function onDelete(NotificationEvent $event)
90
    {
91
        $entity = $event->getNotification();
92
        $log    = [
93
            'bundle'   => 'notification',
94
            'object'   => 'notification',
95
            'objectId' => $entity->deletedId,
96
            'action'   => 'delete',
97
            'details'  => ['name' => $entity->getName()],
98
        ];
99
        $this->auditLogModel->writeToLog($log);
100
    }
101
102
    public function onTokenReplacement(TokenReplacementEvent $event)
103
    {
104
        /** @var Lead $lead */
105
        $lead         = $event->getLead();
106
        $content      = $event->getContent();
107
        $clickthrough = $event->getClickthrough();
108
109
        if ($content) {
110
            $tokens = array_merge(
111
                TokenHelper::findLeadTokens($content, $lead->getProfileFields()),
0 ignored issues
show
It seems like Mautic\LeadBundle\Helper...ad->getProfileFields()) can also be of type string; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
                /** @scrutinizer ignore-type */ TokenHelper::findLeadTokens($content, $lead->getProfileFields()),
Loading history...
112
                $this->pageTokenHelper->findPageTokens($content, $clickthrough),
113
                $this->assetTokenHelper->findAssetTokens($content, $clickthrough)
114
            );
115
116
            list($content, $trackables) = $this->trackableModel->parseContentForTrackables(
117
                $content,
118
                $tokens,
119
                'notification',
120
                $clickthrough['channel'][1]
121
            );
122
123
            /**
124
             * @var string
125
             * @var Trackable $trackable
126
             */
127
            foreach ($trackables as $token => $trackable) {
128
                $tokens[$token] = $this->trackableModel->generateTrackableUrl($trackable, $clickthrough);
129
            }
130
131
            $content = str_replace(array_keys($tokens), array_values($tokens), $content);
132
133
            $event->setContent($content);
134
        }
135
    }
136
}
137