Passed
Push — master ( d1a0d3...b63e04 )
by Zaahid
03:13
created

HeaderFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 110
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassFor() 0 11 4
A newHeaderContainer() 0 3 1
A newInstance() 0 4 1
A __construct() 0 3 1
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
namespace ZBateson\MailMimeParser\Header;
8
9
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
10
11
/**
12
 * Constructs various AbstractHeader types depending on the type of header
13
 * passed.
14
 * 
15
 * If the passed header resolves to a specific defined header type, it is parsed
16
 * as such.  Otherwise, a GenericHeader is instantiated and returned.  Headers
17
 * are mapped as follows:
18
 * 
19
 * AddressHeader: From, To, Cc, Bcc, Sender, Reply-To, Resent-From, Resent-To,
20
 * Resent-Cc, Resent-Bcc, Resent-Reply-To
21
 * DateHeader: Date, Resent-Date, Delivery-Date, Expires, Expiry-Date, Reply-By
22
 * ParameterHeader: Content-Type, Content-Disposition
23
 * IdHeader: Message-ID, Content-ID, In-Reply-To
24
 * ReferenceHeader: Reference
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class HeaderFactory
29
{
30
    /**
31
     * @var ConsumerService the passed ConsumerService providing
32
     * AbstractConsumer singletons.
33
     */
34
    protected $consumerService;
35
    
36
    /**
37
     * @var string[][] maps AbstractHeader types to headers. 
38
     */
39
    protected $types = [
40
        'ZBateson\MailMimeParser\Header\AddressHeader' => [
41
            'from',
42
            'to',
43
            'cc',
44
            'bcc',
45
            'sender',
46
            'reply-to',
47
            'resent-from',
48
            'resent-to',
49
            'resent-cc',
50
            'resent-bcc',
51
            'resent-reply-to',
52
        ],
53
        'ZBateson\MailMimeParser\Header\DateHeader' => [
54
            'date',
55
            'resent-date',
56
            'delivery-date',
57
            'expires',
58
            'expiry-date',
59
            'reply-by',
60
        ],
61
        'ZBateson\MailMimeParser\Header\ParameterHeader' => [
62
            'content-type',
63
            'content-disposition',
64
        ],
65
        'ZBateson\MailMimeParser\Header\SubjectHeader' => [
66
            'subject',
67
        ],
68
        'ZBateson\MailMimeParser\Header\IdHeader' => [
69
            'message-id',
70
            'content-id'
71
        ],
72
        'ZBateson\MailMimeParser\Header\MultipleIdHeader' => [
73
            'in-reply-to',
74
            'reference'
75
        ],
76
        'ZBateson\MailMimeParser\Header\ReceivedHeader' => [
77
            'received'
78
        ]
79
    ];
80
    
81
    /**
82
     * @var string Defines the generic AbstractHeader type to use for headers
83
     * that aren't mapped in $types
84
     */
85
    protected $genericType = 'ZBateson\MailMimeParser\Header\GenericHeader';
86
    
87
    /**
88
     * Instantiates member variables with the passed objects.
89
     * 
90
     * @param ConsumerService $consumerService
91
     */
92 9
    public function __construct(ConsumerService $consumerService)
93
    {
94 9
        $this->consumerService = $consumerService;
95 9
    }
96
    
97
    /**
98
     * Returns the name of an AbstractHeader class for the passed header name.
99
     * 
100
     * @param string $name
101
     * @return string
102
     */
103 8
    private function getClassFor($name)
104
    {
105 8
        $test = strtolower($name);
106 8
        foreach ($this->types as $class => $matchers) {
107 8
            foreach ($matchers as $matcher) {
108 8
                if ($test === $matcher) {
109 8
                    return $class;
110
                }
111
            }
112
        }
113 5
        return $this->genericType;
114
    }
115
    
116
    /**
117
     * Creates an AbstractHeader instance for the passed header name and value,
118
     * and returns it.
119
     * 
120
     * @param string $name
121
     * @param string $value
122
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
123
     */
124 8
    public function newInstance($name, $value)
125
    {
126 8
        $class = $this->getClassFor($name);
127 8
        return new $class($this->consumerService, $name, $value);
128
    }
129
130
    /**
131
     * Creates and returns a HeaderContainer.
132
     *
133
     * @return HeaderContainer;
134
     */
135 1
    public function newHeaderContainer()
136
    {
137 1
        return new HeaderContainer($this);
138
    }
139
}
140