Some updates:
[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\Registry;
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 <<<<<<< HEAD:framework/main/classes/mailer/debug/class_DebugMailer.php
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 =======
24  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
25 >>>>>>> Some updates::inc/main/classes/mailer/debug/class_DebugMailer.php
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class DebugMailer extends BaseMailer implements DeliverableMail {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51         }
52
53         /**
54          * Creates an instance of this mailer class
55          *
56          * @param       $templateInstance               A template instance
57          * @param       $applicationInstance    An application helper class
58          * @param       $templateName                   Name of email template to set
59          * @return      $mailerInstance                 An instance of this mailer class
60          */
61         public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
62                 // Get a new instance
63                 $mailerInstance = new DebugMailer();
64
65                 // Set template instance
66                 $mailerInstance->setTemplateInstance($templateInstance);
67
68                 // Set application instance
69                 $mailerInstance->setApplicationInstance($applicationInstance);
70
71                 // Set template name
72                 $mailerInstance->setTemplateName($templateName);
73
74                 // Return the instance
75                 return $mailerInstance;
76         }
77
78         /**
79          * Deliver email to the recipient(s)
80          *
81          * @return      void
82          * @throws      InvalidInterfaceException       If the recipient instance does not implement ManageableMember
83          */
84         public function deliverEmail () {
85                 // Get template instance
86                 $templateInstance = $this->getTemplateInstance();
87
88                 // "Deliver" all emails
89                 foreach ($this->getRecipientList() as $templateName => $recipientList) {
90                         // Walk through all recipients and "sent", or better print, it out
91                         foreach ($recipientList['recipients'] as $recipientInstance) {
92                                 // The recipient should be a user instance, right?
93                                 if (!$recipientInstance instanceof ManageableMember) {
94                                         // Invalid entry found!
95                                         throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
96                                 } // END - if
97
98                                 // User class found, so entry is valid, first load the template
99                                 $this->loadTemplate($templateName);
100
101                                 // Set subject line
102                                 $templateInstance->assignVariable('subject', $this->getSubjectLine());
103
104                                 // Walk through all variables, first config to assign them
105                                 foreach ($recipientList['config_vars'] as $variable => $dummy) {
106                                         // Load the config value and set it
107                                         $templateInstance->assignConfigVariable($variable);
108                                 } // END - if
109
110                                 // Now do the same with the values but ask the "value instance" instead!
111                                 foreach ($recipientList['value_vars'] as $variable => $dummy) {
112                                         // Is the value instance there?
113                                         if (!isset($recipientList['values'][$variable])) {
114                                                 // Throw exception
115                                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
116                                         } // END - if
117
118                                         // Get the field from the value instance
119                                         $fieldValue = $recipientList['values'][$variable]->getField($variable);
120
121                                         // Set it in template engine
122                                         $templateInstance->assignVariable($variable, $fieldValue);
123                                 }
124
125                                 // Render the content
126                                 $templateInstance->renderXmlContent();
127
128                                 // Get responce instance
129                                 $responseInstance = FrameworkBootstrap::getResponseInstance();
130
131                                 // Transfer the data to the response
132                                 $templateInstance->transferToResponse($responseInstance);
133                         } // END - foreach
134                 } // END - foreach
135         }
136
137         /**
138          * Send notification to the admin
139          *
140          * @return      void
141          * @todo        0% done
142          */
143         public function sendAdminNotification () {
144                 // Unfinished work
145         }
146
147         /**
148          * Invokes the mail delivery process which will prepare the output of the message in a code template
149          *
150          * @return      void
151          */
152         public function invokeMailDelivery () {
153                 // Get template instance
154                 $templateInstance = $this->getTemplateInstance();
155
156                 // Get the compiled message and set it as new template variable
157                 $message = $templateInstance->getCompiledData();
158                 $templateInstance->assignVariable('message', $message);
159
160                 // Load the code template
161                 $templateInstance->loadCodeTemplate('mail_debug');
162
163                 // Compile the template
164                 $templateInstance->compileTemplate();
165
166                 // Assign this template with variable
167                 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
168
169                 // Load header template
170                 $templateInstance->loadCodeTemplate('header');
171
172                 // Compile and assign it with a variable
173                 $templateInstance->compileTemplate();
174                 $templateInstance->assignTemplateWithVariable('header', 'header');
175
176                 // Load footer template
177                 $templateInstance->loadCodeTemplate('footer');
178
179                 // Compile and assign it with a variable
180                 $templateInstance->compileTemplate();
181                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
182
183                 // Load the master template
184                 $templateInstance->loadCodeTemplate(Registry::getRegistry()->getInstance('app')->buildMasterTemplateName());
185
186                 // Then compile it again
187                 $templateInstance->compileVariables();
188         }
189
190 }