Passed
Push — master ( 8c805d...5e8702 )
by Sebastian
02:45
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Safeguard_URLAnalyzer} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_URLAnalyzer
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\ConvertHelper;
15
16
/**
17
 * Detects all URLs in the subject string, and tells all placeholders
18
 * that are contained in URLs, that they are in an URL. This allows
19
 * those commands to adjust themselves automatically if necessary.
20
 *
21
 * Example: showvar commands that automatically turn on URL encoding.
22
 *
23
 * @package Mailcode
24
 * @subpackage Parser
25
 * @author Sebastian Mordziol <[email protected]>
26
 */
27
class Mailcode_Parser_Safeguard_URLAnalyzer
28
{
29
    /**
30
     * @var string
31
     */
32
    private $subject;
33
34
    /**
35
     * @var Mailcode_Parser_Safeguard
36
     */
37
    private $safeguard;
38
39
    public function __construct(string $subject, Mailcode_Parser_Safeguard $safeguard)
40
    {
41
        $this->subject = $subject;
42
        $this->safeguard = $safeguard;
43
    }
44
45
    public function analyze() : void
46
    {
47
        $urls = ConvertHelper::createURLFinder($this->subject)
48
            ->includeEmails(false)
49
            ->getURLs();
50
51
        foreach ($urls as $url)
52
        {
53
            $this->analyzeURL($url);
54
        }
55
    }
56
57
    private function analyzeURL(string $url) : void
58
    {
59
        if(stristr($url, 'tel:'))
60
        {
61
            return;
62
        }
63
64
        $placeholders = $this->safeguard->getPlaceholders();
65
66
        foreach($placeholders as $placeholder)
67
        {
68
            $command = $placeholder->getCommand();
69
70
            if(!$command->supportsURLEncoding())
71
            {
72
                continue;
73
            }
74
75
            if(strstr($url, $placeholder->getReplacementText()) && !$command->isURLDecoded())
76
            {
77
                $command->setURLEncoding(true);
78
            }
79
        }
80
    }
81
}
82