LaminasView::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Laminas\View\Renderer\RendererInterface;
14
15
/**
16
 * An e-mail resolver that uses Laminas\View.
17
 */
18
final class LaminasView 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 (is_string($this->subjects[$channel])) {
65
            return $this->subjects[$channel];
66
        }
67
68
        if (!array_key_exists($locale, $this->subjects[$channel])) {
69
            return '';
70
        }
71
72
        $subject = $this->subjects[$channel][$locale];
73
74
        // Only translate when the i18n module has been loaded
75
        if (method_exists($this->renderer, 'translate')) {
76
            $subject = $this->renderer->translate($subject);
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Laminas\View\Renderer\RendererInterface>.

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.

Loading history...
77
        }
78
79
        return $subject;
80
    }
81
82
    /**
83
     * Resolves the template for the given channel and type.
84
     *
85
     * @param Message $message The message to get the template for.
86
     * @param string $type The type of e-mail to resolve.
87
     * @return null|string Returns the resolved template as a string or null when no template exists.
88
     */
89
    public function resolveTemplate(Message $message, string $type): ?string
90
    {
91
        $channel = $message->getChannel();
92
        $locale = $message->getOptions()['locale'];
93
94
        if (!array_key_exists($channel, $this->templates)) {
95
            return null;
96
        }
97
98
        if (!array_key_exists($locale, $this->templates[$channel])) {
99
            return null;
100
        }
101
102
        if (!array_key_exists($type, $this->templates[$channel][$locale])) {
103
            return null;
104
        }
105
106
        return $this->renderer->render($this->templates[$channel][$locale][$type], $message->getParams());
107
    }
108
}
109