MessageTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 90
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A withHeader() 0 4 1
A withAddedHeader() 0 7 1
A getHeaderLine() 0 3 1
A getHeader() 0 11 3
A getHeaders() 0 3 1
A hasHeader() 0 4 1
A withoutHeader() 0 4 1
A withRecipient() 0 4 1
A withRecipients() 0 6 1
A withAddedRecipient() 0 6 1
A withAddedRecipients() 0 6 1
1
<?php
2
namespace SamIT\React\Smtp;
3
4
trait MessageTrait
5
{
6
    use \GuzzleHttp\Psr7\MessageTrait;
7
8
    protected $recipients = [];
9
    public function withHeader($name, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
10
    {
11
        throw new \Exception('SMTP Message does not support replacing headers.');
12
    }
13
14
    public function withAddedHeader($name, $value)
15
    {
16
        $new = clone $this;
17
        $new->headers[] = [$name, $value];
18
        return $new;
19
20
    }
21
22
    public function getHeaderLine($name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
        throw new \Exception('SMTP Message does not support merging headers with the same name.');
24
    }
25
26
    public function getHeader($header)
27
    {
28
        $result = [];
29
        $header = strtolower($header);
30
        foreach($this->headers as list($name, $value)) {
31
            if ($header == strtolower($name)) {
32
                $result[] = [$name, $value];
33
            }
34
        }
35
        return $result;
36
    }
37
38
    public function getHeaders() {
39
        return $this->headers;
40
    }
41
42
    public function hasHeader($header)
43
    {
44
        return !empty($this->getHeader($header));
45
    }
46
47
    public function withoutHeader($header)
0 ignored issues
show
Unused Code introduced by
The parameter $header is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        throw new \Exception('SMTP Message does not support removing headers.');
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function withRecipient($email, $name = null)
56
    {
57
        return $this->withRecipients([$email => $name]);
58
    }
59
60
   public function withRecipients(array $recipients)
61
   {
62
       $new = clone $this;
63
       $new->recipients = $recipients;
64
       return $new;
65
   }
66
67
    public function withAddedRecipient($email, $name = null)
68
    {
69
        $new = clone $this;
70
        $new->recipients[$email] = $name;
71
        return $new;
72
    }
73
74
    /**
75
     * Return an instance with the provided recipients added to the original recipient(s).
76
     *
77
     * This method MUST be implemented in such a way as to retain the
78
     * immutability of the message, and MUST return an instance that has the
79
     * new and/or updated header and value.
80
     *
81
     * @return self
82
     * @param array $recipients Array of email => name pairs.
83
     * @throws \InvalidArgumentException for invalid email.
84
     */
85
    public function withAddedRecipients(array $recipients)
86
    {
87
        $new = clone $this;
88
        $new->recipients = array_merge($this->recipients, $recipients);
89
        return $new;
90
    }
91
92
93
}
94