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\Mailer\BaseMailer;
9 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
10 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
11 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
14
15 /**
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.
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
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.
29  *
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.
34  *
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/>.
37  */
38 class DebugMailer extends BaseMailer implements DeliverableMail {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of this mailer class
51          *
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
56          */
57         public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
58                 // Get a new instance
59                 $mailerInstance = new DebugMailer();
60
61                 // Set template instance
62                 $mailerInstance->setTemplateInstance($templateInstance);
63
64                 // Set application instance
65                 $mailerInstance->setApplicationInstance($applicationInstance);
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      InvalidInterfaceException       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 InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
92                                 } // END - if
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                                 } // END - if
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                                         } // END - if
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                         } // END - foreach
130                 } // END - foreach
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                 // Load the master template
180                 $templateInstance->loadCodeTemplate(GenericRegistry::getRegistry()->getInstance('application')->buildMasterTemplateName());
181
182                 // Then compile it again
183                 $templateInstance->compileVariables();
184         }
185
186 }