Passed
Push — master ( 1f2094...564202 )
by Zaahid
03:47
created

ConsumerService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 36
c 1
b 0
f 0
dl 0
loc 185
ccs 28
cts 40
cp 0.7
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAddressGroupConsumer() 0 3 1
A getAddressBaseConsumer() 0 3 1
A getAddressConsumer() 0 3 1
A getIdConsumer() 0 3 1
A getParameterConsumer() 0 3 1
A getQuotedStringConsumer() 0 3 1
A getAddressEmailConsumer() 0 3 1
A getReceivedConsumer() 0 3 1
A getDateConsumer() 0 3 1
A getGenericConsumer() 0 3 1
A getCommentConsumer() 0 3 1
A getSubReceivedConsumer() 0 14 5
A getSubjectConsumer() 0 3 1
A getIdBaseConsumer() 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\Consumer;
8
9
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
10
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
11
use ZBateson\MailMimeParser\Header\Consumer\Received\DomainConsumer;
12
use ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer;
13
use ZBateson\MailMimeParser\Header\Consumer\Received\ReceivedDateConsumer;
14
15
/**
16
 * Simple service provider for consumer singletons.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class ConsumerService
21
{
22
    /**
23
     * @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory the
24
     * HeaderPartFactory instance used to create HeaderParts.
25
     */
26
    protected $partFactory;
27
    
28
    /**
29
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory used for
30
     *      GenericConsumer instances.
31
     */
32
    protected $mimeLiteralPartFactory;
33
34
    /**
35
     * @var Received\DomainConsumer[]|Received\GenericReceivedConsumer[]|Received\ReceivedDateConsumer[]
36
     *      an array of sub-received header consumer instances.
37
     */
38
    protected $receivedConsumers = [
39
        'from' => null,
40
        'by' => null,
41
        'via' => null,
42
        'with' => null,
43
        'id' => null,
44
        'for' => null,
45
        'date' => null
46
    ];
47
    
48 10
    public function __construct(HeaderPartFactory $partFactory, MimeLiteralPartFactory $mimeLiteralPartFactory)
49
    {
50 10
        $this->partFactory = $partFactory;
51 10
        $this->mimeLiteralPartFactory = $mimeLiteralPartFactory;
52 10
    }
53
    
54
    /**
55
     * Returns the AddressBaseConsumer singleton instance.
56
     * 
57
     * @return AddressBaseConsumer
58
     */
59 95
    public function getAddressBaseConsumer()
60
    {
61 95
        return AddressBaseConsumer::getInstance($this, $this->partFactory);
62
    }
63
    
64
    /**
65
     * Returns the AddressConsumer singleton instance.
66
     * 
67
     * @return AddressConsumer
68
     */
69 95
    public function getAddressConsumer()
70
    {
71 95
        return AddressConsumer::getInstance($this, $this->partFactory);
72
    }
73
    
74
    /**
75
     * Returns the AddressGroupConsumer singleton instance.
76
     * 
77
     * @return AddressGroupConsumer
78
     */
79 95
    public function getAddressGroupConsumer()
80
    {
81 95
        return AddressGroupConsumer::getInstance($this, $this->partFactory);
82
    }
83
    
84
    /**
85
     * Returns the AddressEmailConsumer singleton instance.
86
     * 
87
     * @return AddressEmailConsumer
88
     */
89 95
    public function getAddressEmailConsumer()
90
    {
91 95
        return AddressEmailConsumer::getInstance($this, $this->partFactory);
92
    }
93
    
94
    /**
95
     * Returns the CommentConsumer singleton instance.
96
     * 
97
     * @return CommentConsumer
98
     */
99 105
    public function getCommentConsumer()
100
    {
101 105
        return CommentConsumer::getInstance($this, $this->partFactory);
102
    }
103
    
104
    /**
105
     * Returns the GenericConsumer singleton instance.
106
     * 
107
     * @return GenericConsumer
108
     */
109 95
    public function getGenericConsumer()
110
    {
111 95
        return GenericConsumer::getInstance($this, $this->mimeLiteralPartFactory);
112
    }
113
114
    /**
115
     * Returns the SubjectConsumer singleton instance.
116
     * 
117
     * @return SubjectConsumer
118
     */
119 94
    public function getSubjectConsumer()
120
    {
121 94
        return SubjectConsumer::getInstance($this, $this->mimeLiteralPartFactory);
122
    }
123
    
124
    /**
125
     * Returns the QuotedStringConsumer singleton instance.
126
     * 
127
     * @return QuotedStringConsumer
128
     */
129 105
    public function getQuotedStringConsumer()
130
    {
131 105
        return QuotedStringConsumer::getInstance($this, $this->partFactory);
132
    }
133
    
134
    /**
135
     * Returns the DateConsumer singleton instance.
136
     * 
137
     * @return DateConsumer
138
     */
139 19
    public function getDateConsumer()
140
    {
141 19
        return DateConsumer::getInstance($this, $this->partFactory);
142
    }
143
    
144
    /**
145
     * Returns the ParameterConsumer singleton instance.
146
     * 
147
     * @return ParameterConsumer
148
     */
149 101
    public function getParameterConsumer()
150
    {
151 101
        return ParameterConsumer::getInstance($this, $this->partFactory);
152
    }
153
154
    /**
155
     * Returns the consumer instance corresponding to the passed part name of a
156
     * Received header.
157
     *
158
     * @param string $partName
159
     * @return AbstractConsumer
160
     */
161
    public function getSubReceivedConsumer($partName)
162
    {
163
        if (empty($this->receivedConsumers[$partName])) {
164
            $consumer = null;
165
            if ($partName === 'from' || $partName === 'by') {
166
                $consumer = new DomainConsumer($this, $this->partFactory, $partName);
167
            } elseif ($partName === 'date') {
168
                $consumer = new ReceivedDateConsumer($this, $this->partFactory);
169
            } else {
170
                $consumer = new GenericReceivedConsumer($this, $this->partFactory, $partName);
171
            }
172
            $this->receivedConsumers[$partName] = $consumer;
173
        }
174
        return $this->receivedConsumers[$partName];
175
    }
176
177
    /**
178
     * Returns the ReceivedConsumer singleton instance.
179
     *
180
     * @return ReceivedConsumer
181
     */
182
    public function getReceivedConsumer()
183
    {
184
        return ReceivedConsumer::getInstance($this, $this->partFactory);
185
    }
186
187
    /**
188
     * Returns the IdConsumer singleton instance.
189
     *
190
     * @return IdConsumer
191
     */
192 26
    public function getIdConsumer()
193
    {
194 26
        return IdConsumer::getInstance($this, $this->partFactory);
195
    }
196
197
    /**
198
     * Returns the IdBaseConsumer singleton instance.
199
     *
200
     * @return IdBaseConsumer
201
     */
202 26
    public function getIdBaseConsumer()
203
    {
204 26
        return IdBaseConsumer::getInstance($this, $this->partFactory);
205
    }
206
}
207