Passed
Push — master ( 6abe24...e0ab70 )
by Zaahid
03:24
created

Container::getConsumerService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
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;
8
9
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
10
use ZBateson\MailMimeParser\Header\HeaderFactory;
11
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
12
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
13
use ZBateson\MailMimeParser\Message\Helper\MessageHelperService;
14
use ZBateson\MailMimeParser\Message\MessageParser;
15
use ZBateson\MailMimeParser\Message\Part\Factory\PartBuilderFactory;
16
use ZBateson\MailMimeParser\Message\Part\Factory\PartFactoryService;
17
use ZBateson\MailMimeParser\Message\Part\Factory\PartStreamFilterManagerFactory;
18
use ZBateson\StreamDecorators\Util\CharsetConverter;
19
20
/**
21
 * Dependency injection container for use by ZBateson\MailMimeParser - because a
22
 * more complex one seems like overkill.
23
 * 
24
 * Constructs objects and whatever dependencies they require.
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class Container
29
{
30
    /**
31
     * @var PartBuilderFactory
32
     */
33
    protected $partBuilderFactory;
34
    
35
    /**
36
     * @var PartFactoryService
37
     */
38
    protected $partFactoryService;
39
    
40
    /**
41
     * @var PartFilterFactory
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\PartFilterFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
     */
43
    protected $partFilterFactory;
44
    
45
    /**
46
     * @var PartStreamFilterManagerFactory
47
     */
48
    protected $partStreamFilterManagerFactory;
49
    
50
    /**
51
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory singleton 'service'
52
     * instance
53
     */
54
    protected $headerFactory;
55
    
56
    /**
57
     * @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory singleton
58
     * 'service' instance
59
     */
60
    protected $headerPartFactory;
61
    
62
    /**
63
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
64
     * singleton 'service' instance
65
     */
66
    protected $mimeLiteralPartFactory;
67
    
68
    /**
69
     * @var \ZBateson\MailMimeParser\Header\Consumer\ConsumerService singleton
70
     * 'service' instance
71
     */
72
    protected $consumerService;
73
    
74
    /**
75
     * @var MessageHelperService Used to get MessageHelper singletons
76
     */
77
    protected $messageHelperService;
78
79
    /**
80
     * @var StreamFactory
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\StreamFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
     */
82
    protected $streamFactory;
83
    
84
    /**
85
     * Constructs a Container - call singleton() to invoke
86
     */
87 6
    public function __construct()
88
    {
89 6
    }
90
91
    /**
92
     * Returns a singleton 'service' instance for the given service named $var
93
     * with a class type of $class.
94
     * 
95
     * @param string $var the name of the service
96
     * @param string $class the name of the class
97
     * @return mixed the service object
98
     */
99 1
    protected function getInstance($var, $class)
100
    {
101 1
        if ($this->$var === null) {
102 1
            $this->$var = new $class();
103
        }
104 1
        return $this->$var;
105
    }
106
    
107
    /**
108
     * Constructs and returns a new MessageParser object.
109
     * 
110
     * @return \ZBateson\MailMimeParser\Message\MessageParser
111
     */
112 1
    public function newMessageParser()
113
    {
114 1
        return new MessageParser(
115 1
            $this->getPartFactoryService(),
116 1
            $this->getPartBuilderFactory()
117
        );
118
    }
119
    
120
    /**
121
     * Returns a MessageHelperService instance.
122
     * 
123
     * @return MessageHelperService
124
     */
125 1
    public function getMessageHelperService()
126
    {
127 1
        if ($this->messageHelperService === null) {
128 1
            $this->messageHelperService = new MessageHelperService(
129 1
                $this->getPartBuilderFactory()
130
            );
131 1
            $this->messageHelperService->setPartFactoryService(
132 1
                $this->getPartFactoryService()
133
            );
134
        }
135 1
        return $this->messageHelperService;
136
    }
137
138
    /**
139
     * Returns a PartFilterFactory instance
140
     *
141
     * @return PartFilterFactory
142
     */
143 1
    public function getPartFilterFactory()
144
    {
145 1
        return $this->getInstance(
146 1
            'partFilterFactory',
147 1
            __NAMESPACE__ . '\Message\PartFilterFactory'
148
        );
149
    }
150
    
151
    /**
152
     * Returns a PartFactoryService singleton.
153
     * 
154
     * @return PartFactoryService
155
     */
156 1
    public function getPartFactoryService()
157
    {
158 1
        if ($this->partFactoryService === null) {
159 1
            $this->partFactoryService = new PartFactoryService(
160 1
                $this->getPartFilterFactory(),
161 1
                $this->getStreamFactory(),
162 1
                $this->getPartStreamFilterManagerFactory(),
163 1
                $this->getMessageHelperService()
164
            );
165
        }
166 1
        return $this->partFactoryService;
167
    }
168
169
    /**
170
     * Returns a PartBuilderFactory instance.
171
     * 
172
     * @return PartBuilderFactory
173
     */
174 1
    public function getPartBuilderFactory()
175
    {
176 1
        if ($this->partBuilderFactory === null) {
177 1
            $this->partBuilderFactory = new PartBuilderFactory(
178 1
                $this->getHeaderFactory()
179
            );
180
        }
181 1
        return $this->partBuilderFactory;
182
    }
183
    
184
    /**
185
     * Returns the header factory service instance.
186
     * 
187
     * @return \ZBateson\MailMimeParser\Header\HeaderFactory
188
     */
189 2
    public function getHeaderFactory()
190
    {
191 2
        if ($this->headerFactory === null) {
192 2
            $this->headerFactory = new HeaderFactory($this->getConsumerService());
193
        }
194 2
        return $this->headerFactory;
195
    }
196
197
    /**
198
     * Returns a StreamFactory.
199
     *
200
     * @return StreamFactory
201
     */
202 1
    public function getStreamFactory()
203
    {
204 1
        return $this->getInstance(
205 1
            'streamFactory',
206 1
            __NAMESPACE__ . '\Stream\StreamFactory'
207
        );
208
    }
209
210
    /**
211
     * Returns a PartStreamFilterManagerFactory.
212
     * 
213
     * @return PartStreamFilterManagerFactory
214
     */
215 1
    public function getPartStreamFilterManagerFactory()
216
    {
217 1
        if ($this->partStreamFilterManagerFactory === null) {
218 1
            $this->partStreamFilterManagerFactory = new PartStreamFilterManagerFactory(
219 1
                $this->getStreamFactory()
220
            );
221
        }
222 1
        return $this->getInstance(
223 1
            'partStreamFilterManagerFactory',
224 1
            __NAMESPACE__ . '\Message\Part\PartStreamFilterManagerFactory'
225
        );
226
    }
227
228
    /**
229
     * Returns a CharsetConverter.
230
     * 
231
     * @return CharsetConverter
232
     */
233 6
    public function getCharsetConverter()
234
    {
235 6
        return new CharsetConverter();
236
    }
237
    
238
    /**
239
     * Returns the part factory service
240
     * 
241
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory
242
     */
243 4
    public function getHeaderPartFactory()
244
    {
245 4
        if ($this->headerPartFactory === null) {
246 4
            $this->headerPartFactory = new HeaderPartFactory($this->getCharsetConverter());
247
        }
248 4
        return $this->headerPartFactory;
249
    }
250
    
251
    /**
252
     * Returns the MimeLiteralPartFactory service
253
     * 
254
     * @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
255
     */
256 4
    public function getMimeLiteralPartFactory()
257
    {
258 4
        if ($this->mimeLiteralPartFactory === null) {
259 4
            $this->mimeLiteralPartFactory = new MimeLiteralPartFactory($this->getCharsetConverter());
260
        }
261 4
        return $this->mimeLiteralPartFactory;
262
    }
263
    
264
    /**
265
     * Returns the header consumer service
266
     * 
267
     * @return \ZBateson\MailMimeParser\Header\Consumer\ConsumerService
268
     */
269 3
    public function getConsumerService()
270
    {
271 3
        if ($this->consumerService === null) {
272 3
            $this->consumerService = new ConsumerService(
273 3
                $this->getHeaderPartFactory(),
274 3
                $this->getMimeLiteralPartFactory()
275
            );
276
        }
277 3
        return $this->consumerService;
278
    }
279
    
280
}
281