Completed
Branch master (6e9dcf)
by Dominik
02:26
created

addCampaignParamsForTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
namespace Azine\EmailBundle\Services;
3
4
use Symfony\Component\Translation\TranslatorInterface;
5
6
class AzineEmailTwigExtension extends \Twig_Extension
7
{
8
    /**
9
     * @var TemplateProviderInterface
10
     */
11
    private $templateProvider;
12
13
    /**
14
     * @var TranslatorInterface
15
     */
16
    private $translator;
17
18 5
    public function __construct(TemplateProviderInterface $templateProvider, TranslatorInterface $translator){
19 5
        $this->templateProvider = $templateProvider;
20 5
        $this->translator = $translator;
21 5
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function getFilters()
27
    {
28
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('textWrap' ...e' => array('html')))); (array<string,Twig_SimpleFilter>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFilters of type Twig_SimpleFilter[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
29 1
            'textWrap' => new \Twig_SimpleFilter('textWrap', array($this, 'textWrap')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30 1
            'urlEncodeText' => new \Twig_SimpleFilter('urlEncodeText', array($this, 'urlEncodeText'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31 1
            'addCampaignParamsForTemplate' => new \Twig_SimpleFilter('addCampaignParamsForTemplate', array($this, 'addCampaignParamsForTemplate'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32 1
        );
33
    }
34
35 1
    public function urlEncodeText($text)
36
    {
37 1
        $text = str_replace("%","%25", $text);
38 1
        $text = str_replace(array(	"\n",
39 1
                                    " ",
40 1
                                    "&",
41 1
                                    "\\",
42 1
                                    "<",
43 1
                                    ">",
44 1
                                    '"',
45 1
                                    "	",
46 1
                                ),
47 1
                            array(	"%0D%0A",
48 1
                                    "%20",
49 1
                                    "%26",
50 1
                                    "%5C",
51 1
                                    "%3D",
52 1
                                    "%3E",
53 1
                                    "%23",
54 1
                                    "%09",
55 1
                                ), $text);
56
57 1
        return $text;
58
    }
59
60
    /**
61
     * Wrap the text to the lineLength is not exeeded.
62
     * @param  string  $text
63
     * @param  integer $lineLength default: 75
64
     * @return string  the wrapped string
65
     */
66 3
    public function textWrap($text, $lineLength = 75)
67
    {
68 3
        return wordwrap($text, $lineLength);
69
    }
70
71
    /**
72
     * Returns the name of the extension.
73
     *
74
     * @return string The extension name
75
     */
76 1
    public function getName()
77
    {
78 1
        return 'azine_email_bundle_twig_extension';
79
    }
80
81
    public function addCampaignParamsForTemplate($html, $templateId, $templateParams){
82
        $campaignParams = $this->templateProvider->getCampaignParamsFor($templateId, $templateParams, $this->translator->getLocale());
0 ignored issues
show
Unused Code introduced by
The call to TemplateProviderInterface::getCampaignParamsFor() has too many arguments starting with $this->translator->getLocale().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
        return $this->addCampaignParamsToAllUrls($html, $campaignParams);
84
    }
85
86
    /**
87
     * Add the campaign-parameters to all URLs in the html
88
     * @param  string $html
89
     * @param  array  $campaignParams
90
     * @return string
91
     */
92 7
    public static function addCampaignParamsToAllUrls($html, $campaignParams)
93
    {
94
95 7
        $urlPattern = '/(href=[\'|"])(http[s]?\:\/\/\S*)([\'|"])/';
96
97 7
        $filteredHtml = preg_replace_callback($urlPattern, function ($matches) use ($campaignParams) {
98 7
                                                                    $start = $matches[1];
99 7
                                                                    $url = $matches[2];
100 7
                                                                    $end = $matches[3];
101
102
                                                                    // avoid duplicate params and don't replace existing params
103 7
                                                                    $params = array();
104 7
                                                                    foreach($campaignParams as $nextKey => $nextValue){
105 7
                                                                        if(strpos($url, $nextKey) === false){
106 7
                                                                            $params[$nextKey] = $nextValue;
107 7
                                                                        }
108 7
                                                                    }
109
110 7
                                                                    $urlParams = http_build_query($params);
111
112 7
                                                                    if (strpos($url,"?") === false) {
113 7
                                                                        $urlParams = "?".$urlParams;
114 7
                                                                    } else {
115 7
                                                                        $urlParams = "&".$urlParams;
116
                                                                    }
117
118 7
                                                                    $replacement = $start.$url.$urlParams.$end;
119
120 7
                                                                    return $replacement;
121
122 7
                                                                }, $html);
123
124 7
        return $filteredHtml;
125
    }
126
}
127