Completed
Push — master ( d4566f...31a5e9 )
by Zaahid
02:27
created

SimpleDi::getConsumerService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 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;
8
9
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
10
use ZBateson\MailMimeParser\Header\HeaderFactory;
11
12
/**
13
 * Dependency injection container for use by ZBateson\MailMimeParser - because a
14
 * more complex one seems like overkill.
15
 * 
16
 * Constructs objects and whatever dependencies they require.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class SimpleDi
21
{
22
    /**
23
     * @var \ZBateson\MailMimeParser\MimePartFactory singleton 'service' instance
24
     */
25
    protected $partFactory;
26
    
27
    /**
28
     * @var \ZBateson\MailMimeParser\PartStreamRegistry singleton 'service'
29
     * instance
30
     */
31
    protected $partStreamRegistry;
32
    
33
    /**
34
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory singleton 'service'
35
     * instance
36
     */
37
    protected $headerFactory;
38
    
39
    /**
40
     * @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory singleton
41
     * 'service' instance
42
     */
43
    protected $headerPartFactory;
44
    
45
    /**
46
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
47
     * singleton 'service' instance
48
     */
49
    protected $mimeLiteralPartFactory;
50
    
51
    /**
52
     * @var \ZBateson\MailMimeParser\Header\Consumer\ConsumerService singleton
53
     * 'service' instance
54
     */
55
    protected $consumerService;
56
    
57
    /**
58
     * Constructs a SimpleDi - call singleton() to invoke
59
     */
60
    private function __construct()
61
    {
62
    }
63
    
64
    /**
65
     * Returns the singleton instance.
66
     * 
67
     * @return \ZBateson\MailMimeParser\SimpleDi
68
     */
69
    public static function singleton()
70
    {
71
        static $singleton = null;
72
        if (empty($singleton)) {
73
            $singleton = new SimpleDi();
74
        }
75
        return $singleton;
76
    }
77
    
78
    /**
79
     * Returns a singleton 'service' instance for the given service named $var
80
     * with a class type of $class.
81
     * 
82
     * @param string $var the name of the service
83
     * @param string $class the name of the class
84
     * @return mixed the service object
85
     */
86
    protected function getInstance($var, $class)
87
    {
88
        if ($this->$var === null) {
89
            $this->$var = new $class();
90
        }
91
        return $this->$var;
92
    }
93
    
94
    /**
95
     * Constructs and returns a new MessageParser object.
96
     * 
97
     * @return \ZBateson\MailMimeParser\MessageParser
98
     */
99
    public function newMessageParser()
100
    {
101
        return new MessageParser(
102
            $this->newMessage(),
103
            $this->getPartFactory(),
104
            $this->getPartStreamRegistry()
105
        );
106
    }
107
    
108
    /**
109
     * Constructs and returns a new Message object.
110
     * 
111
     * @return \ZBateson\MailMimeParser\Message
112
     */
113
    public function newMessage()
114
    {
115
        return new Message(
116
            $this->getHeaderFactory()
117
        );
118
    }
119
    
120
    /**
121
     * Returns the part factory service instance.
122
     * 
123
     * @return \ZBateson\MailMimeParser\MimePartFactory
124
     */
125
    public function getPartFactory()
126
    {
127
        if ($this->partFactory === null) {
128
            $this->partFactory = new MimePartFactory(
129
                $this->getHeaderFactory()
130
            );
131
        }
132
        return $this->partFactory;
133
    }
134
    
135
    /**
136
     * Returns the header factory service instance.
137
     * 
138
     * @return \ZBateson\MailMimeParser\Header\HeaderFactory
139
     */
140
    public function getHeaderFactory()
141
    {
142
        if ($this->headerFactory === null) {
143
            $this->headerFactory = new HeaderFactory($this->getConsumerService());
144
        }
145
        return $this->headerFactory;
146
    }
147
    
148
    /**
149
     * Returns the part stream registry service instance.  The method also
150
     * registers the stream extension by calling registerStreamExtensions.
151
     * 
152
     * @return \ZBateson\MailMimeParser\PartStreamRegistry
153
     */
154
    public function getPartStreamRegistry()
155
    {
156
        if ($this->partStreamRegistry === null) {
157
            $this->registerStreamExtensions();
158
        }
159
        return $this->getInstance('partStreamRegistry', __NAMESPACE__ . '\PartStreamRegistry');
160
    }
161
    
162
    /**
163
     * Returns the part factory service
164
     * 
165
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory
166
     */
167
    public function getHeaderPartFactory()
168
    {
169
        return $this->getInstance('headerPartFactory', __NAMESPACE__ . '\Header\Part\HeaderPartFactory');
170
    }
171
    
172
    /**
173
     * Returns the MimeLiteralPartFactory service
174
     * 
175
     * @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
176
     */
177
    public function getMimeLiteralPartFactory()
178
    {
179
        return $this->getInstance('mimeLiteralPartFactory', __NAMESPACE__ . '\Header\Part\MimeLiteralPartFactory');
180
    }
181
    
182
    /**
183
     * Returns the header consumer service
184
     * 
185
     * @return ZBateson\MailMimeParser\Header\Consumer\ConsumerService
186
     */
187
    public function getConsumerService()
188
    {
189
        if ($this->consumerService === null) {
190
            $this->consumerService = new ConsumerService(
191
                $this->getHeaderPartFactory(),
192
                $this->getMimeLiteralPartFactory()
193
            );
194
        }
195
        return $this->consumerService;
196
    }
197
    
198
    /**
199
     * Registers stream extensions for PartStream and CharsetStreamFilter
200
     * 
201
     * @see stream_filter_register
202
     * @see stream_wrapper_register
203
     */
204
    protected function registerStreamExtensions()
205
    {
206
        stream_filter_register(UUEncodeStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\UUEncodeStreamFilter');
207
        stream_filter_register(CharsetStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\CharsetStreamFilter');
208
        stream_wrapper_register(PartStream::STREAM_WRAPPER_PROTOCOL, __NAMESPACE__ . '\PartStream');
209
    }
210
}
211