Completed
Pull Request — develop (#459)
by ANTHONIUS
10:26
created

StringTemplateMessage::setTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** TextTemplateMessage.php */
11
namespace Core\Mail;
12
13
use Zend\Mail\Header;
14
15
/**
16
 * Class StringTemplateMessage
17
 *
18
 * @author Anthonius Munthi <[email protected]>
19
 * @package Core\Mail
20
 */
21
class StringTemplateMessage extends TranslatorAwareMessage
22
{
23
    protected $variables;
24
    protected $callbacks;
25
    protected $template;
26
    
27
    
28
    public function __construct(array $options = array())
29
    {
30
        parent::__construct($options);
31
        $this->variables = array();
32
        $this->callbacks = array();
33
        $this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/plain; charset=UTF-8'));
34
        $this->setEncoding('UTF-8');
35
    }
36
    
37
    public function setVariables($variables = array())
38
    {
39
        $this->variables = array();
40
        return $this->addVariables($variables);
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getVariables()
47
    {
48
        return $this->variables;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getCallbacks()
55
    {
56
        return $this->callbacks;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getTemplate()
63
    {
64
        return $this->template;
65
    }
66
67
    /**
68
     * @param string $template
69
     * @return $this
70
     */
71
    public function setTemplate($template)
72
    {
73
        $this->template = $template;
74
75
        return $this;
76
    }
77
    
78
    public function addVariables($variables = array())
79
    {
80 View Code Duplication
        if (!is_array($variables)) {
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...
81
            if (!$variables instanceof \Traversable) {
82
                throw new \InvalidArgumentException(
83
                    sprintf(
84
                        'Expect an array or an instance of \Traversable, but received %s',
85
                        is_object($variables) ? 'instance of ' . get_class($variables) : 'skalar'
86
                    )
87
                );
88
            }
89
            $variables = \Zend\Stdlib\ArrayUtils::iteratorToArray($variables);
90
        }
91
        
92
        $this->variables = array_merge($this->variables, $variables);
93
        return $this;
94
    }
95
    
96
    public function setVariable($name, $value)
97
    {
98
        $this->variables[$name] = $value;
99
        return $this;
100
    }
101
    
102
    public function setCallbacks($callbacks = array())
103
    {
104
        $this->callbacks = array();
105
        return $this->addCallbacks($callbacks);
106
    }
107
    
108
    public function addCallbacks($callbacks = array())
109
    {
110 View Code Duplication
        if (!is_array($callbacks)) {
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...
111
            if (!$callbacks instanceof \Traversable) {
112
                throw new \InvalidArgumentException(
113
                    sprintf(
114
                        'Expect an array or an instance of \Traversable, but received %s',
115
                        is_object($callbacks) ? 'instance of ' . get_class($callbacks) : 'skalar'
116
                    )
117
                );
118
            }
119
        }
120
        
121
        foreach ($callbacks as $name => $callback) {
122
            $this->setCallback($name, $callback);
123
        }
124
        return $this;
125
    }
126
    
127
    public function setCallback($name, $callable)
128
    {
129
        if (!is_string($callable) && !is_callable($callable)) {
130
            throw new \InvalidArgumentException('Provided callback is not callable');
131
        }
132
        $this->callbacks[$name] = $callable;
133
    }
134
    
135
    public function getBodyText()
136
    {
137
        $body = parent::getBodyText();
138
        $body = $this->parseVariables($body);
139
        $body = $this->parseCallbacks($body);
140
        
141
        if (preg_match('~\+\+subject:(?P<subject>.*?)\+\+~is', $body, $match)) {
142
            $this->setSubject(trim($match['subject']));
143
            $body = str_replace($match[0], '', $body);
144
            $body = trim($body);
145
        }
146
        
147
        return $body;
148
    }
149
    
150
    protected function parseVariables($body)
151
    {
152
        if (empty($this->variables)) {
153
            return $body;
154
        }
155
        
156
        $variableNames = array_map(array($this, 'getNamePattern'), array_keys($this->variables));
157
        $variableValues = array_values($this->variables);
158
        $body = preg_replace($variableNames, $variableValues, $body);
159
        return $body;
160
    }
161
    
162
    protected function parseCallbacks($body)
163
    {
164
        if (null == $this->callbacks) {
165
            return $body;
166
        }
167
        
168
        foreach ($this->callbacks as $name => $callable) {
169
            $pattern = $this->getNamePattern($name);
170
            
171
            if (preg_match($pattern, $body)) {
172
                if (is_string($callable)) {
173
                    if (!method_exists($this, $callable)) {
174
                        continue;
175
                    }
176
                    $value = $this->$callable();
177
                } else {
178
                    $value = call_user_func($callable);
179
                }
180
                $body = preg_replace($pattern, $value, $body);
181
            }
182
        }
183
        return $body;
184
    }
185
    
186
    protected function getNamePattern($name)
187
    {
188
        return '~##' . preg_quote($name) . '##~is';
189
    }
190
}
191