Completed
Push — master ( 673fc6...a88edb )
by Oleg
03:40
created

EmailDriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A sendMessage() 0 11 1
1
<?php /** MicroEmailDriver */
2
3
namespace Micro\Logger\Drivers;
4
5
use Micro\Mail\Message;
6
use Micro\Mail\Transport\ITransport;
7
use Micro\Mail\Transport\TransportInjector;
8
9
/**
10
 * EmailDriver logger class file.
11
 *
12
 * Sender email for logger
13
 *
14
 * @author Oleg Lunegov <[email protected]>
15
 * @link https://github.com/linpax/microphp-framework
16
 * @copyright Copyright (c) 2013 Oleg Lunegov
17
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
18
 * @package Micro
19
 * @subpackage Logger\Driver
20
 * @version 1.0
21
 * @since 1.0
22
 */
23
class EmailDriver extends LoggerDriver
24
{
25
    /** @var string $from email for sender attribute */
26
    private $from;
27
    /** @var string $type message attribute */
28
    private $type = 'text/plain';
29
    /** @var string $to message recipient */
30
    private $to;
31
    /** @var string $subject message theme */
32
    private $subject;
33
34
35
    /**
36
     * Constructor is a initialize logger
37
     *
38
     * @access public
39
     *
40
     * @param array $params configuration params
41
     *
42
     * @throws \Micro\Base\Exception
43
     * @result void
44
     */
45
    public function __construct(array $params = [])
46
    {
47
        parent::__construct($params);
48
49
        $this->from = !empty($params['from']) ? $params['from'] : getenv('SERVER_ADMIN');
50
        $this->to = !empty($params['to']) ? $params['to'] : $this->from;
51
        $this->subject = $params['subject'] ?: getenv('SERVER_NAME').' log message';
52
    }
53
54
    /**
55
     * Send message in log
56
     *
57
     * @access public
58
     *
59
     * @param integer $level level number
60
     * @param string $message message to write
61
     *
62
     * @result void
63
     * @throws \Micro\Base\Exception
64
     */
65
    public function sendMessage($level, $message)
66
    {
67
        $mail = new Message($this->from);
68
69
        $mail->setTo($this->to);
70
        $mail->setText(ucfirst($level).': '.$message, $this->type);
71
72
        /** @var ITransport $transport */
73
        $transport = (new TransportInjector)->build();
74
        $transport->send($mail);
75
    }
76
}
77