40b67c6cbaccd05323daa19e8b25cb306494831a
[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 template name
65                 $mailerInstance->setTemplateName($templateName);
66
67                 // Return the instance
68                 return $mailerInstance;
69         }
70
71         /**
72          * Deliver email to the recipient(s)
73          *
74          * @return      void
75          * @throws      InvalidInterfaceException       If the recipient instance does not implement ManageableMember
76          */
77         public function deliverEmail () {
78                 // Get template instance
79                 $templateInstance = $this->getTemplateInstance();
80
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);
89                                 } // END - if
90
91                                 // User class found, so entry is valid, first load the template
92                                 $this->loadTemplate($templateName);
93
94                                 // Set subject line
95                                 $templateInstance->assignVariable('subject', $this->getSubjectLine());
96
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);
101                                 } // END - if
102
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])) {
107                                                 // Throw exception
108                                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
109                                         } // END - if
110
111                                         // Get the field from the value instance
112                                         $fieldValue = $recipientList['values'][$variable]->getField($variable);
113
114                                         // Set it in template engine
115                                         $templateInstance->assignVariable($variable, $fieldValue);
116                                 }
117
118                                 // Render the content
119                                 $templateInstance->renderXmlContent();
120
121                                 // Get responce instance
122                                 $responseInstance = FrameworkBootstrap::getResponseInstance();
123
124                                 // Transfer the data to the response
125                                 $templateInstance->transferToResponse($responseInstance);
126                         } // END - foreach
127                 } // END - foreach
128         }
129
130         /**
131          * Send notification to the admin
132          *
133          * @return      void
134          * @todo        0% done
135          */
136         public function sendAdminNotification () {
137                 // Unfinished work
138         }
139
140         /**
141          * Invokes the mail delivery process which will prepare the output of the message in a code template
142          *
143          * @return      void
144          */
145         public function invokeMailDelivery () {
146                 // Get template instance
147                 $templateInstance = $this->getTemplateInstance();
148
149                 // Get the compiled message and set it as new template variable
150                 $message = $templateInstance->getCompiledData();
151                 $templateInstance->assignVariable('message', $message);
152
153                 // Load the code template
154                 $templateInstance->loadCodeTemplate('mail_debug');
155
156                 // Compile the template
157                 $templateInstance->compileTemplate();
158
159                 // Assign this template with variable
160                 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
161
162                 // Load header template
163                 $templateInstance->loadCodeTemplate('header');
164
165                 // Compile and assign it with a variable
166                 $templateInstance->compileTemplate();
167                 $templateInstance->assignTemplateWithVariable('header', 'header');
168
169                 // Load footer template
170                 $templateInstance->loadCodeTemplate('footer');
171
172                 // Compile and assign it with a variable
173                 $templateInstance->compileTemplate();
174                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
175
176                 // Get master template name
177                 $masterTemplateName = GenericRegistry::getRegistry()->getInstance('application')->buildMasterTemplateName();
178
179                 // Load the master template
180                 $templateInstance->loadCodeTemplate($masterTemplateName);
181
182                 // Then compile it again
183                 $templateInstance->compileVariables();
184         }
185
186 }