Passed
Push — 1.0.0 ( 4505d9...06b3ad )
by Zaahid
04:10
created

SimpleDi::getStreamDecoratorFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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\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\Message\Part\PartStreamFilterManagerFactory;
17
use ZBateson\StreamDecorators\Util\CharsetConverter;
18
19
/**
20
 * Dependency injection container for use by ZBateson\MailMimeParser - because a
21
 * more complex one seems like overkill.
22
 * 
23
 * Constructs objects and whatever dependencies they require.
24
 *
25
 * @author Zaahid Bateson
26
 */
27
class SimpleDi
28
{
29
    /**
30
     * @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...
31
     */
32
    protected $partBuilderFactory;
33
    
34
    /**
35
     * @var type 
36
     */
37
    protected $partFactoryService;
38
    
39
    /**
40
     * @var type 
41
     */
42
    protected $partFilterFactory;
43
    
44
    /**
45
     * @var type 
46
     */
47
    protected $partStreamFilterManagerFactory;
48
    
49
    /**
50
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory singleton 'service'
51
     * instance
52
     */
53
    protected $headerFactory;
54
    
55
    /**
56
     * @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory singleton
57
     * 'service' instance
58
     */
59
    protected $headerPartFactory;
60
    
61
    /**
62
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
63
     * singleton 'service' instance
64
     */
65
    protected $mimeLiteralPartFactory;
66
    
67
    /**
68
     * @var \ZBateson\MailMimeParser\Header\Consumer\ConsumerService singleton
69
     * 'service' instance
70
     */
71
    protected $consumerService;
72
    
73
    /**
74
     * @var \ZBateson\MailMimeParser\Message\Writer\MessageWriterService 
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\...er\MessageWriterService 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...
75
     * singleton 'service' instance for getting MimePartWriter and MessageWriter
76
     * instances
77
     */
78
    protected $messageWriterService;
79
80
    protected $streamDecoratorFactory;
81
    
82
    /**
83
     * Constructs a SimpleDi - call singleton() to invoke
84
     */
85 1
    private function __construct()
86
    {
87 1
    }
88
    
89
    /**
90
     * Returns the singleton instance.
91
     * 
92
     * @return \ZBateson\MailMimeParser\SimpleDi
93
     */
94 7
    public static function singleton()
95
    {
96 7
        static $singleton = null;
97 7
        if ($singleton === null) {
98 1
            $singleton = new SimpleDi();
99
        }
100 7
        return $singleton;
101
    }
102
    
103
    /**
104
     * Returns a singleton 'service' instance for the given service named $var
105
     * with a class type of $class.
106
     * 
107
     * @param string $var the name of the service
108
     * @param string $class the name of the class
109
     * @return mixed the service object
110
     */
111 1
    protected function getInstance($var, $class)
112
    {
113 1
        if ($this->$var === null) {
114 1
            $this->$var = new $class();
115
        }
116 1
        return $this->$var;
117
    }
118
    
119
    /**
120
     * Constructs and returns a new MessageParser object.
121
     * 
122
     * @return \ZBateson\MailMimeParser\Message\MessageParser
123
     */
124 1
    public function newMessageParser()
125
    {
126 1
        return new MessageParser(
127 1
            $this->getPartFactoryService(),
128 1
            $this->getPartBuilderFactory()
129
        );
130
    }
131
    
132
    /**
133
     * Returns a MessageWriterService instance.
134
     * 
135
     * @return MessageWriterService
136
     */
137
    public function getMessageWriterService()
138
    {
139
        if ($this->messageWriterService === null) {
140
            $this->messageWriterService = new MessageWriterService();
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\MessageWriterService 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...
141
        }
142
        return $this->messageWriterService;
143
    }
144
    
145 1
    public function getPartFilterFactory()
146
    {
147 1
        return $this->getInstance(
148 1
            'partFilterFactory',
149 1
            __NAMESPACE__ . '\Message\PartFilterFactory'
150
        );
151
    }
152
    
153
    /**
154
     * 
155
     * @return type
156
     */
157 1
    public function getPartFactoryService()
158
    {
159 1
        if ($this->partFactoryService === null) {
160 1
            $this->partFactoryService = new PartFactoryService(
0 ignored issues
show
Documentation Bug introduced by
It seems like new ZBateson\MailMimePar...FilterManagerFactory()) of type ZBateson\MailMimeParser\...Part\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...
161 1
                $this->getHeaderFactory(),
162 1
                $this->getPartFilterFactory(),
163 1
                $this->getStreamDecoratorFactory(),
164 1
                $this->getPartStreamFilterManagerFactory()
165
            );
166
        }
167 1
        return $this->partFactoryService;
168
    }
169
170 1
    public function getPartBuilderFactory()
171
    {
172 1
        if ($this->partBuilderFactory === null) {
173 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\...Part\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...
174 1
                $this->getHeaderFactory()
175
            );
176
        }
177 1
        return $this->partBuilderFactory;
178
    }
179
    
180
    /**
181
     * Returns the header factory service instance.
182
     * 
183
     * @return \ZBateson\MailMimeParser\Header\HeaderFactory
184
     */
185 2
    public function getHeaderFactory()
186
    {
187 2
        if ($this->headerFactory === null) {
188 1
            $this->headerFactory = new HeaderFactory($this->getConsumerService());
189
        }
190 2
        return $this->headerFactory;
191
    }
192
193 1
    public function getStreamDecoratorFactory()
194
    {
195 1
        return $this->getInstance(
196 1
            'streamDecoratorFactory',
197 1
            __NAMESPACE__ . '\Stream\StreamDecoratorFactory'
198
        );
199
    }
200
    
201 1
    public function getPartStreamFilterManagerFactory()
202
    {
203 1
        if ($this->partStreamFilterManagerFactory === null) {
204 1
            $this->partStreamFilterManagerFactory = new PartStreamFilterManagerFactory(
0 ignored issues
show
Documentation Bug introduced by
It seems like new ZBateson\MailMimePar...reamDecoratorFactory()) 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...
205 1
                $this->getStreamDecoratorFactory()
206
            );
207
        }
208 1
        return $this->getInstance(
209 1
            'partStreamFilterManagerFactory',
210 1
            __NAMESPACE__ . '\Message\Part\PartStreamFilterManagerFactory'
211
        );
212
    }
213
    
214 2
    public function getCharsetConverter()
215
    {
216 2
        return new CharsetConverter();
217
    }
218
    
219
    /**
220
     * Returns the part factory service
221
     * 
222
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory
223
     */
224 2
    public function getHeaderPartFactory()
225
    {
226 2
        if ($this->headerPartFactory === null) {
227 1
            $this->headerPartFactory = new HeaderPartFactory($this->getCharsetConverter());
228
        }
229 2
        return $this->headerPartFactory;
230
    }
231
    
232
    /**
233
     * Returns the MimeLiteralPartFactory service
234
     * 
235
     * @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory
236
     */
237 2
    public function getMimeLiteralPartFactory()
238
    {
239 2
        if ($this->mimeLiteralPartFactory === null) {
240 1
            $this->mimeLiteralPartFactory = new MimeLiteralPartFactory($this->getCharsetConverter());
241
        }
242 2
        return $this->mimeLiteralPartFactory;
243
    }
244
    
245
    /**
246
     * Returns the header consumer service
247
     * 
248
     * @return \ZBateson\MailMimeParser\Header\Consumer\ConsumerService
249
     */
250 2
    public function getConsumerService()
251
    {
252 2
        if ($this->consumerService === null) {
253 1
            $this->consumerService = new ConsumerService(
254 1
                $this->getHeaderPartFactory(),
255 1
                $this->getMimeLiteralPartFactory()
256
            );
257
        }
258 2
        return $this->consumerService;
259
    }
260
    
261
}
262