Passed
Push — 1.3.x ( c5ab82...057911 )
by Zaahid
03:06
created

MailMimeParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 1
rs 10
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
     * @var Extension[] Array of extensions.
39
     */
40
    private $extensions = [];
41
42
    /**
43
     * Sets up the parser.
44
     *
45
     * @param Container $di pass a Container object to use it for
46
     *        initialization.
47
     */
48 4
    public function __construct(Container $di = null)
49
    {
50 4
        if ($di === null) {
51
            $di = new Container();
52
        }
53 4
        $this->di = $di;
54 4
    }
55
56
    /**
57
     * Updates the array of extensions used when parsing a message.
58
     *
59
     * @param \ZBateson\MailMimeParser\Message\Extension[] $extensions
60
     */
61 1
    public function setExtensions(array $extensions)
62
    {
63 1
        $this->extensions = $extensions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $extensions of type ZBateson\MailMimeParser\Message\Extension[] is incompatible with the declared type ZBateson\MailMimeParser\Extension[] of property $extensions.

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...
64 1
    }
65
66
    /**
67
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
68
     * object and returns it.
69
     *
70
     * Internally, the message is first copied to a temp stream (with php://temp
71
     * which may keep it in memory or write it to disk) and its stream is used.
72
     * That way if the message is too large to hold in memory it can be written
73
     * to a temporary file if need be.
74
     *
75
     * @param resource|string $handleOrString the resource handle to the input
76
     *        stream of the mime message, or a string containing a mime message
77
     * @return \ZBateson\MailMimeParser\Message
78
     */
79 3
    public function parse($handleOrString)
80
    {
81 3
        $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

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

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