Completed
Push — master ( 081c79...74281f )
by Guillaume
02:59
created

OctosendHtmlRender   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 18
loc 75
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 14 1
A renderMirror() 0 6 1
A renderPixel() 0 6 1
A renderLien() 0 17 4
A renderDesinscription() 9 9 2
A renderClick() 9 9 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($key, $contenu)
37
    {
38
        if (!in_array($key, ["unsub", "click"])) {
39
            throw new \InvalidArgumentException();
40
        }
41
        $arrayContenu = [];
42
        preg_match_all("#<a data-id=\"".$key."\" target=\"__blank\" href=\"(.*?)\" style=\"(.*?)\">(.*?)</a>#", $contenu, $arrayContenu);
43
        if (empty($arrayContenu[0])) {
44
            return $contenu;
45
        }
46
        if ($key == "unsub") {
47
            return $this->renderDesinscription($contenu, $arrayContenu);
48
        }
49
50
        return $this->renderClick($contenu, $arrayContenu);
51
52
    }
53
54 View Code Duplication
    protected function renderDesinscription($contenu, $arrayContenu)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        foreach ($arrayContenu[0] as $key => $chaineARemplacer) {
57
            $chaineOctoSend = "<a href='{{unsubscribe:".$arrayContenu[1][$key]."}}' style='".$arrayContenu[2][$key]."' title='Désinscription'>".$arrayContenu[3][$key].'</a>';
58
            $contenu = str_replace($chaineARemplacer, $chaineOctoSend, $contenu);
59
        }
60
61
        return $contenu;
62
    }
63
64 View Code Duplication
    protected function renderClick($contenu, $arrayContenu)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        foreach ($arrayContenu[0] as $key => $chaineARemplacer) {
67
            $chaineOctoSend = "<a href='{{click:".$arrayContenu[1][$key]."}}' style='".$arrayContenu[2][$key]."'>".$arrayContenu[3][$key].'</a>';
68
            $contenu = str_replace($chaineARemplacer, $chaineOctoSend, $contenu);
69
        }
70
71
        return $contenu;
72
    }
73
74
    public function getSupport($api, $version)
75
    {
76
        return strtolower($api) == 'octosend' && $version == 'html';
77
    }
78
79
}
80