|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Communicator (https://github.com/waltertamboer/communicator) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/waltertamboer/communicator for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2017 Communicator (https://github.com/waltertamboer/communicator) |
|
7
|
|
|
* @license https://github.com/waltertamboer/communicator/blob/master/LICENSE.md MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Communicator\Transport\Email\Resolver; |
|
11
|
|
|
|
|
12
|
|
|
use Communicator\Message; |
|
13
|
|
|
use Zend\View\Renderer\RendererInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* An e-mail resolver that uses Zend\View. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ZendView implements ResolverInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var RendererInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $renderer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $templates; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $subjects; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initializes a new instance of this class. |
|
37
|
|
|
* |
|
38
|
|
|
* @param RendererInterface $renderer |
|
39
|
|
|
* @param array $templates |
|
40
|
|
|
* @param array $subjects |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(RendererInterface $renderer, array $templates, array $subjects) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->renderer = $renderer; |
|
45
|
|
|
$this->templates = $templates; |
|
46
|
|
|
$this->subjects = $subjects; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Resolves the subject for the given channel and type. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Message $message The message to get the subject for. |
|
53
|
|
|
* @return null|string Returns the resolved subject as a string. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function resolveSubject(Message $message): string |
|
56
|
|
|
{ |
|
57
|
|
|
$channel = $message->getChannel(); |
|
58
|
|
|
$locale = $message->getOptions()['locale']; |
|
59
|
|
|
|
|
60
|
|
|
if (!array_key_exists($channel, $this->subjects)) { |
|
61
|
|
|
return ''; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!array_key_exists($locale, $this->subjects[$channel])) { |
|
65
|
|
|
return ''; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$subject = $this->subjects[$channel][$locale]; |
|
69
|
|
|
|
|
70
|
|
|
// Only translate when the i18n module has been loaded |
|
71
|
|
|
if (method_exists($this->renderer, 'translate')) { |
|
72
|
|
|
$subject = $this->renderer->translate($subject); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $subject; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Resolves the template for the given channel and type. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Message $message The message to get the template for. |
|
82
|
|
|
* @param string $type The type of e-mail to resolve. |
|
83
|
|
|
* @return null|string Returns the resolved template as a string or null when no template exists. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function resolveTemplate(Message $message, string $type): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
$channel = $message->getChannel(); |
|
88
|
|
|
$locale = $message->getOptions()['locale']; |
|
89
|
|
|
|
|
90
|
|
|
if (!array_key_exists($channel, $this->templates)) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!array_key_exists($locale, $this->templates[$channel])) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!array_key_exists($type, $this->templates[$channel][$locale])) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->renderer->render($this->templates[$channel][$locale][$type], $message->getParams()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.