Passed
Push — master ( ad3faa...5c4918 )
by Zaahid
03:33
created

HeaderFactory::newHeaderContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
 *
24
 * @author Zaahid Bateson
25
 */
26
class HeaderFactory
27
{
28
    /**
29
     * @var ConsumerService the passed ConsumerService providing
30
     * AbstractConsumer singletons.
31
     */
32
    protected $consumerService;
33
    
34
    /**
35
     * @var string[][] maps AbstractHeader types to headers. 
36
     */
37
    protected $types = [
38
        'ZBateson\MailMimeParser\Header\AddressHeader' => [
39
            'from',
40
            'to',
41
            'cc',
42
            'bcc',
43
            'sender',
44
            'reply-to',
45
            'resent-from',
46
            'resent-to',
47
            'resent-cc',
48
            'resent-bcc',
49
            'resent-reply-to',
50
        ],
51
        'ZBateson\MailMimeParser\Header\DateHeader' => [
52
            'date',
53
            'resent-date',
54
            'delivery-date',
55
            'expires',
56
            'expiry-date',
57
            'reply-by',
58
        ],
59
        'ZBateson\MailMimeParser\Header\ParameterHeader' => [
60
            'content-type',
61
            'content-disposition',
62
        ],
63
        'ZBateson\MailMimeParser\Header\SubjectHeader' => [
64
            'subject',
65
        ]
66
    ];
67
    
68
    /**
69
     * @var string Defines the generic AbstractHeader type to use for headers
70
     * that aren't mapped in $types
71
     */
72
    protected $genericType = 'ZBateson\MailMimeParser\Header\GenericHeader';
73
    
74
    /**
75
     * Instantiates member variables with the passed objects.
76
     * 
77
     * @param ConsumerService $consumerService
78
     */
79 6
    public function __construct(ConsumerService $consumerService)
80
    {
81 6
        $this->consumerService = $consumerService;
82 6
    }
83
    
84
    /**
85
     * Returns the name of an AbstractHeader class for the passed header name.
86
     * 
87
     * @param string $name
88
     * @return string
89
     */
90 5
    private function getClassFor($name)
91
    {
92 5
        $test = strtolower($name);
93 5
        foreach ($this->types as $class => $matchers) {
94 5
            foreach ($matchers as $matcher) {
95 5
                if ($test === $matcher) {
96 5
                    return $class;
97
                }
98
            }
99
        }
100 5
        return $this->genericType;
101
    }
102
    
103
    /**
104
     * Creates an AbstractHeader instance for the passed header name and value,
105
     * and returns it.
106
     * 
107
     * @param string $name
108
     * @param string $value
109
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
110
     */
111 5
    public function newInstance($name, $value)
112
    {
113 5
        $class = $this->getClassFor($name);
114 5
        return new $class($this->consumerService, $name, $value);
115
    }
116
117
    /**
118
     * Creates and returns a HeaderContainer.
119
     *
120
     * @return HeaderContainer;
121
     */
122 1
    public function newHeaderContainer()
123
    {
124 1
        return new HeaderContainer($this);
125
    }
126
}
127