Passed
Push — master ( 9e6d2f...c6b62f )
by Zaahid
06:54
created

MailMimeParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 67
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDefaultCharset() 0 3 1
A getDefaultCharset() 0 3 1
A parse() 0 12 1
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\SimpleDi dependency injection container
32
     */
33
    protected $di;
34
    
35
    /**
36
     * Sets up the parser.
37
     */
38 3
    public function __construct()
39
    {
40 3
        $this->di = SimpleDi::singleton();
41 3
    }
42
    
43
    /**
44
     * Sets the default charset used by MMP for strings returned by read
45
     * operations on text content (e.g. MessagePart::getContentResourceHandle,
46
     * getContent, etc...)
47
     * 
48
     * @param string $charset
49
     */
50
    public static function setDefaultCharset($charset)
51
    {
52
        self::$defaultCharset = $charset;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultCharset does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
    }
54
    
55
    /**
56
     * Returns the default charset that will be used by MMP strings returned.
57
     * 
58
     * @return string
59
     */
60
    public static function getDefaultCharset()
61
    {
62
        return self::$defaultCharset;
63
    }
64
    
65
    /**
66
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
67
     * object and returns it.
68
     * 
69
     * Internally, the message is first copied to a temp stream (with php://temp
70
     * which may keep it in memory or write it to disk) and its stream is used.
71
     * That way if the message is too large to hold in memory it can be written
72
     * to a temporary file if need be.
73
     * 
74
     * @param resource|string $handleOrString the resource handle to the input
75
     *        stream of the mime message, or a string containing a mime message
76
     * @return \ZBateson\MailMimeParser\Message
77
     */
78 2
    public function parse($handleOrString)
79
    {
80 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

80
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($handleOrString);
Loading history...
81 2
        $copy = Psr7\stream_for(fopen('php://temp', 'r+'));
82
83 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

83
        /** @scrutinizer ignore-call */ 
84
        Psr7\copy_to_stream($stream, $copy);
Loading history...
84 2
        $copy->rewind();
85
86
        // don't close it when $stream gets destroyed
87 2
        $stream->detach();
88 2
        $parser = $this->di->newMessageParser();
89 2
        return $parser->parse($copy);
90
    }
91
}
92