]> git.mxchange.org Git - core.git/blob - framework/main/classes/mailer/debug/class_DebugMailer.php
Continued:
[core.git] / framework / main / classes / mailer / debug / class_DebugMailer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Mailer\Debug;
4
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;
14
15 // Import SPL stuff
16 use \UnexpectedValueException;
17
18 /**
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.
21  *
22  * @author              Roland Haeder <webmaster@shipsimu.org>
23  * @version             0.0.0
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
27  *
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.
32  *
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.
37  *
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/>.
40  */
41 class DebugMailer extends BaseMailer implements DeliverableMail {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         private function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of this mailer class
54          *
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
59          */
60         public static final function createDebugMailer (CompileableTemplate $templateInstance, string $templateName) {
61                 // Get a new instance
62                 $mailerInstance = new DebugMailer();
63
64                 // Set template instance
65                 $mailerInstance->setTemplateInstance($templateInstance);
66
67                 // Set template name
68                 $mailerInstance->setTemplateName($templateName);
69
70                 // Return the instance
71                 return $mailerInstance;
72         }
73
74         /**
75          * Deliver email to the recipient(s)
76          *
77          * @return      void
78          * @throws      UnexpectedValueException        If the recipient instance does not implement ManageableMember
79          */
80         public function deliverEmail () {
81                 // Get template instance
82                 $templateInstance = $this->getTemplateInstance();
83
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)));
92                                 }
93
94                                 // User class found, so entry is valid, first load the template
95                                 $this->loadTemplate($templateName);
96
97                                 // Set subject line
98                                 $templateInstance->assignVariable('subject', $this->getSubjectLine());
99
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);
104                                 }
105
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])) {
110                                                 // Throw exception
111                                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
112                                         }
113
114                                         // Get the field from the value instance
115                                         $fieldValue = $recipientList['values'][$variable]->getField($variable);
116
117                                         // Set it in template engine
118                                         $templateInstance->assignVariable($variable, $fieldValue);
119                                 }
120
121                                 // Render the content
122                                 $templateInstance->renderXmlContent();
123
124                                 // Get responce instance
125                                 $responseInstance = FrameworkBootstrap::getResponseInstance();
126
127                                 // Transfer the data to the response
128                                 $templateInstance->transferToResponse($responseInstance);
129                         }
130                 }
131         }
132
133         /**
134          * Send notification to the admin
135          *
136          * @return      void
137          * @todo        0% done
138          */
139         public function sendAdminNotification () {
140                 // Unfinished work
141         }
142
143         /**
144          * Invokes the mail delivery process which will prepare the output of the message in a code template
145          *
146          * @return      void
147          */
148         public function invokeMailDelivery () {
149                 // Get template instance
150                 $templateInstance = $this->getTemplateInstance();
151
152                 // Get the compiled message and set it as new template variable
153                 $message = $templateInstance->getCompiledData();
154                 $templateInstance->assignVariable('message', $message);
155
156                 // Load the code template
157                 $templateInstance->loadCodeTemplate('mail_debug');
158
159                 // Compile the template
160                 $templateInstance->compileTemplate();
161
162                 // Assign this template with variable
163                 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
164
165                 // Load header template
166                 $templateInstance->loadCodeTemplate('header');
167
168                 // Compile and assign it with a variable
169                 $templateInstance->compileTemplate();
170                 $templateInstance->assignTemplateWithVariable('header', 'header');
171
172                 // Load footer template
173                 $templateInstance->loadCodeTemplate('footer');
174
175                 // Compile and assign it with a variable
176                 $templateInstance->compileTemplate();
177                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
178
179                 // Get master template name
180                 $masterTemplateName = ApplicationHelper::getSelfInstance()->buildMasterTemplateName();
181
182                 // Load the master template
183                 $templateInstance->loadCodeTemplate($masterTemplateName);
184
185                 // Then compile it again
186                 $templateInstance->compileVariables();
187         }
188
189 }