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

MailMimeParser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
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 GuzzleHttp\Psr7;
10
11
/**
12
 * Parses a MIME message into a \ZBateson\MailMimeParser\Message object.
13
 *
14
 * To invoke, call parse on a MailMimeParser object.
15
 * 
16
 * $handle = fopen('path/to/file.txt');
17
 * $parser = new MailMimeParser();
18
 * $parser->parse($handle);
19
 * fclose($handle);
20
 * 
21
 * @author Zaahid Bateson
22
 */
23
class MailMimeParser
24
{
25
    /**
26
     * @var string defines the default charset used by MessagePart.
27
     */
28
    const DEFAULT_CHARSET = 'UTF-8';
29
30
    /**
31
     * @var \ZBateson\MailMimeParser\Container dependency injection container
32
     */
33
    protected $di;
34
    
35
    /**
36
     * Sets up the parser.
37
     */
38 3
    public function __construct(Container $di = null)
39
    {
40 3
        if ($di === null) {
41
            $di = new Container();
42
        }
43 3
        $this->di = $di;
44 3
    }
45
46
    /**
47
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
48
     * object and returns it.
49
     * 
50
     * Internally, the message is first copied to a temp stream (with php://temp
51
     * which may keep it in memory or write it to disk) and its stream is used.
52
     * That way if the message is too large to hold in memory it can be written
53
     * to a temporary file if need be.
54
     * 
55
     * @param resource|string $handleOrString the resource handle to the input
56
     *        stream of the mime message, or a string containing a mime message
57
     * @return \ZBateson\MailMimeParser\Message
58
     */
59 2
    public function parse($handleOrString)
60
    {
61 2
        $stream = Psr7\stream_for($handleOrString);
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($handleOrString);
Loading history...
62 2
        $copy = Psr7\stream_for(fopen('php://temp', 'r+'));
63
64 2
        Psr7\copy_to_stream($stream, $copy);
0 ignored issues
show
Bug introduced by
The function copy_to_stream was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        /** @scrutinizer ignore-call */ 
65
        Psr7\copy_to_stream($stream, $copy);
Loading history...
65 2
        $copy->rewind();
66
67
        // don't close it when $stream gets destroyed
68 2
        $stream->detach();
69 2
        $parser = $this->di->newMessageParser();
70 2
        return $parser->parse($copy);
71
    }
72
}
73