3 namespace Org\Mxchange\CoreFramework\Mailer\Debug;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
9 use Org\Mxchange\CoreFramework\Mailer\BaseMailer;
10 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
11 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
16 use \UnexpectedValueException;
19 * A mailer class for debugging purposes only. This class will print the
20 * prepared mail out and will not send it to the recipient.
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class DebugMailer extends BaseMailer implements DeliverableMail {
43 * Protected constructor
47 private function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this mailer class
55 * @param $templateInstance A template instance
56 * @param $applicationInstance An application helper class
57 * @param $templateName Name of email template to set
58 * @return $mailerInstance An instance of this mailer class
60 public static final function createDebugMailer (CompileableTemplate $templateInstance, string $templateName) {
62 $mailerInstance = new DebugMailer();
64 // Set template instance
65 $mailerInstance->setTemplateInstance($templateInstance);
68 $mailerInstance->setTemplateName($templateName);
70 // Return the instance
71 return $mailerInstance;
75 * Deliver email to the recipient(s)
78 * @throws UnexpectedValueException If the recipient instance does not implement ManageableMember
80 public function deliverEmail () {
81 // Get template instance
82 $templateInstance = $this->getTemplateInstance();
84 // "Deliver" all emails
85 foreach ($this->getRecipientList() as $templateName => $recipientList) {
86 // Walk through all recipients and "sent", or better print, it out
87 foreach ($recipientList['recipients'] as $recipientInstance) {
88 // The recipient should be a user instance, right?
89 if (!$recipientInstance instanceof ManageableMember) {
90 // Invalid entry found!
91 throw new UnexpectedValueException(sprintf('recipientInstance[]=%s does not implement ManageableMember', gettype($recipientInstance)));
94 // User class found, so entry is valid, first load the template
95 $this->loadTemplate($templateName);
98 $templateInstance->assignVariable('subject', $this->getSubjectLine());
100 // Walk through all variables, first config to assign them
101 foreach ($recipientList['config_vars'] as $variable => $dummy) {
102 // Load the config value and set it
103 $templateInstance->assignConfigVariable($variable);
106 // Now do the same with the values but ask the "value instance" instead!
107 foreach ($recipientList['value_vars'] as $variable => $dummy) {
108 // Is the value instance there?
109 if (!isset($recipientList['values'][$variable])) {
111 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
114 // Get the field from the value instance
115 $fieldValue = $recipientList['values'][$variable]->getField($variable);
117 // Set it in template engine
118 $templateInstance->assignVariable($variable, $fieldValue);
121 // Render the content
122 $templateInstance->renderXmlContent();
124 // Get responce instance
125 $responseInstance = FrameworkBootstrap::getResponseInstance();
127 // Transfer the data to the response
128 $templateInstance->transferToResponse($responseInstance);
134 * Send notification to the admin
139 public function sendAdminNotification () {
144 * Invokes the mail delivery process which will prepare the output of the message in a code template
148 public function invokeMailDelivery () {
149 // Get template instance
150 $templateInstance = $this->getTemplateInstance();
152 // Get the compiled message and set it as new template variable
153 $message = $templateInstance->getCompiledData();
154 $templateInstance->assignVariable('message', $message);
156 // Load the code template
157 $templateInstance->loadCodeTemplate('mail_debug');
159 // Compile the template
160 $templateInstance->compileTemplate();
162 // Assign this template with variable
163 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
165 // Load header template
166 $templateInstance->loadCodeTemplate('header');
168 // Compile and assign it with a variable
169 $templateInstance->compileTemplate();
170 $templateInstance->assignTemplateWithVariable('header', 'header');
172 // Load footer template
173 $templateInstance->loadCodeTemplate('footer');
175 // Compile and assign it with a variable
176 $templateInstance->compileTemplate();
177 $templateInstance->assignTemplateWithVariable('footer', 'footer');
179 // Get master template name
180 $masterTemplateName = ApplicationHelper::getSelfInstance()->buildMasterTemplateName();
182 // Load the master template
183 $templateInstance->loadCodeTemplate($masterTemplateName);
185 // Then compile it again
186 $templateInstance->compileVariables();