Passed
Branch php8-testing (0e47ea)
by Zaahid
03:09
created

MailMimeParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 53
ccs 13
cts 13
cp 1
rs 10
c 2
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
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 the default charset used to encode strings (or string content
27
     *      like streams) returned by MailMimeParser (for e.g. the string
28
     *      returned by calling $message->getTextContent()).
29
     */
30
    const DEFAULT_CHARSET = 'UTF-8';
31
32
    /**
33
     * @var \ZBateson\MailMimeParser\Container dependency injection container
34
     */
35
    protected $di;
36
    
37
    /**
38
     * Sets up the parser.
39
     *
40
     * @param Container $di pass a Container object to use it for
41
     *        initialization.
42
     */
43 107
    public function __construct(Container $di = null)
44
    {
45 107
        if ($di === null) {
46 104
            $di = new Container();
47
        }
48 107
        $this->di = $di;
49 107
    }
50
51
    /**
52
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
53
     * object and returns it.
54
     * 
55
     * Internally, the message is first copied to a temp stream (with php://temp
56
     * which may keep it in memory or write it to disk) and its stream is used.
57
     * That way if the message is too large to hold in memory it can be written
58
     * to a temporary file if need be.
59
     * 
60
     * @param resource|string $handleOrString the resource handle to the input
61
     *        stream of the mime message, or a string containing a mime message
62
     * @return \ZBateson\MailMimeParser\Message
63
     */
64 106
    public function parse($handleOrString)
65
    {
66 106
        $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

66
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($handleOrString);
Loading history...
67 106
        $copy = Psr7\stream_for(fopen('php://temp', 'r+'));
68
69 106
        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

69
        /** @scrutinizer ignore-call */ 
70
        Psr7\copy_to_stream($stream, $copy);
Loading history...
70 106
        $copy->rewind();
71
72
        // don't close it when $stream gets destroyed
73 106
        $stream->detach();
74 106
        $parser = $this->di->newMessageParser();
75 106
        return $parser->parse($copy);
76
    }
77
}
78