Passed
Push — 1.0.0 ( a1adee...bca153 )
by Zaahid
03:20
created

MailMimeParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
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
/**
10
 * Parses a MIME message into a \ZBateson\MailMimeParser\Message object.
11
 *
12
 * To invoke, call parse on a MailMimeParser object.
13
 * 
14
 * $handle = fopen('path/to/file.txt');
15
 * $parser = new MailMimeParser();
16
 * $parser->parse($handle);
17
 * fclose($handle);
18
 * 
19
 * @author Zaahid Bateson
20
 */
21
class MailMimeParser
22
{
23
    /**
24
     * @var string defines the default charset used by MessagePart.
25
     */
26
    const DEFAULT_CHARSET = 'UTF-8';
27
28
    /**
29
     * @var \ZBateson\MailMimeParser\SimpleDi dependency injection container
30
     */
31
    protected $di;
32
    
33
    /**
34
     * Sets up the parser.
35
     */
36 3
    public function __construct()
37
    {
38 3
        $this->di = SimpleDi::singleton();
39 3
    }
40
    
41
    /**
42
     * Sets the default charset used by MMP for strings returned by read
43
     * operations on text content (e.g. MessagePart::getContentResourceHandle,
44
     * getContent, etc...)
45
     * 
46
     * @param string $charset
47
     */
48
    public static function setDefaultCharset($charset)
49
    {
50
        self::$defaultCharset = $charset;
51
    }
52
    
53
    /**
54
     * Returns the default charset that will be used by MMP strings returned.
55
     * 
56
     * @return string
57
     */
58
    public static function getDefaultCharset()
59
    {
60
        return self::$defaultCharset;
61
    }
62
    
63
    /**
64
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
65
     * object and returns it.
66
     * 
67
     * Internally, the message is first copied to a temp stream (with php://temp
68
     * which may keep it in memory or write it to disk) and its stream is used.
69
     * That way if the message is too large to hold in memory it can be written
70
     * to a temporary file if need be.
71
     * 
72
     * @param resource|string $handleOrString the resource handle to the input
73
     *        stream of the mime message, or a string containing a mime message
74
     * @return \ZBateson\MailMimeParser\Message
75
     */
76 2
    public function parse($handleOrString)
77
    {
78
        // $tempHandle is attached to $message, and closed in its destructor
79 2
        $tempHandle = fopen('php://temp', 'r+');
80 2
        if (is_string($handleOrString)) {
81 1
            fwrite($tempHandle, $handleOrString);
82 1
        } else {
83 1
            stream_copy_to_stream($handleOrString, $tempHandle);
84
        }
85 2
        rewind($tempHandle);
86 2
        $parser = $this->di->newMessageParser();
87 2
        return $parser->parse($tempHandle);
88
    }
89
}
90