TwigFileRepository::renderBlock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace SfCod\EmailEngineBundle\Repository;
4
5
use SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException;
6
use SfCod\EmailEngineBundle\Template\TemplateInterface;
7
use SfCod\EmailEngineBundle\Template\TwigTemplateAwareInterface;
8
use Twig_Environment;
9
10
/**
11
 * Class TwigFileRepository
12
 *
13
 * @author Virchenko Maksim <[email protected]>
14
 *
15
 * @package SfCod\EmailEngineBundle\Repository
16
 */
17
class TwigFileRepository implements RepositoryInterface
18
{
19
    /**
20
     * Twig template
21
     *
22
     * @var string
23
     */
24
    protected $template;
25
26
    /**
27
     * @var Twig_Environment
28
     */
29
    protected $twig;
30
31
    /**
32
     * TwigFileRepository constructor.
33
     *
34
     * @param Twig_Environment $twig
35
     */
36
    public function __construct(Twig_Environment $twig)
37
    {
38
        $this->twig = $twig;
39
    }
40
41
    /**
42
     * Get subject template
43
     *
44
     * @param array $data
45
     *
46
     * @return string
47
     *
48
     * @throws \Throwable
49
     */
50
    public function getSubjectTemplate(array $data): string
51
    {
52
        return $this->renderBlock('subject', $data);
53
    }
54
55
    /**
56
     * Get body template
57
     *
58
     * @param array $data
59
     *
60
     * @return string
61
     *
62
     * @throws \Throwable
63
     */
64
    public function getBodyTemplate(array $data): string
65
    {
66
        return $this->renderBlock('content', $data);
67
    }
68
69
    /**
70
     * Get sender name template
71
     *
72
     * @param array $data
73
     *
74
     * @return string
75
     *
76
     * @throws \Throwable
77
     */
78
    public function getSenderNameTemplate(array $data): string
79
    {
80
        if ($this->template->hasBlock('sender_name')) {
0 ignored issues
show
Bug introduced by
The method hasBlock cannot be called on $this->template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
81
            return $this->renderBlock('sender_name', $data);
82
        }
83
84
        return getenv('SENDER_NAME');
85
    }
86
87
    /**
88
     * Get sender email template
89
     *
90
     * @param array $data
91
     *
92
     * @return string
93
     *
94
     * @throws \Throwable
95
     */
96
    public function getSenderEmailTemplate(array $data): string
97
    {
98
        if ($this->template->hasBlock('sender_email')) {
0 ignored issues
show
Bug introduced by
The method hasBlock cannot be called on $this->template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
            return $this->renderBlock('sender_email', $data);
100
        }
101
102
        return getenv('SENDER_EMAIL');
103
    }
104
105
    /**
106
     * Render twig template
107
     *
108
     * @param string $block
109
     * @param array $data
110
     *
111
     * @return string
112
     *
113
     * @throws \Throwable
114
     */
115
    protected function renderBlock(string $block, array $data): string
116
    {
117
        try {
118
            return $this->template->renderBlock($block, $data);
0 ignored issues
show
Bug introduced by
The method renderBlock cannot be called on $this->template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
        } catch (\Throwable $e) {
120
            throw new RepositoryUnavailableException($e->getMessage());
121
        }
122
    }
123
124
    /**
125
     * Repository initialize
126
     *
127
     * @param TemplateInterface $template
128
     * @param array $arguments
129
     *
130
     * @throws RepositoryUnavailableException
131
     */
132
    public function connect(TemplateInterface $template, array $arguments = [])
133
    {
134
        if (false === $template instanceof TwigTemplateAwareInterface) {
135
            throw new RepositoryUnavailableException('Template should implement TwigTemplateAwareInterface to work with TwigFileRepository.');
136
        }
137
138
        try {
139
            if (file_exists($template->getTwigTemplate())) {
140
                $directory = dirname($template->getTwigTemplate());
141
142
                $this->twig->setLoader(new \Twig_Loader_Chain([
143
                    $this->twig->getLoader(),
144
                    new \Twig_Loader_Filesystem(basename($directory), dirname($directory)),
145
                ]));
146
147
                $templateName = basename($template->getTwigTemplate());
148
            } else {
149
                $templateName = $template->getTwigTemplate();
150
            }
151
152
            $this->twig->setCache(false);
153
154
            $this->template = $this->twig->load($templateName);
155
        } catch (\Throwable $e) {
156
            throw new RepositoryUnavailableException($e->getMessage());
157
        }
158
    }
159
}
160