Completed
Push — 1.3.x ( c0422d )
by Zaahid
15:11 queued 03:49
created

MailMimeParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 15
c 6
b 0
f 0
dl 0
loc 68
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setExtensions() 0 3 1
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
use ZBateson\MailMimeParser\Message\Extension;
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\Extension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ZBateson\MailMimeParser\Message\ExtensionsManager;
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\ExtensionsManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Parses a MIME message into a \ZBateson\MailMimeParser\Message object.
15
 *
16
 * To invoke, call parse on a MailMimeParser object.
17
 *
18
 * $handle = fopen('path/to/file.txt');
19
 * $parser = new MailMimeParser();
20
 * $parser->parse($handle);
21
 * fclose($handle);
22
 *
23
 * @author Zaahid Bateson
24
 */
25
class MailMimeParser
26
{
27
    /**
28
     * @var string the default charset used to encode strings (or string content
29
     *      like streams) returned by MailMimeParser (for e.g. the string
30
     *      returned by calling $message->getTextContent()).
31
     */
32
    const DEFAULT_CHARSET = 'UTF-8';
33
34
    /**
35
     * @var \ZBateson\MailMimeParser\Container dependency injection container
36
     */
37
    protected $di;
38
39
    /**
40
     * @var Extension[] Array of extensions.
41
     */
42
    private $extensions = [];
43
44
    /**
45
     * Sets up the parser.
46
     *
47
     * @param Container $di pass a Container object to use it for
48
     *        initialization.
49
     */
50
    public function __construct(Container $di = null)
51
    {
52
        if ($di === null) {
53
            $di = new Container();
54
        }
55
        $this->di = $di;
56
    }
57
58
    /**
59
     * Updates the array of extensions used when parsing a message.
60
     *
61
     * @param \ZBateson\MailMimeParser\Message\Extension[] $extensions
62
     */
63
    public function setExtensions(array $extensions)
64
    {
65
        $this->extensions = $extensions;
66
    }
67
68
    /**
69
     * Parses the passed stream handle into a ZBateson\MailMimeParser\Message
70
     * object and returns it.
71
     *
72
     * Internally, the message is first copied to a temp stream (with php://temp
73
     * which may keep it in memory or write it to disk) and its stream is used.
74
     * That way if the message is too large to hold in memory it can be written
75
     * to a temporary file if need be.
76
     *
77
     * @param resource|string $handleOrString the resource handle to the input
78
     *        stream of the mime message, or a string containing a mime message
79
     * @return \ZBateson\MailMimeParser\Message
80
     */
81
    public function parse($handleOrString)
82
    {
83
        $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

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

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