Passed
Push — 2.0 ( df1b1e...20b3e4 )
by Zaahid
08:44 queued 05:27
created

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