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 * A mailer class for debugging purposes only. This class will print the
17 * prepared mail out and will not send it to the recipient.
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class DebugMailer extends BaseMailer implements DeliverableMail {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of this mailer class
52 * @param $templateInstance A template instance
53 * @param $applicationInstance An application helper class
54 * @param $templateName Name of email template to set
55 * @return $mailerInstance An instance of this mailer class
57 public static final function createDebugMailer (CompileableTemplate $templateInstance, string $templateName) {
59 $mailerInstance = new DebugMailer();
61 // Set template instance
62 $mailerInstance->setTemplateInstance($templateInstance);
65 $mailerInstance->setTemplateName($templateName);
67 // Return the instance
68 return $mailerInstance;
72 * Deliver email to the recipient(s)
75 * @throws InvalidInterfaceException If the recipient instance does not implement ManageableMember
77 public function deliverEmail () {
78 // Get template instance
79 $templateInstance = $this->getTemplateInstance();
81 // "Deliver" all emails
82 foreach ($this->getRecipientList() as $templateName => $recipientList) {
83 // Walk through all recipients and "sent", or better print, it out
84 foreach ($recipientList['recipients'] as $recipientInstance) {
85 // The recipient should be a user instance, right?
86 if (!$recipientInstance instanceof ManageableMember) {
87 // Invalid entry found!
88 throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
91 // User class found, so entry is valid, first load the template
92 $this->loadTemplate($templateName);
95 $templateInstance->assignVariable('subject', $this->getSubjectLine());
97 // Walk through all variables, first config to assign them
98 foreach ($recipientList['config_vars'] as $variable => $dummy) {
99 // Load the config value and set it
100 $templateInstance->assignConfigVariable($variable);
103 // Now do the same with the values but ask the "value instance" instead!
104 foreach ($recipientList['value_vars'] as $variable => $dummy) {
105 // Is the value instance there?
106 if (!isset($recipientList['values'][$variable])) {
108 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
111 // Get the field from the value instance
112 $fieldValue = $recipientList['values'][$variable]->getField($variable);
114 // Set it in template engine
115 $templateInstance->assignVariable($variable, $fieldValue);
118 // Render the content
119 $templateInstance->renderXmlContent();
121 // Get responce instance
122 $responseInstance = FrameworkBootstrap::getResponseInstance();
124 // Transfer the data to the response
125 $templateInstance->transferToResponse($responseInstance);
131 * Send notification to the admin
136 public function sendAdminNotification () {
141 * Invokes the mail delivery process which will prepare the output of the message in a code template
145 public function invokeMailDelivery () {
146 // Get template instance
147 $templateInstance = $this->getTemplateInstance();
149 // Get the compiled message and set it as new template variable
150 $message = $templateInstance->getCompiledData();
151 $templateInstance->assignVariable('message', $message);
153 // Load the code template
154 $templateInstance->loadCodeTemplate('mail_debug');
156 // Compile the template
157 $templateInstance->compileTemplate();
159 // Assign this template with variable
160 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
162 // Load header template
163 $templateInstance->loadCodeTemplate('header');
165 // Compile and assign it with a variable
166 $templateInstance->compileTemplate();
167 $templateInstance->assignTemplateWithVariable('header', 'header');
169 // Load footer template
170 $templateInstance->loadCodeTemplate('footer');
172 // Compile and assign it with a variable
173 $templateInstance->compileTemplate();
174 $templateInstance->assignTemplateWithVariable('footer', 'footer');
176 // Get master template name
177 $masterTemplateName = ApplicationHelper::getSelfInstance()->buildMasterTemplateName();
179 // Load the master template
180 $templateInstance->loadCodeTemplate($masterTemplateName);
182 // Then compile it again
183 $templateInstance->compileVariables();