MandrillSwiftEmailMapper::addMtaHeadersToMessage()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8337
c 0
b 0
f 0
cc 6
nc 32
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Notification\Client\Email\SwiftMailer\Mapper;
16
17
use Acme\App\Core\Port\Notification\Client\Email\Email;
18
use Swift_Mime_SimpleMessage;
19
20
/**
21
 * @author Marijn Koesen
22
 * @author Herberto Graca <[email protected]>
23
 */
24
class MandrillSwiftEmailMapper extends SwiftEmailMapper
25
{
26
    /**
27
     * @var string
28
     */
29
    private $host;
30
31
    /**
32
     * @param string $host The domain/host used in google analytics
33
     */
34
    public function __construct($host)
35
    {
36
        $this->host = $host;
37
    }
38
39
    public function map(Email $message): Swift_Mime_SimpleMessage
40
    {
41
        $message = $this->addMtaHeadersToMessage($message);
42
43
        $swiftMessage = parent::map($message);
44
45
        return $swiftMessage;
46
    }
47
48
    /**
49
     * Adds Mandrill specific headers to the message
50
     *
51
     * @see http://help.mandrill.com/entries/21688056-Using-SMTP-Headers-to-customize-your-messages
52
     */
53
    protected function addMtaHeadersToMessage(Email $message)
54
    {
55
        $tracking = [];
56
57
        if ($message->shouldTrackClicks()) {
58
            $tracking[] = 'clicks_all';
59
        }
60
61
        if ($message->shouldTrackMessageOpening()) {
62
            $tracking[] = 'opens';
63
        }
64
65
        if (!empty($tracking)) {
66
            $message->addHeader('X-MC-Track', implode(',', $tracking));
67
        }
68
69
        $tags = $message->getTags();
70
        if (!empty($tags)) {
71
            $message->addHeader('X-MC-Tags', implode(',', $tags));
72
        }
73
74
        $googleCampaign = $message->getTrackingCampaign();
75
        if (!empty($googleCampaign)) {
76
            $message->addHeader('X-MC-GoogleAnalytics', $this->host);
77
            $message->addHeader('X-MC-GoogleAnalyticsCampaign', $googleCampaign);
78
        }
79
80
        return $message;
81
    }
82
}
83