Passed
Push — master ( 5aa9a6...90bef3 )
by Sebastian
02:25
created

processPlaceholder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.9666
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Safeguard_Restorer} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Restorer
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * The restorer is used to determine which strings the
16
 * safeguard's placeholders have to be made whole with,
17
 * depending on the specified settings.
18
 *
19
 * @package Mailcode
20
 * @subpackage Parser
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Parser_Safeguard_Restorer
24
{
25
   /**
26
    * @var Mailcode_Parser_Safeguard
27
    */
28
    private $safeguard;
29
    
30
   /**
31
    * @var bool
32
    */
33
    private $highlighted;
34
    
35
   /**
36
    * @var bool
37
    */
38
    private $normalize;
39
    
40
   /**
41
    * @var array<string,string>
42
    */
43
    private $replaces = array();
44
    
45
    public function __construct(Mailcode_Parser_Safeguard $safeguard, bool $highlighted, bool $normalize)
46
    {
47
        $this->safeguard = $safeguard;
48
        $this->highlighted = $highlighted;
49
        $this->normalize = $normalize;
50
    }
51
    
52
   /**
53
    * @return array<string,string>
54
    */
55
    public function getReplaces() : array
56
    {
57
        $placeholders = $this->safeguard->getPlaceholders();
58
        
59
        $this->replaces = array();
60
        
61
        foreach($placeholders as $placeholder)
62
        {
63
            $this->processPlaceholder($placeholder);
64
        }
65
        
66
        return $this->replaces;
67
    }
68
    
69
    private function processPlaceholder(Mailcode_Parser_Safeguard_Placeholder $placeholder) : void
70
    {
71
        $replace = '';
72
        $needle = $placeholder->getReplacementText();
73
        
74
        if($this->highlighted)
75
        {
76
            $replace = $this->processPlaceholder_highlighted($placeholder, $needle);
77
        }
78
        else if($this->normalize)
79
        {
80
            $replace = $placeholder->getNormalizedText();
81
        }
82
        else
83
        {
84
            $replace = $placeholder->getOriginalText();
85
        }
86
        
87
        $this->replaces[$needle] = $replace;
88
    }
89
    
90
    private function processPlaceholder_highlighted(Mailcode_Parser_Safeguard_Placeholder $placeholder, string $needle) : string
91
    {
92
        $formatter = $this->safeguard->getFormatter();
93
        
94
        if(!$formatter)
0 ignored issues
show
introduced by
$formatter is of type Mailcode\Mailcode_Parser_Safeguard_Formatter, thus it always evaluated to true.
Loading history...
95
        {
96
            return $placeholder->getHighlightedText();
97
        }
98
        
99
        $formattedNeedle = $formatter->getReplaceNeedle($placeholder);
100
        
101
        if($formattedNeedle !== $needle)
102
        {
103
            $this->replaces[$formattedNeedle] = $placeholder->getHighlightedText();
104
            
105
            return $placeholder->getNormalizedText();
106
        }
107
        
108
        return $placeholder->getHighlightedText();
109
    }
110
}
111