Completed
Pull Request — master (#39)
by Patrick
04:49 queued 01:30
created

DBEmail::getReplyTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Email;
3
require_once('Autoload.php');
4
5
abstract class DBEmail extends \Email\Email
6
{
7
    protected $dbData;
8
9
    public function __construct($datasetName, $datatableName, $dataRowName)
10
    {
11
        parent::__construct();
12
        $dataTable = \DataSetFactory::getDataTableByNames($datasetName, $datatableName);
13
        $this->dbData = $dataTable->read(new \Data\Filter('id eq '.$dataRowName));
14
        if($this->dbData === false)
15
        {
16
            throw new \Exception('Unknown dataRow identified by ID '.$dataRowName);
17
        }
18
    }
19
20
    public function getFromAddress()
21
    {
22
        if(isset($this->dbData['from']))
23
        {
24
            return $this->dbData['from'];;
25
        }
26
        return parent::getFromAddress();
27
    }
28
29
    public function getReplyTo()
30
    {
31
        if(isset($this->dbData['replyTo']))
32
        {
33
            return $this->dbData['replyTo'];;
34
        }
35
        return parent::getReplyTo();
36
    }
37
38
    public function getSubject()
39
    {
40
        if(isset($this->dbData['subject']))
41
        {
42
            return $this->dbData['subject'];
43
        }
44
        return parent::getSubject();
45
    }
46
47
    public abstract function getSubstituteVars();
48
49
    protected function getRawBodyText($html)
50
    {
51
        if(isset($this->dbData['body']))
52
        {
53
            return $this->dbData;
54
        }
55
        if($html === true)
56
        {
57
            return $this->htmlBody;
58
        }
59
        return $this->textBody;
60
    }
61
62
    protected function getBodyFromDB($html=true)
63
    {
64
        $vars = $this->getSubstituteVars();
65
        $rawText = $this->getRawBodyText($html);
66
        if($html === true)
67
        {
68
            $text = strtr($rawText, $vars);
69
            return $text;
70
        }
71
        $index = strpos($rawText, "<script");
72
        if($index !== false)
73
        {
74
            $end = strpos($rawText, "</script>");
75
            if($index === 0)
76
            {
77
                $rawText = substr($rawText, $end+9);
78
            }
79
        }
80
        return strtr(strip_tags($rawText), $vars);
81
    }
82
83
    public function getHTMLBody()
84
    {
85
        return $this->getBodyFromDB();
86
    }
87
88
    public function getTextBody()
89
    {
90
        return $this->getBodyFromDB(false);
91
    }
92
}
93
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
94