Test Failed
Push — master ( e258e4...a626ba )
by Zaahid
15:25
created

ReceivedDomainPart::__construct()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 8.4444
ccs 4
cts 4
cp 1
cc 8
nc 36
nop 4
crap 8
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
use Psr\Log\LoggerInterface;
11
use ZBateson\MbWrapper\MbWrapper;
12
13
/**
14
 * Holds extra information about a parsed Received header part, for FROM and BY
15
 * parts, namely: ehlo name, hostname, and address.
16
 *
17
 * The parsed parts would be mapped as follows:
18
 *
19
 * FROM ehlo name (hostname [address]), for example: FROM computer (domain.com
20
 * [1.2.3.4]) would contain "computer" for getEhloName(), domain.com for
21
 * getHostname and 1.2.3.4 for getAddress().
22
 *
23
 * This doesn't change if the ehlo name is an address, it is still returned in
24
 * getEhloName(), and not in getAddress().  Additionally square brackets are not
25
 * stripped from getEhloName() if its an address.  For example: "FROM [1.2.3.4]"
26
 * would return "[1.2.3.4]" in a call to getEhloName().
27
 *
28
 * For further information on how the header's parsed, check the documentation
29
 * for {@see \ZBateson\MailMimeParser\Header\Consumer\Received\DomainConsumer}.
30
 *
31
 * @author Zaahid Bateson
32
 */
33
class ReceivedDomainPart extends ReceivedPart
34
{
35
    /**
36
     * @var string The name used to identify the server in the EHLO line.
37
     */
38
    protected ?string $ehloName = null;
39
40
    /**
41
     * @var string The hostname.
42
     */
43
    protected ?string $hostname = null;
44
45
    /**
46
     * @var string The address.
47
     */
48
    protected ?string $address = null;
49 1
50
    /**
51
     * @param HeaderPart[] $subParts
52
     */
53
    public function __construct(
54
        LoggerInterface $logger,
55
        MbWrapper $charsetConverter,
56
        string $name,
57 1
        array $children
58 1
    ) {
59 1
        parent::__construct($logger, $charsetConverter, $name, $children);
60 1
61
        $this->ehloName = ($this->value !== '') ? $this->value : null;
62
        $cps = $this->getComments();
63
        $commentPart = (!empty($cps)) ? $cps[0] : null;
64
65
        $pattern = '~^(\[(IPv[64])?(?P<addr1>[a-f\d\.\:]+)\])?\s*(helo=)?(?P<name>[a-z0-9\-]+[a-z0-9\-\.]+)?\s*(\[(IPv[64])?(?P<addr2>[a-f\d\.\:]+)\])?$~i';
66
        if ($commentPart !== null && \preg_match($pattern, $commentPart->getComment(), $matches)) {
67
            $this->value .= ' (' . $commentPart->getComment() . ')';
68
            $this->hostname = (!empty($matches['name'])) ? $matches['name'] : null;
69
            $this->address = (!empty($matches['addr1'])) ? $matches['addr1'] : ((!empty($matches['addr2'])) ? $matches['addr2'] : null);
70
        }
71 1
    }
72
73 1
    /**
74
     * Returns the name used to identify the server in the first part of the
75
     * extended-domain line.
76
     *
77
     * Note that this is not necessarily the name used in the EHLO line to an
78
     * SMTP server, since implementations differ so much, not much can be
79
     * guaranteed except the position it was parsed in.
80 1
     */
81
    public function getEhloName() : ?string
82 1
    {
83
        return $this->ehloName;
84
    }
85
86
    /**
87
     * Returns the hostname of the server, or whatever string in the hostname
88
     * position when parsing (but never an address).
89 1
     */
90
    public function getHostname() : ?string
91 1
    {
92
        return $this->hostname;
93
    }
94
95
    /**
96
     * Returns the address of the server, or whatever string that looks like an
97
     * address in the address position when parsing (but never a hostname).
98
     */
99
    public function getAddress() : ?string
100
    {
101
        return $this->address;
102
    }
103
}
104