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