Completed
Pull Request — develop (#542)
by Mathias
09:01
created

HTMLTemplateMessage   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Test Coverage

Coverage 80.48%

Importance

Changes 0
Metric Value
wmc 30
eloc 68
dl 0
loc 250
ccs 66
cts 82
cp 0.8048
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B setVariables() 0 26 8
A __isset() 0 4 1
A getVariables() 0 3 1
A getVariable() 0 8 2
A factory() 0 3 1
A clearVariables() 0 4 1
A __construct() 0 10 1
A setVariable() 0 4 1
A __set() 0 3 1
A __get() 0 8 2
A getTemplate() 0 3 1
A __unset() 0 4 2
A setTemplate() 0 4 1
A getBodyText() 0 4 1
B renderBodyText() 0 34 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Core\Mail;
12
13
use Interop\Container\ContainerInterface;
14
use Zend\Mail\Header;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
use Zend\View\Model\ViewModel;
17
use Zend\Stdlib\Response;
18
use Zend\View\Variables as ViewVariables;
19
use Zend\Stdlib\ArrayUtils;
20
21
/**
22
 * Class HTMLTemplateMessage.
23
 * uses methods alike ViewModel
24
 * @package Core\Mail
25
 */
26
class HTMLTemplateMessage extends TranslatorAwareMessage
27
{
28
    /**
29
     * @var ServiceLocatorInterface
30
     */
31
    protected $serviceManager;
32
33
    /**
34
     * View variables
35
     * @var array|\ArrayAccess|\Traversable
36
     */
37
    protected $variables = array();
38
39
    protected $renderedBody;
40
    
41
    /**
42
     * HTMLTemplateMessage constructor.
43
     *
44
     * @param ContainerInterface $serviceManager
45
     * @param array $options
46
     */
47 8
    public function __construct(
48
        ContainerInterface $serviceManager,
49
        array $options = array()
50
    ) {
51
        // @TODO make this multipart
52 8
        parent::__construct($options);
53 8
        $this->serviceManager = $serviceManager;
0 ignored issues
show
Documentation Bug introduced by
$serviceManager is of type Interop\Container\ContainerInterface, but the property $serviceManager was declared to be of type Zend\ServiceManager\ServiceLocatorInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
54 8
        $this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/html; charset=UTF-8'));
55 8
        $this->setEncoding('UTF-8');
56 8
        $this->variables = new ViewVariables();
57 8
    }
58
59
    /**
60
     * Property overloading: set variable value
61
     *
62
     * @param  string $name
63
     * @param  mixed $value
64
     * @return void
65
     */
66 2
    public function __set($name, $value)
67
    {
68 2
        $this->setVariable($name, $value);
69 2
    }
70
71
    /**
72
     * Property overloading: get variable value
73
     *
74
     * @param  string $name
75
     * @return mixed
76
     */
77 3
    public function __get($name)
78
    {
79 3
        if (!$this->__isset($name)) {
80 2
            return null;
81
        }
82
83 3
        $variables = $this->getVariables();
84 3
        return $variables[$name];
85
    }
86
87
    /**
88
     * Property overloading: do we have the requested variable value?
89
     *
90
     * @param  string $name
91
     * @return bool
92
     */
93 3
    public function __isset($name)
94
    {
95 3
        $variables = $this->getVariables();
96 3
        return isset($variables[$name]);
97
    }
98
99
    /**
100
     * Property overloading: unset the requested variable
101
     *
102
     * @param  string $name
103
     * @return void
104
     */
105 1
    public function __unset($name)
106
    {
107 1
        if ($this->__isset($name)) {
108 1
            unset($this->variables[$name]);
109
        }
110 1
    }
111
112
    /**
113
     * Get a single view variable
114
     *
115
     * @param  string       $name
116
     * @param  mixed|null   $default (optional) default value if the variable is not present.
117
     * @return mixed
118
     */
119 4
    public function getVariable($name, $default = null)
120
    {
121 4
        $name = (string) $name;
122 4
        if (array_key_exists($name, $this->variables)) {
0 ignored issues
show
Bug introduced by
It seems like $this->variables can also be of type ArrayAccess and Traversable; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
        if (array_key_exists($name, /** @scrutinizer ignore-type */ $this->variables)) {
Loading history...
123 2
            return $this->variables[$name];
124
        }
125
126 3
        return $default;
127
    }
128
129
    /**
130
     * Set view variable
131
     *
132
     * @param  string $name
133
     * @param  mixed $value
134
     * @return ViewModel
135
     */
136 5
    public function setVariable($name, $value)
137
    {
138 5
        $this->variables[(string) $name] = $value;
139 5
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Core\Mail\HTMLTemplateMessage which is incompatible with the documented return type Zend\View\Model\ViewModel.
Loading history...
140
    }
141
142
    /**
143
     * Set view variables en masse
144
     *
145
     * Can be an array or a Traversable + ArrayAccess object.
146
     *
147
     * @param  array|\ArrayAccess|\Traversable $variables
148
     * @param  bool $overwrite Whether or not to overwrite the internal container with $variables
149
     * @throws \InvalidArgumentException
150
     * @return self
151
     */
152 2
    public function setVariables($variables, $overwrite = false)
153
    {
154 2
        if (!is_array($variables) && !$variables instanceof \Traversable) {
155 1
            throw new \InvalidArgumentException(
156 1
                sprintf(
157 1
                    '%s: expects an array, or Traversable argument; received "%s"',
158 1
                    __METHOD__,
159 1
                    (is_object($variables) ? get_class($variables) : gettype($variables))
160
                )
161
            );
162
        }
163
164 2
        if ($overwrite) {
165 1
            if (is_object($variables) && !$variables instanceof \ArrayAccess) {
166 1
                $variables = ArrayUtils::iteratorToArray($variables);
167
            }
168
169 1
            $this->variables = $variables;
170 1
            return $this;
171
        }
172
173 2
        foreach ($variables as $key => $value) {
174 2
            $this->setVariable($key, $value);
175
        }
176
177 2
        return $this;
178
    }
179
180
    /**
181
     * Get view variables
182
     *
183
     * @return array|\ArrayAccess|\Traversable
184
     */
185 4
    public function getVariables()
186
    {
187 4
        return $this->variables;
188
    }
189
190
    /**
191
     * Clear all variables
192
     *
193
     * Resets the internal variable container to an empty container.
194
     *
195
     * @return ViewModel
196
     */
197 1
    public function clearVariables()
198
    {
199 1
        $this->variables = new ViewVariables();
200 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Core\Mail\HTMLTemplateMessage which is incompatible with the documented return type Zend\View\Model\ViewModel.
Loading history...
201
    }
202
203
    /**
204
     *
205
     * @param $template
206
     *
207
     * @return self
208
     */
209 1
    public function setTemplate($template)
210
    {
211 1
        $this->template = $template;
0 ignored issues
show
Bug Best Practice introduced by
The property template does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
212 1
        return $this;
213
    }
214
215 1
    public function getTemplate()
216
    {
217 1
        return $this->template;
218
    }
219
220
    /**
221
     * @example /module/Jobs/src/Jobs/Listener/PortalListener.php
222
     * @return string
223
     * @throws \InvalidArgumentException the mail body must completely be provided by the template, any other attempt is a misconception that may leave the coder in an quagmire
224
     */
225 1
    public function renderBodyText($force = true, $forceLanguage = null)
226
    {
227 1
        if (!$this->renderedBody || $force) {
228 1
            $viewModel    = new ViewModel();
229 1
            $response     = new Response();
230 1
            $body         = parent::getBodyText();
231 1
            if (!empty($body)) {
232 1
                throw new \InvalidArgumentException('mail body shall come from Template.');
233
            }
234
235
            /* @var \Zend\Mvc\View\Http\ViewManager $viewManager */
236
            $viewManager  = $this->serviceManager->get('ViewManager');
237
            $resolver = $this->serviceManager->get('ViewResolver');
238
239
            /* @var \Zend\Mvc\MvcEvent $event */
240
            $event = $this->serviceManager->get('Application')->getMvcEvent();
241
            $lang = $forceLanguage ?: $event->getRouteMatch()->getParam('lang');
242
243
244
            if ($resolver->resolve($this->getTemplate() . '.' . $lang)) {
245
                $viewModel->setTemplate($this->getTemplate() . '.' . $lang);
246
            } else {
247
                $viewModel->setTemplate($this->getTemplate());
248
            }
249
250
            $view         = $viewManager->getView();
251
252
            $viewModel->setVariables($this->getVariables());
253
            $viewModel->setVariable('mail', $this);
254
            $view->setResponse($response);
255
            $view->render($viewModel);
256
            $body = $response->getContent();
257
258
            $this->renderedBody = $body;
259
        }
260
    }
261
262 1
    public function getBodyText()
263
    {
264 1
        $this->renderBodyText();
265
        return $this->renderedBody;
266
    }
267
    
268
    /**
269
     * @param ContainerInterface $container
270
     *
271
     * @return static
272
     */
273 1
    public static function factory(ContainerInterface $container)
274
    {
275 1
        return new static($container);
276
    }
277
}
278