Passed
Push — 1.0.0 ( a1adee...bca153 )
by Zaahid
03:20
created

SimpleDi::getHeaderPartFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
namespace ZBateson\MailMimeParser;
8
9
use ZBateson\MailMimeParser\Message\MessageParser;
10
use ZBateson\MailMimeParser\Message\Part\PartBuilderFactory;
11
use ZBateson\MailMimeParser\Message\Part\PartFactoryService;
12
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
13
use ZBateson\MailMimeParser\Header\HeaderFactory;
14
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
15
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
16
use ZBateson\MailMimeParser\Stream\PartStream;
17
use ZBateson\MailMimeParser\Stream\ConvertStreamFilter;
18
use ZBateson\MailMimeParser\Stream\UUDecodeStreamFilter;
19
use ZBateson\MailMimeParser\Stream\CharsetStreamFilter;
20
use ZBateson\MailMimeParser\Stream\Base64DecodeStreamFilter;
21
use ZBateson\MailMimeParser\Util\CharsetConverter;
22
use ZBateson\MailMimeParser\Message\Part\PartStreamFilterManagerFactory;
23
24
/**
25
 * Dependency injection container for use by ZBateson\MailMimeParser - because a
26
 * more complex one seems like overkill.
27
 * 
28
 * Constructs objects and whatever dependencies they require.
29
 *
30
 * @author Zaahid Bateson
31
 */
32
class SimpleDi
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
33
{
34
    /**
35
     * @var type 
36
     */
37
    protected $partBuilderFactory;
38
    
39
    /**
40
     * @var type 
41
     */
42
    protected $partFactoryService;
43
    
44
    /**
45
     * @var type 
46
     */
47
    protected $partFilterFactory;
48
    
49
    /**
50
     * @var type 
51
     */
52
    protected $partStreamFilterManagerFactory;
53
    
54
    /**
55
     * @var \ZBateson\MailMimeParser\Stream\PartStreamRegistry singleton
56
     * 'service' instance
57
     */
58
    protected $partStreamRegistry;
59
    
60
    /**
61
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory singleton 'service'
62
     * instance
63
     */
64
    protected $headerFactory;
65
    
66
    /**
67
     * @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory singleton
68
     * 'service' instance
69
     */
70
    protected $headerPartFactory;
71
    
72
    /**
73
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
74
     * singleton 'service' instance
75
     */
76
    protected $mimeLiteralPartFactory;
77
    
78
    /**
79
     * @var \ZBateson\MailMimeParser\Header\Consumer\ConsumerService singleton
80
     * 'service' instance
81
     */
82
    protected $consumerService;
83
    
84
    /**
85
     * @var \ZBateson\MailMimeParser\Message\Writer\MessageWriterService 
86
     * singleton 'service' instance for getting MimePartWriter and MessageWriter
87
     * instances
88
     */
89
    protected $messageWriterService;
90
    
91
    /**
92
     * Constructs a SimpleDi - call singleton() to invoke
93
     */
94 1
    private function __construct()
95
    {
96 1
    }
97
    
98
    /**
99
     * Returns the singleton instance.
100
     * 
101
     * @return \ZBateson\MailMimeParser\SimpleDi
102
     */
103 8
    public static function singleton()
104
    {
105 8
        static $singleton = null;
106 8
        if ($singleton === null) {
107 1
            $singleton = new SimpleDi();
108 1
        }
109 8
        return $singleton;
110
    }
111
    
112
    /**
113
     * Returns a singleton 'service' instance for the given service named $var
114
     * with a class type of $class.
115
     * 
116
     * @param string $var the name of the service
117
     * @param string $class the name of the class
118
     * @return mixed the service object
119
     */
120 2
    protected function getInstance($var, $class)
121
    {
122 2
        if ($this->$var === null) {
123 1
            $this->$var = new $class();
124 1
        }
125 2
        return $this->$var;
126
    }
127
    
128
    /**
129
     * Constructs and returns a new MessageParser object.
130
     * 
131
     * @return \ZBateson\MailMimeParser\Message\MessageParser
132
     */
133 1
    public function newMessageParser()
134
    {
135 1
        return new MessageParser(
136 1
            $this->getPartFactoryService(),
137 1
            $this->getPartBuilderFactory(),
138 1
            $this->getPartStreamRegistry()
139 1
        );
140
    }
141
    
142
    /**
143
     * Returns a MessageWriterService instance.
144
     * 
145
     * @return MessageWriterService
146
     */
147
    public function getMessageWriterService()
148
    {
149
        if ($this->messageWriterService === null) {
150
            $this->messageWriterService = new MessageWriterService();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ZBateson\MailMimePa...\MessageWriterService() of type object<ZBateson\MailMime...r\MessageWriterService> is incompatible with the declared type object<ZBateson\MailMime...r\MessageWriterService> of property $messageWriterService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
151
        }
152
        return $this->messageWriterService;
153
    }
154
    
155
    /**
156
     * Constructs and returns a new CharsetConverter object.
157
     * 
158
     * @return \ZBateson\MailMimeParser\Util\CharsetConverter
159
     */
160 1
    public function newCharsetConverter()
161
    {
162 1
        return new CharsetConverter();
163
    }
164
    
165
    
166 1
    public function getPartFilterFactory()
167
    {
168 1
        return $this->getInstance(
169 1
            'partFilterFactory',
170
            __NAMESPACE__ . '\Message\PartFilterFactory'
171 1
        );
172
    }
173
    
174
    /**
175
     * 
176
     * @return type
177
     */
178 1
    public function getPartFactoryService()
179
    {
180 1
        if ($this->partFactoryService === null) {
181 1
            $this->partFactoryService = new PartFactoryService(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ZBateson\MailMimePa...FilterManagerFactory()) of type object<ZBateson\MailMime...art\PartFactoryService> is incompatible with the declared type object<ZBateson\MailMimeParser\type> of property $partFactoryService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
182 1
                $this->getHeaderFactory(),
183 1
                $this->getPartFilterFactory(),
184 1
                $this->getPartStreamFilterManagerFactory()
185 1
            );
186 1
        }
187 1
        return $this->partFactoryService;
188
    }
189
190 1
    public function getPartBuilderFactory()
191
    {
192 1
        if ($this->partBuilderFactory === null) {
193 1
            $this->partBuilderFactory = new PartBuilderFactory(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ZBateson\MailMimePa...TREAM_WRAPPER_PROTOCOL) of type object<ZBateson\MailMime...art\PartBuilderFactory> is incompatible with the declared type object<ZBateson\MailMimeParser\type> of property $partBuilderFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
194 1
                $this->getHeaderFactory(),
195
                PartStream::STREAM_WRAPPER_PROTOCOL
196 1
            );
197 1
        }
198 1
        return $this->partBuilderFactory;
199
    }
200
    
201
    /**
202
     * Returns the header factory service instance.
203
     * 
204
     * @return \ZBateson\MailMimeParser\Header\HeaderFactory
205
     */
206 2
    public function getHeaderFactory()
207
    {
208 2
        if ($this->headerFactory === null) {
209 1
            $this->headerFactory = new HeaderFactory($this->getConsumerService());
210 1
        }
211 2
        return $this->headerFactory;
212
    }
213
    
214 1
    public function getPartStreamFilterManagerFactory()
215
    {
216 1
        if ($this->partStreamFilterManagerFactory === null) {
217 1
            $this->partStreamFilterManagerFactory = new PartStreamFilterManagerFactory(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ZBateson\MailMimePa...er::STREAM_FILTER_NAME) of type object<ZBateson\MailMime...amFilterManagerFactory> is incompatible with the declared type object<ZBateson\MailMimeParser\type> of property $partStreamFilterManagerFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
218 1
                ConvertStreamFilter::STREAM_DECODER_FILTER_NAME,
219 1
                Base64DecodeStreamFilter::STREAM_FILTER_NAME,
220 1
                UUDecodeStreamFilter::STREAM_FILTER_NAME,
221
                CharsetStreamFilter::STREAM_FILTER_NAME
222 1
            );
223 1
        }
224 1
        return $this->getInstance(
225 1
            'partStreamFilterManagerFactory',
226
            __NAMESPACE__ . '\Message\Part\PartStreamFilterManagerFactory'
227 1
        );
228
    }
229
    
230
    /**
231
     * Returns the part stream registry service instance.  The method also
232
     * registers the stream extension by calling registerStreamExtensions.
233
     * 
234
     * @return \ZBateson\MailMimeParser\Stream\PartStreamRegistry
235
     */
236 2
    public function getPartStreamRegistry()
237
    {
238 2
        if ($this->partStreamRegistry === null) {
239 1
            $this->registerStreamExtensions();
240 1
        }
241 2
        return $this->getInstance('partStreamRegistry', __NAMESPACE__ . '\Stream\PartStreamRegistry');
242
    }
243
    
244 1
    public function getCharsetConverter()
245
    {
246 1
        return new CharsetConverter();
247
    }
248
    
249
    /**
250
     * Returns the part factory service
251
     * 
252
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory
253
     */
254 2
    public function getHeaderPartFactory()
255
    {
256 2
        if ($this->headerPartFactory === null) {
257 1
            $this->headerPartFactory = new HeaderPartFactory($this->getCharsetConverter());
258 1
        }
259 2
        return $this->headerPartFactory;
260
    }
261
    
262
    /**
263
     * Returns the MimeLiteralPartFactory service
264
     * 
265
     * @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
266
     */
267 2
    public function getMimeLiteralPartFactory()
268
    {
269 2
        if ($this->mimeLiteralPartFactory === null) {
270 1
            $this->mimeLiteralPartFactory = new MimeLiteralPartFactory($this->getCharsetConverter());
271 1
        }
272 2
        return $this->mimeLiteralPartFactory;
273
    }
274
    
275
    /**
276
     * Returns the header consumer service
277
     * 
278
     * @return \ZBateson\MailMimeParser\Header\Consumer\ConsumerService
279
     */
280 2
    public function getConsumerService()
281
    {
282 2
        if ($this->consumerService === null) {
283 1
            $this->consumerService = new ConsumerService(
284 1
                $this->getHeaderPartFactory(),
285 1
                $this->getMimeLiteralPartFactory()
286 1
            );
287 1
        }
288 2
        return $this->consumerService;
289
    }
290
    
291
    /**
292
     * Registers stream extensions for PartStream and CharsetStreamFilter
293
     * 
294
     * @see stream_filter_register
295
     * @see stream_wrapper_register
296
     */
297 1
    protected function registerStreamExtensions()
298
    {
299 1
        stream_filter_register(
300 1
            UUDecodeStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\Stream\UUDecodeStreamFilter');
301 1
        stream_filter_register(CharsetStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\Stream\CharsetStreamFilter');
302 1
        stream_wrapper_register(PartStream::STREAM_WRAPPER_PROTOCOL, __NAMESPACE__ . '\Stream\PartStream');
303
        
304
        // originally created for HHVM compatibility, but decided to use them
305
        // instead of built-in stream filters for reliability -- it seems the
306
        // built-in base64-decode and encode stream filter does pretty much the
307
        // same thing as HHVM's -- it only works on smaller streams where the
308
        // entire stream comes in a single buffer.
309
        // In addition, in HHVM 3.15 there seems to be a problem registering
310
        // 'convert.quoted-printable-decode/encode -- so to make things simple
311
        // decided to use my version instead and name them mmp-convert.*
312
        // In 3.18-3.20, it seems we're not able to overwrite 'convert.*'
313
        // filters, so now they're all named mmp-convert.*
314 1
        stream_filter_register(
315 1
            ConvertStreamFilter::STREAM_DECODER_FILTER_NAME,
316
            __NAMESPACE__ . '\Stream\ConvertStreamFilter'
317 1
        );
318 1
        stream_filter_register(
319 1
            Base64DecodeStreamFilter::STREAM_FILTER_NAME,
320
            __NAMESPACE__ . '\Stream\Base64DecodeStreamFilter'
321 1
        );
322 1
    }
323
}
324