GoogleCampaignPlugin::getCampaign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Openbuildings\Swiftmailer;
4
5
use Swift_Events_SendListener;
6
use Swift_Events_SendEvent;
7
8
/**
9
 * @author     Yasen Yanev <[email protected]>
10
 * @copyright  (c) 2014 Clippings Ltd.
11
 * @license    http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class GoogleCampaignPlugin implements Swift_Events_SendListener
14
{
15
    const CAMPAIGN_QUERY_PARAM = 'google_campaign';
16
17
    /**
18
     * Embed campaigns into the newsletter and return the updated html
19
     *
20
     * @param  string $html                 the email content
21
     * @param  array  $campaign             the general campaign for the newsletter
22
     * @param  array  $additionalCampaigns  additional campaigns to be replaced
23
     * @return string $html                 html with updated hrefs
24
     */
25 1
    public static function embedCampaigns(
26
        $html,
27
        $campaign = [],
28
        $additionalCampaigns = []
29
    ) {
30 1
        $pattern = '/<a(\s[^>]*)href="([^"]*)"([^>]*)>/si';
31
32
        $html = preg_replace_callback($pattern, function($matches) use ($campaign, $additionalCampaigns) {
33 1
            $href = GoogleCampaignPlugin::replaceLink($matches[2], $campaign, $additionalCampaigns);
34 1
            return "<a{$matches[1]}href=\"{$href}\"{$matches[3]}>";
35 1
        }, $html);
36
37 1
        return $html;
38
    }
39
40
    /**
41
     * Append campaign parameters to the href attribute of $element object
42
     * or replace `google_campaign` parameter with the correct campaign params
43
     *
44
     * @param  string  $href                  the href which needs to be replaced
45
     * @param  array   $campaign              the general campaign parameters
46
     * @param  array   $additionalCampaigns  additional campaigns for the newsletter
47
     * @return DomNode the $element with replaced href attribute
48
     */
49 1
    public static function replaceLink($href, $campaign = array(), $additionalCampaigns = array())
50
    {
51 1
        $href = html_entity_decode($href);
52 1
        $params = array();
53 1
        $parts = explode('?', $href);
54 1
        $uri = $parts[0];
55
56 1
        if (isset($parts[1])) {
57 1
            parse_str($parts[1], $params);
58
        }
59
60 1
        if ( ! count(array_intersect_key($campaign, $params)) and ! array_key_exists(GoogleCampaignPlugin::CAMPAIGN_QUERY_PARAM, $params)) {
61 1
            $params = array_merge($params, $campaign);
62
        } elseif (
63 1
            array_key_exists(GoogleCampaignPlugin::CAMPAIGN_QUERY_PARAM, $params)
64 1
            and $campaign_name = $params[GoogleCampaignPlugin::CAMPAIGN_QUERY_PARAM]
65 1
            and isset($additionalCampaigns[$campaign_name])
66
        ) {
67 1
            unset($params[GoogleCampaignPlugin::CAMPAIGN_QUERY_PARAM]);
68 1
            $params = array_merge($params, $additionalCampaigns[$campaign_name]);
69
        }
70
71 1
        if (count($params)) {
72 1
            $uri .= '?'.urldecode(http_build_query($params));
73
        }
74
75 1
        return $uri;
76
    }
77
78
    private $campaign;
79
    private $additionalCampaigns;
80
81 1
    public function __construct(array $campaign, $additionalCampaigns = [])
82
    {
83 1
        $this->campaign = $campaign;
84 1
        $this->additionalCampaigns = $additionalCampaigns;
85 1
    }
86
87 1
    public function getCampaign()
88
    {
89 1
        return $this->campaign;
90
    }
91
92 1
    public function setCampaign(array $campaign)
93
    {
94 1
        $this->campaign = $campaign;
95 1
    }
96
97 1
    public function getAdditionalCampaigns()
98
    {
99 1
        return $this->additionalCampaigns;
100
    }
101
102 1
    public function setAdditionalCampaigns(array $campaigns)
103
    {
104 1
        $this->additionalCampaigns = $campaigns;
105 1
    }
106
107
    /**
108
     *
109
     * @param Swift_Events_SendEvent $event
110
     */
111 3
    public function beforeSendPerformed(Swift_Events_SendEvent $event)
112
    {
113 3
        $message = $event->getMessage();
114
115 3
        if ($message->getContentType() === 'text/html') {
116 1
            $html = GoogleCampaignPlugin::embedCampaigns(
117 1
                $message->getBody(),
118 1
                $this->getCampaign(),
119 1
                $this->getAdditionalCampaigns()
120
            );
121
122 1
            $message->setBody($html);
123
        }
124
125 3
        foreach ($message->getChildren() as $part) {
126 2
            if (strpos($part->getContentType(), 'text/html') !== false) {
127 2
                $html = GoogleCampaignPlugin::embedCampaigns(
128 2
                    $part->getBody(),
129 2
                    $this->getCampaign(),
130 2
                    $this->getAdditionalCampaigns()
131
                );
132
133 2
                $part->setBody($html);
134
            }
135
        }
136 3
    }
137
138
    /**
139
     * Do nothing
140
     * @codeCoverageIgnore
141
     * @param Swift_Events_SendEvent $event
142
     */
143
    public function sendPerformed(Swift_Events_SendEvent $event)
144
    {
145
        // Do Nothing
146
    }
147
}
148