Passed
Push — 1.0.0 ( d49389...cbb558 )
by Zaahid
03:21
created

SimpleDi::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\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 SimpleDi
29
{
30
    /**
31
     * @var type 
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\type 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...
32
     */
33
    protected $partBuilderFactory;
34
    
35
    /**
36
     * @var type 
37
     */
38
    protected $partFactoryService;
39
    
40
    /**
41
     * @var type 
42
     */
43
    protected $partFilterFactory;
44
    
45
    /**
46
     * @var type 
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
    protected $streamFactory;
80
    
81
    /**
82
     * Constructs a SimpleDi - call singleton() to invoke
83
     */
84 1
    private function __construct()
85
    {
86 1
    }
87
    
88
    /**
89
     * Returns the singleton instance.
90
     * 
91
     * @return \ZBateson\MailMimeParser\SimpleDi
92
     */
93 7
    public static function singleton()
94
    {
95 7
        static $singleton = null;
96 7
        if ($singleton === null) {
97 1
            $singleton = new SimpleDi();
98
        }
99 7
        return $singleton;
100
    }
101
    
102
    /**
103
     * Returns a singleton 'service' instance for the given service named $var
104
     * with a class type of $class.
105
     * 
106
     * @param string $var the name of the service
107
     * @param string $class the name of the class
108
     * @return mixed the service object
109
     */
110 1
    protected function getInstance($var, $class)
111
    {
112 1
        if ($this->$var === null) {
113 1
            $this->$var = new $class();
114
        }
115 1
        return $this->$var;
116
    }
117
    
118
    /**
119
     * Constructs and returns a new MessageParser object.
120
     * 
121
     * @return \ZBateson\MailMimeParser\Message\MessageParser
122
     */
123 1
    public function newMessageParser()
124
    {
125 1
        return new MessageParser(
126 1
            $this->getPartFactoryService(),
127 1
            $this->getPartBuilderFactory()
128
        );
129
    }
130
    
131
    /**
132
     * Returns a MessageHelperService instance.
133
     * 
134
     * @return MessageHelperService
135
     */
136 1
    public function getMessageHelperService()
137
    {
138 1
        if ($this->messageHelperService === null) {
139 1
            $this->messageHelperService = new MessageHelperService(
140 1
                $this->getPartBuilderFactory()
141
            );
142 1
            $this->messageHelperService->setPartFactoryService(
143 1
                $this->getPartFactoryService()
144
            );
145
        }
146 1
        return $this->messageHelperService;
147
    }
148
    
149 1
    public function getPartFilterFactory()
150
    {
151 1
        return $this->getInstance(
152 1
            'partFilterFactory',
153 1
            __NAMESPACE__ . '\Message\PartFilterFactory'
154
        );
155
    }
156
    
157
    /**
158
     * 
159
     * @return type
160
     */
161 1
    public function getPartFactoryService()
162
    {
163 1
        if ($this->partFactoryService === null) {
164 1
            $this->partFactoryService = new PartFactoryService(
0 ignored issues
show
Documentation Bug introduced by
It seems like new ZBateson\MailMimePar...MessageHelperService()) of type ZBateson\MailMimeParser\...tory\PartFactoryService is incompatible with the declared type 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...
165 1
                $this->getHeaderFactory(),
166 1
                $this->getPartFilterFactory(),
167 1
                $this->getStreamFactory(),
168 1
                $this->getPartStreamFilterManagerFactory(),
169 1
                $this->getMessageHelperService()
170
            );
171
        }
172 1
        return $this->partFactoryService;
173
    }
174
175 1
    public function getPartBuilderFactory()
176
    {
177 1
        if ($this->partBuilderFactory === null) {
178 1
            $this->partBuilderFactory = new PartBuilderFactory(
0 ignored issues
show
Documentation Bug introduced by
It seems like new ZBateson\MailMimePar...is->getHeaderFactory()) of type ZBateson\MailMimeParser\...tory\PartBuilderFactory is incompatible with the declared type 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...
179 1
                $this->getHeaderFactory()
180
            );
181
        }
182 1
        return $this->partBuilderFactory;
183
    }
184
    
185
    /**
186
     * Returns the header factory service instance.
187
     * 
188
     * @return \ZBateson\MailMimeParser\Header\HeaderFactory
189
     */
190 2
    public function getHeaderFactory()
191
    {
192 2
        if ($this->headerFactory === null) {
193 1
            $this->headerFactory = new HeaderFactory($this->getConsumerService());
194
        }
195 2
        return $this->headerFactory;
196
    }
197
198 1
    public function getStreamFactory()
199
    {
200 1
        return $this->getInstance(
201 1
            'streamFactory',
202 1
            __NAMESPACE__ . '\Stream\StreamFactory'
203
        );
204
    }
205
    
206 1
    public function getPartStreamFilterManagerFactory()
207
    {
208 1
        if ($this->partStreamFilterManagerFactory === null) {
209 1
            $this->partStreamFilterManagerFactory = new PartStreamFilterManagerFactory(
0 ignored issues
show
Documentation Bug introduced by
It seems like new ZBateson\MailMimePar...is->getStreamFactory()) of type ZBateson\MailMimeParser\...eamFilterManagerFactory is incompatible with the declared type 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...
210 1
                $this->getStreamFactory()
211
            );
212
        }
213 1
        return $this->getInstance(
214 1
            'partStreamFilterManagerFactory',
215 1
            __NAMESPACE__ . '\Message\Part\PartStreamFilterManagerFactory'
216
        );
217
    }
218
    
219 2
    public function getCharsetConverter()
220
    {
221 2
        return new CharsetConverter();
222
    }
223
    
224
    /**
225
     * Returns the part factory service
226
     * 
227
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory
228
     */
229 2
    public function getHeaderPartFactory()
230
    {
231 2
        if ($this->headerPartFactory === null) {
232 1
            $this->headerPartFactory = new HeaderPartFactory($this->getCharsetConverter());
233
        }
234 2
        return $this->headerPartFactory;
235
    }
236
    
237
    /**
238
     * Returns the MimeLiteralPartFactory service
239
     * 
240
     * @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
241
     */
242 2
    public function getMimeLiteralPartFactory()
243
    {
244 2
        if ($this->mimeLiteralPartFactory === null) {
245 1
            $this->mimeLiteralPartFactory = new MimeLiteralPartFactory($this->getCharsetConverter());
246
        }
247 2
        return $this->mimeLiteralPartFactory;
248
    }
249
    
250
    /**
251
     * Returns the header consumer service
252
     * 
253
     * @return \ZBateson\MailMimeParser\Header\Consumer\ConsumerService
254
     */
255 2
    public function getConsumerService()
256
    {
257 2
        if ($this->consumerService === null) {
258 1
            $this->consumerService = new ConsumerService(
259 1
                $this->getHeaderPartFactory(),
260 1
                $this->getMimeLiteralPartFactory()
261
            );
262
        }
263 2
        return $this->consumerService;
264
    }
265
    
266
}
267