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

OctosendHtmlRender   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 4.55 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 3
loc 66
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 14 1
A renderMirror() 0 6 1
A renderPixel() 0 6 1
A renderLien() 0 18 4
A retournerLaChaine() 3 8 2
A getSupport() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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