Completed
Push — master ( cfb962...99cf21 )
by Guillaume
02:54
created

OctosendHtmlRender::retournerLaChaine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 3
Ratio 37.5 %

Importance

Changes 0
Metric Value
dl 3
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 4
1
<?php
2
3
namespace Starkerxp\CampagneBundle\Render;
4
5
class OctosendHtmlRender extends AbstractRender
6
{
7
    public function render()
8
    {
9
        // Gestion des liens mirror
10
        $contenu = $this->renderMirror($this->contenu);
11
        // Gestion des liens pixels
12
        $contenu = $this->renderPixel($contenu);
13
        // Gestion des liens de desinscriptions.
14
        $contenu = $this->renderLien("unsub", $contenu);
15
        // Gestion des liens click:http://
16
        $contenu = $this->renderLien("click", $contenu);
17
        $contenu = str_replace('  ', ' ', str_replace('  ', ' ', $contenu));
18
19
        return $contenu;
20
    }
21
22
    protected function renderMirror($contenu)
23
    {
24
        $contenuReplace = preg_replace('/\[\{\@mirror\}\]/', '{{mirror}}', $contenu);
25
26
        return $contenuReplace;
27
    }
28
29
    protected function renderPixel($contenu)
30
    {
31
        $contenuReplace = preg_replace('/\[\{\@pixel\}\]/', '{{pixel}}', $contenu);
32
33
        return $contenuReplace;
34
    }
35
36
    protected function renderLien($type, $contenu)
37
    {
38
        if (!in_array($type, ["unsub", "click"])) {
39
            throw new \InvalidArgumentException();
40
        }
41
        $arrayContenu = [];
42
        preg_match_all("#<a data-id=\"".$type."\" target=\"__blank\" href=\"(.*?)\" style=\"(.*?)\">(.*?)</a>#", $contenu, $arrayContenu);
43
        if (empty($arrayContenu[0])) {
44
            return $contenu;
45
        }
46
        foreach ($arrayContenu[0] as $key => $chaineARemplacer) {
47
            $chaineOctoSend = $this->retournerLaChaine($type, $arrayContenu[1][$key], $arrayContenu[3][$key], $arrayContenu[2][$key]);
48
            $contenu = str_replace($chaineARemplacer, $chaineOctoSend, $contenu);
49
        }
50
51
        return $contenu;
52
53
    }
54
55
    protected function retournerLaChaine($type, $lien, $texte, $style = null)
56
    {
57 View Code Duplication
        if ($type == "unsub") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            return "<a href='{{unsubscribe:".$lien."}}' style='".$style."' title='Désinscription'>".$texte.'</a>';
59
        }
60
61
        return "<a href='{{click:".$lien."}}' style='".$style."'>".$texte.'</a>';;
62
    }
63
64
    public function getSupport($api, $version)
65
    {
66
        return strtolower($api) == 'octosend' && $version == 'html';
67
    }
68
69
70
}
71