Passed
Push — master ( 856966...11e739 )
by Oleg
03:58
created

EmailDriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A sendMessage() 0 9 1
1
<?php /** MicroEmailDriver */
2
3
namespace Micro\Logger\Driver;
4
5
use Micro\Base\IContainer;
6
use Micro\Mail\Message;
7
8
/**
9
 * EmailDriver logger class file.
10
 *
11
 * Sender email for logger
12
 *
13
 * @author Oleg Lunegov <[email protected]>
14
 * @link https://github.com/linpax/microphp-framework
15
 * @copyright Copyright &copy; 2013 Oleg Lunegov
16
 * @license /LICENSE
17
 * @package Micro
18
 * @subpackage Logger\Driver
19
 * @version 1.0
20
 * @since 1.0
21
 */
22
class EmailDriver extends LoggerDriver
23
{
24
    /** @var string $from email for sender attribute */
25
    private $from;
26
    /** @var string $type message attribute */
27
    private $type = 'text/plain';
28
    /** @var string $to message recipient */
29
    private $to;
30
    /** @var string $subject message theme */
31
    private $subject;
32
33
34
    /**
35
     * Constructor is a initialize logger
36
     *
37
     * @access public
38
     *
39
     * @param IContainer $container Container
40
     * @param array $params configuration params
41
     *
42
     * @throws \Micro\Base\Exception
43
     * @result void
44
     */
45
    public function __construct(IContainer $container, array $params = [])
46
    {
47
        parent::__construct($container, $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
        $this->container->mail->send($mail);
0 ignored issues
show
Bug introduced by
Accessing mail on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73
    }
74
}
75