1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\EmailEngineBundle\Repository; |
4
|
|
|
|
5
|
|
|
use SfCod\EmailEngineBundle\Entity\EmailEntityInterface; |
6
|
|
|
use SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException; |
7
|
|
|
use SfCod\EmailEngineBundle\Template\TemplateInterface; |
8
|
|
|
use Twig_Environment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DbRepository |
12
|
|
|
* |
13
|
|
|
* @author Virchenko Maksim <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @package SfCod\EmailEngineBundle\Repository |
16
|
|
|
*/ |
17
|
|
|
class DbRepository implements RepositoryInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EmailEntityInterface |
21
|
|
|
*/ |
22
|
|
|
protected $email; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EntityManagerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $em; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Twig_Environment |
31
|
|
|
*/ |
32
|
|
|
protected $twig; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* DbRepository constructor. |
36
|
|
|
* |
37
|
|
|
* @param EntityManagerInterface $em |
38
|
|
|
*/ |
39
|
|
|
public function __construct(EntityManagerInterface $em, Twig_Environment $twig) |
40
|
|
|
{ |
41
|
|
|
$this->em = $em; |
42
|
|
|
$this->twig = $twig; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Repository initialize |
47
|
|
|
* |
48
|
|
|
* @param TemplateInterface $template |
49
|
|
|
* @param array $arguments |
50
|
|
|
* |
51
|
|
|
* @throws RepositoryUnavailableException |
52
|
|
|
*/ |
53
|
|
|
public function connect(TemplateInterface $template, array $arguments = []) |
54
|
|
|
{ |
55
|
|
|
if (false === isset($arguments['entity'], $arguments['attribute'])) { |
56
|
|
|
throw new RepositoryUnavailableException('DbRepository configuration incorrect, "entity" and "attribute" must be configured.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->email = $this->em |
60
|
|
|
->getRepository($arguments['entity']) |
61
|
|
|
->findOneBy([$arguments['attribute'] => get_class($template)::getSlug()]); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (is_null($this->email)) { |
64
|
|
|
throw new RepositoryUnavailableException(sprintf('Record with slug "%s" does not exists.', get_class($template)::getSlug())); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get subject template |
70
|
|
|
* |
71
|
|
|
* @param array $data |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
* |
75
|
|
|
* @throws \Throwable |
76
|
|
|
*/ |
77
|
|
|
public function getSubjectTemplate(array $data): string |
78
|
|
|
{ |
79
|
|
|
return $this->applyArguments($this->email->getTitle(), $data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get body template |
84
|
|
|
* |
85
|
|
|
* @param array $data |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* |
89
|
|
|
* @throws \Throwable |
90
|
|
|
*/ |
91
|
|
|
public function getBodyTemplate(array $data): string |
92
|
|
|
{ |
93
|
|
|
return $this->applyArguments($this->email->getBody(), $data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get sender name template |
98
|
|
|
* |
99
|
|
|
* @param array $data |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getSenderNameTemplate(array $data): string |
104
|
|
|
{ |
105
|
|
|
return getenv('SENDER_NAME'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get sender email template |
110
|
|
|
* |
111
|
|
|
* @param array $data |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getSenderEmailTemplate(array $data): string |
116
|
|
|
{ |
117
|
|
|
return getenv('SENDER_EMAIL'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Apply arguments to string |
122
|
|
|
* |
123
|
|
|
* @param string $str |
124
|
|
|
* @param array $args |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* |
128
|
|
|
* @throws \Throwable |
129
|
|
|
*/ |
130
|
|
|
protected function applyArguments(string $str, array $args): string |
131
|
|
|
{ |
132
|
|
|
try { |
133
|
|
|
return $this->twig |
134
|
|
|
->createTemplate('{% autoescape false %}' . $str . '{% endautoescape %}') |
135
|
|
|
->render($args); |
136
|
|
|
} catch (\Throwable $e) { |
137
|
|
|
throw new RepositoryUnavailableException($e->getMessage()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.