Issues (3627)

EmailBundle/EventListener/PointSubscriber.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 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\EmailBundle\EventListener;
13
14
use Doctrine\ORM\EntityManager;
15
use Mautic\EmailBundle\EmailEvents;
16
use Mautic\EmailBundle\Event\EmailOpenEvent;
17
use Mautic\EmailBundle\Event\EmailSendEvent;
18
use Mautic\EmailBundle\Form\Type\EmailOpenType;
19
use Mautic\EmailBundle\Form\Type\EmailSendType;
20
use Mautic\EmailBundle\Form\Type\EmailToUserType;
21
use Mautic\LeadBundle\Entity\Lead;
22
use Mautic\PointBundle\Event\PointBuilderEvent;
23
use Mautic\PointBundle\Event\TriggerBuilderEvent;
24
use Mautic\PointBundle\Model\PointModel;
25
use Mautic\PointBundle\PointEvents;
26
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
27
28
class PointSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var PointModel
32
     */
33
    private $pointModel;
34
35
    /**
36
     * @var EntityManager
37
     */
38
    private $entityManager;
39
40
    public function __construct(PointModel $pointModel, EntityManager $entityManager)
41
    {
42
        $this->pointModel    = $pointModel;
43
        $this->entityManager = $entityManager;
44
    }
45
46
    public static function getSubscribedEvents()
47
    {
48
        return [
49
            PointEvents::POINT_ON_BUILD   => ['onPointBuild', 0],
50
            PointEvents::TRIGGER_ON_BUILD => ['onTriggerBuild', 0],
51
            EmailEvents::EMAIL_ON_OPEN    => ['onEmailOpen', 0],
52
            EmailEvents::EMAIL_ON_SEND    => ['onEmailSend', 0],
53
        ];
54
    }
55
56
    public function onPointBuild(PointBuilderEvent $event)
57
    {
58
        $action = [
59
            'group'    => 'mautic.email.actions',
60
            'label'    => 'mautic.email.point.action.open',
61
            'callback' => ['\\Mautic\\EmailBundle\\Helper\\PointEventHelper', 'validateEmail'],
62
            'formType' => EmailOpenType::class,
63
        ];
64
65
        $event->addAction('email.open', $action);
66
67
        $action = [
68
            'group'    => 'mautic.email.actions',
69
            'label'    => 'mautic.email.point.action.send',
70
            'callback' => ['\\Mautic\\EmailBundle\\Helper\\PointEventHelper', 'validateEmail'],
71
            'formType' => EmailOpenType::class,
72
        ];
73
74
        $event->addAction('email.send', $action);
75
    }
76
77
    public function onTriggerBuild(TriggerBuilderEvent $event)
78
    {
79
        $sendEvent = [
80
            'group'           => 'mautic.email.point.trigger',
81
            'label'           => 'mautic.email.point.trigger.sendemail',
82
            'callback'        => ['\\Mautic\\EmailBundle\\Helper\\PointEventHelper', 'sendEmail'],
83
            'formType'        => EmailSendType::class,
84
            'formTypeOptions' => ['update_select' => 'pointtriggerevent_properties_email'],
85
            'formTheme'       => 'MauticEmailBundle:FormTheme\EmailSendList',
86
        ];
87
88
        $event->addEvent('email.send', $sendEvent);
89
90
        $sendToOwnerEvent = [
91
          'group'           => 'mautic.email.point.trigger',
92
          'label'           => 'mautic.email.point.trigger.send_email_to_user',
93
          'formType'        => EmailToUserType::class,
94
          'formTypeOptions' => ['update_select' => 'pointtriggerevent_properties_email'],
95
          'formTheme'       => 'MauticEmailBundle:FormTheme\EmailSendList',
96
          'eventName'       => EmailEvents::ON_SENT_EMAIL_TO_USER,
97
        ];
98
99
        $event->addEvent('email.send_to_user', $sendToOwnerEvent);
100
    }
101
102
    /**
103
     * Trigger point actions for email open.
104
     */
105
    public function onEmailOpen(EmailOpenEvent $event)
106
    {
107
        $this->pointModel->triggerAction('email.open', $event->getEmail());
108
    }
109
110
    /**
111
     * Trigger point actions for email send.
112
     */
113
    public function onEmailSend(EmailSendEvent $event)
114
    {
115
        $leadArray = $event->getLead();
116
        if ($leadArray && is_array($leadArray) && !empty($leadArray['id'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $leadArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
            $lead = $this->entityManager->getReference(Lead::class, $leadArray['id']);
118
        } else {
119
            return;
120
        }
121
122
        $this->pointModel->triggerAction('email.send', $event->getEmail(), null, $lead, true);
123
    }
124
}
125