Issues (3627)

LeadBundle/EventListener/EmailSubscriber.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\LeadBundle\EventListener;
13
14
use Mautic\CoreBundle\Helper\BuilderTokenHelperFactory;
15
use Mautic\EmailBundle\EmailEvents;
16
use Mautic\EmailBundle\Event\EmailBuilderEvent;
17
use Mautic\EmailBundle\Event\EmailSendEvent;
18
use Mautic\LeadBundle\Helper\TokenHelper;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class EmailSubscriber implements EventSubscriberInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private static $contactFieldRegex = '{contactfield=(.*?)}';
27
28
    /**
29
     * @var string
30
     */
31
    private $builderTokenHelperFactory;
32
33
    public function __construct(BuilderTokenHelperFactory $builderTokenHelperFactory)
34
    {
35
        $this->builderTokenHelperFactory = $builderTokenHelperFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $builderTokenHelperFactory of type Mautic\CoreBundle\Helper\BuilderTokenHelperFactory is incompatible with the declared type string of property $builderTokenHelperFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            EmailEvents::EMAIL_ON_BUILD   => ['onEmailBuild', 0],
45
            EmailEvents::EMAIL_ON_SEND    => ['onEmailGenerate', 0],
46
            EmailEvents::EMAIL_ON_DISPLAY => ['onEmailDisplay', 0],
47
        ];
48
    }
49
50
    public function onEmailBuild(EmailBuilderEvent $event)
51
    {
52
        $tokenHelper = $this->builderTokenHelperFactory->getBuilderTokenHelper('lead.field', 'lead:fields', 'MauticLeadBundle');
53
        // the permissions are for viewing contact data, not for managing contact fields
54
        $tokenHelper->setPermissionSet(['lead:leads:viewown', 'lead:leads:viewother']);
55
56
        if ($event->tokensRequested(self::$contactFieldRegex)) {
57
            $event->addTokensFromHelper($tokenHelper, self::$contactFieldRegex, 'label', 'alias');
58
        }
59
    }
60
61
    public function onEmailDisplay(EmailSendEvent $event)
62
    {
63
        $this->onEmailGenerate($event);
64
    }
65
66
    public function onEmailGenerate(EmailSendEvent $event)
67
    {
68
        // Combine all possible content to find tokens across them
69
        $content = $event->getSubject();
70
        $content .= $event->getContent();
71
        $content .= $event->getPlainText();
72
        $content .= implode(' ', $event->getTextHeaders());
73
74
        $lead = $event->getLead();
75
76
        $tokenList = TokenHelper::findLeadTokens($content, $lead);
77
        if (count($tokenList)) {
78
            $event->addTokens($tokenList);
79
            unset($tokenList);
80
        }
81
    }
82
}
83