LoggerAppenderMailEvent.php
Current file: /home/ihabunek/apache/log4php/src/main/php/appenders/LoggerAppenderMailEvent.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
37.50% 3 / 8 CRAP
65.85% 27 / 41
LoggerAppenderMailEvent
0.00% 0 / 1
37.50% 3 / 8 26.19
65.85% 27 / 41
 activateOptions()
0.00% 0 / 1 4.77
63.64% 7 / 11
 setFrom($from)
100.00% 1 / 1 1
100.00% 2 / 2
 setPort($port)
0.00% 0 / 1 2
0.00% 0 / 2
 setSmtpHost($smtpHost)
0.00% 0 / 1 2
0.00% 0 / 2
 setSubject($subject)
0.00% 0 / 1 2
0.00% 0 / 2
 setTo($to)
100.00% 1 / 1 1
100.00% 2 / 2
 setDry($dry)
100.00% 1 / 1 1
100.00% 2 / 2
 append(LoggerLoggingEvent $event)
0.00% 0 / 1 6.40
77.78% 14 / 18


       1                 : <?php                                                                                                                                                            
       2                 : /**                                                                                                                                                              
       3                 :  * Licensed to the Apache Software Foundation (ASF) under one or more                                                                                            
       4                 :  * contributor license agreements. See the NOTICE file distributed with                                                                                          
       5                 :  * this work for additional information regarding copyright ownership.                                                                                           
       6                 :  * The ASF licenses this file to You under the Apache License, Version 2.0                                                                                       
       7                 :  * (the "License"); you may not use this file except in compliance with                                                                                          
       8                 :  * the License. You may obtain a copy of the License at                                                                                                          
       9                 :  *                                                                                                                                                               
      10                 :  *       http://www.apache.org/licenses/LICENSE-2.0                                                                                                              
      11                 :  *                                                                                                                                                               
      12                 :  * Unless required by applicable law or agreed to in writing, software                                                                                           
      13                 :  * distributed under the License is distributed on an "AS IS" BASIS,                                                                                             
      14                 :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                                                                      
      15                 :  * See the License for the specific language governing permissions and                                                                                           
      16                 :  * limitations under the License.                                                                                                                                
      17                 :  *                                                                                                                                                               
      18                 :  * @package log4php                                                                                                                                              
      19                 :  */                                                                                                                                                              
      20                 :                                                                                                                                                                  
      21                 : /**                                                                                                                                                              
      22                 :  * Log every events as a separate email.                                                                                                                         
      23                 :  *                                                                                                                                                               
      24                 :  * Configurable parameters for this appender are:                                                                                                                
      25                 :  *                                                                                                                                                               
      26                 :  * - layout             - Sets the layout class for this appender (required)                                                                                     
      27                 :  * - to                 - Sets the recipient of the mail (required)                                                                                              
      28                 :  * - from               - Sets the sender of the mail (optional)                                                                                                 
      29                 :  * - subject            - Sets the subject of the mail (optional)                                                                                                
      30                 :  * - smtpHost           - Sets the mail server (optional, default is ini_get('SMTP'))                                                                            
      31                 :  * - port               - Sets the port of the mail server (optional, default is 25)                                                                             
      32                 :  *                                                                                                                                                               
      33                 :  * An example:                                                                                                                                                   
      34                 :  *                                                                                                                                                               
      35                 :  * {@example ../../examples/php/appender_mailevent.php 19}                                                                                                       
      36                 :  *                                                                                                                                                               
      37                 :  * {@example ../../examples/resources/appender_mailevent.properties 18}                                                                                          
      38                 :  *                                                                                                                                                               
      39                 :  *                                                                                                                                                               
      40                 :  * The above will output something like:                                                                                                                         
      41                 :  * <pre>                                                                                                                                                         
      42                 :  *      Date: Tue,  8 Sep 2009 21:51:04 +0200 (CEST)                                                                                                             
      43                 :  *      From: someone@example.com                                                                                                                                
      44                 :  *      To: root@localhost                                                                                                                                       
      45                 :  *      Subject: Log4php test                                                                                                                                    
      46                 :  *                                                                                                                                                               
      47                 :  *      Tue Sep  8 21:51:04 2009,120 [5485] FATAL root - Some critical message!                                                                                  
      48                 :  * </pre>                                                                                                                                                        
      49                 :  *                                                                                                                                                               
      50                 :  * @version $Revision: 1237433 $                                                                                                                                 
      51                 :  * @package log4php                                                                                                                                              
      52                 :  * @subpackage appenders                                                                                                                                         
      53                 :  */                                                                                                                                                              
      54                 : class LoggerAppenderMailEvent extends LoggerAppender {                                                                                                           
      55                 :                                                                                                                                                                  
      56                 :     /**  'from' field (defaults to 'sendmail_from' from php.ini on win32).                                                                                       
      57                 :      * @var string                                                                                                                                               
      58                 :      */                                                                                                                                                          
      59                 :     protected $from;                                                                                                                                             
      60                 :                                                                                                                                                                  
      61                 :     /** Mailserver port (win32 only).                                                                                                                            
      62                 :      * @var integer                                                                                                                                              
      63                 :      */                                                                                                                                                          
      64                 :     protected $port = 25;                                                                                                                                        
      65                 :                                                                                                                                                                  
      66                 :     /** Mailserver hostname (win32 only).                                                                                                                        
      67                 :      * @var string                                                                                                                                               
      68                 :      */                                                                                                                                                          
      69                 :     protected $smtpHost = null;                                                                                                                                  
      70                 :                                                                                                                                                                  
      71                 :     /**                                                                                                                                                          
      72                 :      * @var string 'subject' field                                                                                                                               
      73                 :      */                                                                                                                                                          
      74                 :     protected $subject = '';                                                                                                                                     
      75                 :                                                                                                                                                                  
      76                 :     /**                                                                                                                                                          
      77                 :      * @var string 'to' field                                                                                                                                    
      78                 :      */                                                                                                                                                          
      79                 :     protected $to = null;                                                                                                                                        
      80                 :                                                                                                                                                                  
      81                 :     /** @var indiciates if this appender should run in dry mode */                                                                                               
      82                 :     protected $dry = false;                                                                                                                                      
      83                 :                                                                                                                                                                  
      84                 :     public function activateOptions() {                                                                                                                          
      85               3 :         if (empty($this->to)) {                                                                                                                                  
      86               1 :             $this->warn("Required parameter 'to' not set. Closing appender.");                                                                                   
      87               0 :             $this->close = true;                                                                                                                                 
      88               0 :             return;                                                                                                                                              
      89                 :         }                                                                                                                                                        
      90                 :                                                                                                                                                                  
      91               2 :         $sendmail_from = ini_get('sendmail_from');                                                                                                               
      92               2 :         if (empty($this->from) and empty($sendmail_from)) {                                                                                                      
      93               1 :             $this->warn("Required parameter 'from' not set. Closing appender.");                                                                                 
      94               0 :             $this->close = true;                                                                                                                                 
      95               0 :             return;                                                                                                                                              
      96                 :         }                                                                                                                                                        
      97                 :                                                                                                                                                                  
      98               1 :         $this->closed = false;                                                                                                                                   
      99               1 :     }                                                                                                                                                            
     100                 :                                                                                                                                                                  
     101                 :     public function setFrom($from) {                                                                                                                             
     102               2 :         $this->setString('from', $from);                                                                                                                         
     103               2 :     }                                                                                                                                                            
     104                 :                                                                                                                                                                  
     105                 :     public function setPort($port) {                                                                                                                             
     106               0 :         $this->setPositiveInteger('port', $port);                                                                                                                
     107               0 :     }                                                                                                                                                            
     108                 :                                                                                                                                                                  
     109                 :     public function setSmtpHost($smtpHost) {                                                                                                                     
     110               0 :         $this->setString('smtpHost', $smtpHost);                                                                                                                 
     111               0 :     }                                                                                                                                                            
     112                 :                                                                                                                                                                  
     113                 :     public function setSubject($subject) {                                                                                                                       
     114               0 :         $this->setString('subject',  $subject);                                                                                                                  
     115               0 :     }                                                                                                                                                            
     116                 :                                                                                                                                                                  
     117                 :     public function setTo($to) {                                                                                                                                 
     118               2 :         $this->setString('to',  $to);                                                                                                                            
     119               2 :     }                                                                                                                                                            
     120                 :                                                                                                                                                                  
     121                 :     public function setDry($dry) {                                                                                                                               
     122               1 :         $this->setBoolean('dry', $dry);                                                                                                                          
     123               1 :     }                                                                                                                                                            
     124                 :                                                                                                                                                                  
     125                 :     public function append(LoggerLoggingEvent $event) {                                                                                                          
     126               1 :         $smtpHost = $this->smtpHost;                                                                                                                             
     127               1 :         $prevSmtpHost = ini_get('SMTP');                                                                                                                         
     128               1 :         if(!empty($smtpHost)) {                                                                                                                                  
     129               0 :             ini_set('SMTP', $smtpHost);                                                                                                                          
     130               0 :         }                                                                                                                                                        
     131                 :                                                                                                                                                                  
     132               1 :         $smtpPort = $this->port;                                                                                                                                 
     133               1 :         $prevSmtpPort= ini_get('smtp_port');                                                                                                                     
     134               1 :         if($smtpPort > 0 and $smtpPort < 65535) {                                                                                                                
     135               1 :             ini_set('smtp_port', $smtpPort);                                                                                                                     
     136               1 :         }                                                                                                                                                        
     137                 :                                                                                                                                                                  
     138                 :         // On unix only sendmail_path, which is PHP_INI_SYSTEM i.e. not changeable here, is used.                                                                
     139                 :                                                                                                                                                                  
     140               1 :         $addHeader = empty($this->from) ? '' : "From: {$this->from}\r\n";                                                                                        
     141                 :                                                                                                                                                                  
     142               1 :         if(!$this->dry) {                                                                                                                                        
     143               0 :             $result = mail($this->to, $this->subject, $this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event), $addHeader);
     144               0 :         } else {                                                                                                                                                 
     145               1 :             echo "DRY MODE OF MAIL APP.: Send mail to: ".$this->to." with additional headers '".trim($addHeader)."' and content: ".$this->layout->format($event);
     146                 :         }                                                                                                                                                        
     147                 :                                                                                                                                                                  
     148               1 :         ini_set('SMTP', $prevSmtpHost);                                                                                                                          
     149               1 :         ini_set('smtp_port', $prevSmtpPort);                                                                                                                     
     150               1 :     }                                                                                                                                                            
     151                 : }                                                                                                                                                                

Generated by PHP_CodeCoverage 1.1.1 using PHP 5.3.3-7+squeeze3 and PHPUnit 3.6.3 at Sat Feb 18 22:32:39 GMT 2012.