Message   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 125
c 0
b 0
f 0
wmc 13
lcom 2
cbo 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getTo() 0 4 1
A setTo() 0 4 1
A getSubject() 0 4 1
A setSubject() 0 4 1
A getText() 0 4 1
A setText() 0 6 1
A getHeaders() 0 8 1
A setHeaders() 0 4 1
A getParams() 0 8 2
A setParams() 0 4 1
1
<?php /** MicroMailMessage */
2
3
namespace Micro\Mail;
4
5
/**
6
 * Message class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/linpax/microphp-framework
10
 * @copyright Copyright (c) 2013 Oleg Lunegov
11
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
12
 * @package Micro
13
 * @subpackage Mail
14
 * @version 1.0
15
 * @since 1.0
16
 * @final
17
 */
18
class Message implements IMessage
19
{
20
    /** @var string $to Recipient */
21
    private $to;
22
    /** @var string $type Doctype */
23
    private $type = 'text/html';
24
    /** @var string $encoding encoding */
25
    private $encoding = 'utf-8';
26
    /** @var string $subject Subject */
27
    private $subject;
28
    /** @var string $text Body */
29
    private $text;
30
    /** @var array $headers Headers */
31
    private $headers = [];
32
    /** @var array $params Parameters */
33
    private $params = [];
34
35
36
    /**
37
     * Message constructor
38
     *
39
     * @access public
40
     *
41
     * @param string $from
42
     * @param string $fromName
43
     *
44
     * @result void
45
     */
46
    public function __construct($from = '', $fromName = '')
47
    {
48
        if ($from) {
49
            $this->setHeaders('From', sprintf('=?utf-8?B?%s?= <%s>', base64_encode($fromName), $from));
50
        }
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getTo()
57
    {
58
        return $this->to;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function setTo($mail)
65
    {
66
        $this->to = $mail;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getSubject()
73
    {
74
        return $this->subject;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function setSubject($text)
81
    {
82
        $this->subject = '=?utf-8?B?'.base64_encode($text).'?=';
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function getText()
89
    {
90
        return $this->text;
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function setText($body, $type = 'text/plain', $encoding = 'utf-8')
97
    {
98
        $this->text = $body;
99
        $this->type = $type;
100
        $this->encoding = $encoding;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function getHeaders()
107
    {
108
        return sprintf("%s\r\nContent-type: %s; charset=%s\r\n",
109
            implode("\r\n", array_values($this->headers)),
110
            $this->type,
111
            $this->encoding
112
        );
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function setHeaders($name, $value)
119
    {
120
        $this->headers[$name] = $name.': '.$value;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function getParams()
127
    {
128
        if (!$this->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Micro\Mail\IMessage::getParams of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
130
        }
131
132
        return implode("\r\n", array_values($this->params))."\r\n";
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public function setParams($name, $value)
139
    {
140
        $this->params[$name] = $name.': '.$value;
141
    }
142
}
143