Passed
Push — master ( 16bd50...cbcb00 )
by Zaahid
03:19
created

HeaderFactory::newInstanceOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
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;
9
10
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
11
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
12
13
/**
14
 * Constructs various IHeader types depending on the type of header passed.
15
 *
16
 * If the passed header resolves to a specific defined header type, it is parsed
17
 * as such.  Otherwise, a GenericHeader is instantiated and returned.  Headers
18
 * are mapped as follows:
19
 *
20
 *  - {@see AddressHeader}: From, To, Cc, Bcc, Sender, Reply-To, Resent-From,
21
 *    Resent-To, Resent-Cc, Resent-Bcc, Resent-Reply-To, Return-Path,
22
 *    Delivered-To
23
 *  - {@see DateHeader}: Date, Resent-Date, Delivery-Date, Expires, Expiry-Date,
24
 *    Reply-By
25
 *  - {@see ParameterHeader}: Content-Type, Content-Disposition, Received-SPF,
26
 *    Authentication-Results, DKIM-Signature, Autocrypt
27
 *  - {@see SubjectHeader}: Subject
28
 *  - {@see IdHeader}: Message-ID, Content-ID, In-Reply-To, References
29
 *  - {@see ReceivedHeader}: Received
30
 *
31
 * @author Zaahid Bateson
32
 */
33
class HeaderFactory
34
{
35
    /**
36
     * @var ConsumerService the passed ConsumerService providing
37
     *      AbstractConsumer singletons.
38
     */
39
    protected $consumerService;
40
41
    /**
42
     * @var MimeLiteralPartFactory for mime decoding.
43
     */
44
    protected $mimeLiteralPartFactory;
45
46
    /**
47
     * @var string[][] maps IHeader types to headers.
48
     */
49
    protected $types = [
50
        \ZBateson\MailMimeParser\Header\AddressHeader::class => [
51
            'from',
52
            'to',
53
            'cc',
54
            'bcc',
55
            'sender',
56
            'replyto',
57
            'resentfrom',
58
            'resentto',
59
            'resentcc',
60
            'resentbcc',
61
            'resentreplyto',
62
            'returnpath',
63
            'deliveredto',
64
        ],
65
        \ZBateson\MailMimeParser\Header\DateHeader::class => [
66
            'date',
67
            'resentdate',
68
            'deliverydate',
69
            'expires',
70
            'expirydate',
71
            'replyby',
72
        ],
73
        \ZBateson\MailMimeParser\Header\ParameterHeader::class => [
74
            'contenttype',
75
            'contentdisposition',
76
            'receivedspf',
77
            'authenticationresults',
78
            'dkimsignature',
79
            'autocrypt',
80
        ],
81
        \ZBateson\MailMimeParser\Header\SubjectHeader::class => [
82
            'subject',
83
        ],
84
        \ZBateson\MailMimeParser\Header\IdHeader::class => [
85
            'messageid',
86
            'contentid',
87
            'inreplyto',
88
            'references'
89
        ],
90
        \ZBateson\MailMimeParser\Header\ReceivedHeader::class => [
91
            'received'
92
        ]
93
    ];
94
95
    /**
96
     * @var string Defines the generic IHeader type to use for headers that
97
     *      aren't mapped in $types
98
     */
99
    protected $genericType = \ZBateson\MailMimeParser\Header\GenericHeader::class;
100
101
    /**
102
     * Instantiates member variables with the passed objects.
103
     *
104
     */
105 9
    public function __construct(ConsumerService $consumerService, MimeLiteralPartFactory $mimeLiteralPartFactory)
106
    {
107 9
        $this->consumerService = $consumerService;
108 9
        $this->mimeLiteralPartFactory = $mimeLiteralPartFactory;
109
    }
110
111
    /**
112
     * Returns the string in lower-case, and with non-alphanumeric characters
113
     * stripped out.
114
     *
115
     * @param string $header The header name
116
     * @return string The normalized header name
117
     */
118 112
    public function getNormalizedHeaderName(string $header) : string
119
    {
120 112
        return \preg_replace('/[^a-z0-9]/', '', \strtolower($header));
121
    }
122
123
    /**
124
     * Returns the name of an IHeader class for the passed header name.
125
     *
126
     * @param string $name The header name.
127
     * @return string The Fully Qualified class name.
128
     */
129 112
    private function getClassFor(string $name) : string
130
    {
131 112
        $test = $this->getNormalizedHeaderName($name);
132 112
        foreach ($this->types as $class => $matchers) {
133 112
            foreach ($matchers as $matcher) {
134 112
                if ($test === $matcher) {
135 111
                    return $class;
136
                }
137
            }
138
        }
139 99
        return $this->genericType;
140
    }
141
142
    /**
143
     * Creates an IHeader instance for the passed header name and value, and
144
     * returns it.
145
     *
146
     * @param string $name The header name.
147
     * @param string $value The header value.
148
     * @return IHeader The created header object.
149
     */
150 112
    public function newInstance(string $name, string $value)
151
    {
152 112
        $class = $this->getClassFor($name);
153 112
        return $this->newInstanceOf($name, $value, $class);
154
    }
155
156
    /**
157
     * Creates an IHeader instance for the passed header name and value, and
158
     * returns it.
159
     *
160
     * @param string $name The header name.
161
     * @param string $value The header value.
162
     * @return IHeader The created header object.
163
     */
164 113
    public function newInstanceOf(string $name, string $value, string $iHeaderClass) : IHeader
165
    {
166 113
        if (\is_a($iHeaderClass, 'ZBateson\MailMimeParser\Header\MimeEncodedHeader', true)) {
167 34
            return new $iHeaderClass(
168 34
                $this->mimeLiteralPartFactory,
169 34
                $this->consumerService,
170 34
                $name,
171 34
                $value
172 34
            );
173
        }
174 113
        return new $iHeaderClass($this->consumerService, $name, $value);
175
    }
176
}
177