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\Mailer\BaseMailer;
9 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
10 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
15 * A mailer class for debugging purposes only. This class will print the
16 * prepared mail out and will not send it to the recipient.
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class DebugMailer extends BaseMailer implements DeliverableMail {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this mailer class
51 * @param $templateInstance A template instance
52 * @param $applicationInstance An application helper class
53 * @param $templateName Name of email template to set
54 * @return $mailerInstance An instance of this mailer class
56 public static final function createDebugMailer (CompileableTemplate $templateInstance, $templateName) {
58 $mailerInstance = new DebugMailer();
60 // Set template instance
61 $mailerInstance->setTemplateInstance($templateInstance);
64 $mailerInstance->setTemplateName($templateName);
66 // Return the instance
67 return $mailerInstance;
71 * Deliver email to the recipient(s)
74 * @throws InvalidInterfaceException If the recipient instance does not implement ManageableMember
76 public function deliverEmail () {
77 // Get template instance
78 $templateInstance = $this->getTemplateInstance();
80 // "Deliver" all emails
81 foreach ($this->getRecipientList() as $templateName => $recipientList) {
82 // Walk through all recipients and "sent", or better print, it out
83 foreach ($recipientList['recipients'] as $recipientInstance) {
84 // The recipient should be a user instance, right?
85 if (!$recipientInstance instanceof ManageableMember) {
86 // Invalid entry found!
87 throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
90 // User class found, so entry is valid, first load the template
91 $this->loadTemplate($templateName);
94 $templateInstance->assignVariable('subject', $this->getSubjectLine());
96 // Walk through all variables, first config to assign them
97 foreach ($recipientList['config_vars'] as $variable => $dummy) {
98 // Load the config value and set it
99 $templateInstance->assignConfigVariable($variable);
102 // Now do the same with the values but ask the "value instance" instead!
103 foreach ($recipientList['value_vars'] as $variable => $dummy) {
104 // Is the value instance there?
105 if (!isset($recipientList['values'][$variable])) {
107 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
110 // Get the field from the value instance
111 $fieldValue = $recipientList['values'][$variable]->getField($variable);
113 // Set it in template engine
114 $templateInstance->assignVariable($variable, $fieldValue);
117 // Render the content
118 $templateInstance->renderXmlContent();
120 // Get responce instance
121 $responseInstance = FrameworkBootstrap::getResponseInstance();
123 // Transfer the data to the response
124 $templateInstance->transferToResponse($responseInstance);
130 * Send notification to the admin
135 public function sendAdminNotification () {
140 * Invokes the mail delivery process which will prepare the output of the message in a code template
144 public function invokeMailDelivery () {
145 // Get template instance
146 $templateInstance = $this->getTemplateInstance();
148 // Get the compiled message and set it as new template variable
149 $message = $templateInstance->getCompiledData();
150 $templateInstance->assignVariable('message', $message);
152 // Load the code template
153 $templateInstance->loadCodeTemplate('mail_debug');
155 // Compile the template
156 $templateInstance->compileTemplate();
158 // Assign this template with variable
159 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
161 // Load header template
162 $templateInstance->loadCodeTemplate('header');
164 // Compile and assign it with a variable
165 $templateInstance->compileTemplate();
166 $templateInstance->assignTemplateWithVariable('header', 'header');
168 // Load footer template
169 $templateInstance->loadCodeTemplate('footer');
171 // Compile and assign it with a variable
172 $templateInstance->compileTemplate();
173 $templateInstance->assignTemplateWithVariable('footer', 'footer');
175 // Get master template name
176 $masterTemplateName = GenericRegistry::getRegistry()->getInstance('application')->buildMasterTemplateName();
178 // Load the master template
179 $templateInstance->loadCodeTemplate($masterTemplateName);
181 // Then compile it again
182 $templateInstance->compileVariables();