ExampleTemplateProvider::addCustomHeaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Azine\EmailBundle\Services;
4
5
/**
6
 * This Service provides the templates and template-variables to be used for emails
7
 * This class is only an example. Implement your own!
8
 *
9
 * @codeCoverageIgnore
10
 *
11
 * @author Dominik Businger
12
 */
13
class ExampleTemplateProvider extends AzineTemplateProvider implements TemplateProviderInterface
14
{
15
    // design your own twig-templates for your custom emails
16
    // and list them here as constants, to avoid typos.
17
    const VIP_INFO_MAIL_TEMPLATE = 'AcmeExampleEmailBundle:VIP:infoMail';
18
    const SOME_OTHER_MAIL_TEMPLATE = 'AcmeExampleEmailBundle:Foo:marMail';
19
    // and also design your own base-template that should be used for newsletter/notification-emails
20
    // and define the template IDs for the newsletter- and notification-emails in your config.yml
21
22
    /**
23
     * @see Azine\EmailBundle\Services\AzineTemplateProvider::getParamArrayFor()
24
     *
25
     * @param string $template
26
     *
27
     * @return array
28
     */
29
    protected function getParamArrayFor($template)
30
    {
31
        // get the style-params from the parent (if you like)
32
        $newVars = parent::getParamArrayFor($template);
33
34
        // If you configured two SwiftMailers, one with spooling and one without, then
35
        // all templates that have set the "SEND_IMMEDIATELY_FLAG = true" will be sent with the
36
        // mailer that does not use spooling => faster email delivery => e.g. for the Reset-Password-Email.
37
        if (self::VIP_INFO_MAIL_TEMPLATE == $template) {
38
            $newVars[self::SEND_IMMEDIATELY_FLAG] = true;
39
        }
40
41
        // add template specific stuff
42
        if (self::NOTIFICATIONS_TEMPLATE == $template) {
43
            $newVars['%someUrl%'] = 'http://example.com'; 				//$this->router->generate("your_route", $routeParamArray, UrlGeneratorInterface::ABSOLUTE_URL);
44
            $newVars['%someOtherUrl%'] = 'http://example.com/other';	//$this->router->generate("your_route", $routeParamArray, UrlGeneratorInterface::ABSOLUTE_URL);
45
        }
46
47
        // override some generic stuff needed for all templates
48
        $newVars['h2Style'] = "style='padding:0; margin:0; font:bold 24px Arial; color:red; text-decoration:none;'";
49
50
        // add an image that should be embedded into the html-email.
51
        $newVars['someRandomImage'] = $this->getTemplateImageDir().'someRandomImage.png';
52
        // after the image has been added here, it will be base64-encoded so it can be embedded into a html-snippet
53
        // see self::getSnippetArrayFor()
54
55
        return $newVars;
56
    }
57
58
    /**
59
     * @see Azine\EmailBundle\Services\AzineTemplateProvider::getSnippetArrayFor()
60
     *
61
     * @param string $template
62
     * @param array  $vars
63
     * @param string $emailLocale
64
     *
65
     * @return array
66
     *
67
     * @throws \Exception
68
     */
69
    protected function getSnippetArrayFor($template, array $vars, $emailLocale)
70
    {
71
        // add a code snippet to reference the random image you added in the getParamArrayFor() method.
72
        // in the mean time it has been base64-encoded and attached as mime-part to your email.
73
        $vars['sampleSnippetWithImage'] = "<img src='".$vars['logo_png']."'>";
74
        // with this html-snippet you can display the "someRandomImage.png" from your
75
        // template-folder like this in your twig-template:   .... {{ sampleSnippetWithImage }} ...
76
77
        return parent::getSnippetArrayFor($template, $vars, $emailLocale);
78
    }
79
80
    /**
81
     * @see Azine\EmailBundle\Services\AzineTemplateProvider::addCustomHeaders()
82
     *
83
     * @param string         $template
84
     * @param \Swift_Message $message
85
     * @param array          $params
86
     *
87
     * @return array
88
     */
89
    public function addCustomHeaders($template, \Swift_Message $message, array $params)
90
    {
91
        // see http://documentation.mailgun.com/user_manual.html#attaching-data-to-messages
92
        // for an idea what could be added here.
93
        //$headerSet = $message->getHeaders();
94
        //$headerSet->addTextHeader($name, $value);
95
    }
96
97
    /**
98
     * @see Azine\EmailBundle\Services\AzineTemplateProvider::getCampaignParamsFor()
99
     *
100
     * @param $templateId
101
     * @param array $params
102
     *
103
     * @return array
104
     */
105
    public function getCampaignParamsFor($templateId, array $params = null)
106
    {
107
        $campaignParams = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $campaignParams is dead and can be removed.
Loading history...
108
        //if ($templateId == "AcmeFooBundle:bar:mail.template") {
109
        //      $campaignParams[$this->tracking_params_campaign_name] = "foo-bar-campaign";
110
        //      $campaignParams[$this->tracking_params_campaign_term] = "keyword";
111
        //} else {
112
        // get some other params
113
        $campaignParams = parent::getCampaignParamsFor($templateId, $params);
114
        //}
115
        return $campaignParams;
116
    }
117
118
    /**
119
     * Override this function to define which emails you want to make the web-view available and for which not.
120
     *
121
     * @see Azine\EmailBundle\Services\AzineTemplateProvider::getTemplatesToStoreForWebView()
122
     *
123
     * @return array
124
     */
125
    public function getTemplatesToStoreForWebView()
126
    {
127
        $include = parent::getTemplatesToStoreForWebView();
128
        $include[] = self::VIP_INFO_MAIL_TEMPLATE;
129
130
        return $include;
131
    }
132
}
133