Passed
Push — master ( 46ed75...ca2387 )
by Zaahid
03:33
created

AddressEmailConsumerService::processParts()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 3
nop 1
crap 5
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\Consumer;
9
10
use ZBateson\MailMimeParser\Header\Part\CommentPart;
11
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
12
use ZBateson\MailMimeParser\Header\Part\LiteralPart;
13
14
/**
15
 * Parses the Address portion of an email address header, for an address part
16
 * that contains both a name and an email address, e.g. "name" <[email protected]>.
17
 *
18
 * The address portion found within the '<' and '>' chars may contain comments
19
 * and quoted portions.
20
 *
21
 * @author Zaahid Bateson
22
 */
23
class AddressEmailConsumerService extends AbstractConsumerService
24
{
25 4
    public function __construct(
26
        HeaderPartFactory $partFactory,
27
        CommentConsumerService $commentConsumerService,
28
        QuotedStringConsumerService $quotedStringConsumerService
29
    ) {
30 4
        parent::__construct(
31 4
            $partFactory,
32 4
            [ $commentConsumerService, $quotedStringConsumerService ]
33 4
        );
34
    }
35
36
    /**
37
     * Overridden to return patterns matching the beginning/end part of an
38
     * address in a name/address part ("<" and ">" chars).
39
     *
40
     * @return string[] the patterns
41
     */
42 5
    public function getTokenSeparators() : array
43
    {
44 5
        return ['<', '>'];
45
    }
46
47
    /**
48
     * Returns true for the '>' char.
49
     */
50 98
    protected function isEndToken(string $token) : bool
51
    {
52 98
        return ($token === '>');
53
    }
54
55
    /**
56
     * Returns true for the '<' char.
57
     */
58 95
    protected function isStartToken(string $token) : bool
59
    {
60 95
        return ($token === '<');
61
    }
62
63
    /**
64
     * Returns a single {@see ZBateson\MailMimeParser\Header\Part\AddressPart}
65
     * with its 'email' portion set, so an {@see AddressConsumerService} can
66
     * identify it and create an
67
     * {@see ZBateson\MailMimeParser\Header\Part\AddressPart} AddressPart with
68
     * both a name and email set.
69
     *
70
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
71
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]|array
72
     */
73 98
    protected function processParts(array $parts) : array
74
    {
75 98
        $strEmail = '';
76 98
        foreach ($parts as $p) {
77 98
            $val = $p->getValue();
78 98
            if ((($p instanceof LiteralPart) && !($p instanceof CommentPart)) && $val !== '') {
79 1
                $val = '"' . \preg_replace('/(["\\\])/', '\\\$1', $val) . '"';
80
            } else {
81 98
                $val = \preg_replace('/\s+/', '', $val);
82
            }
83 98
            $strEmail .= $val;
84
        }
85 98
        return [$this->partFactory->newAddressPart('', $strEmail)];
86
    }
87
}
88