Completed
Push — master ( 74fe50...0f19ea )
by Zaahid
09:04
created

MessageWriter::writeMessageTo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
crap 20
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\Message\Writer;
8
9
use ZBateson\MailMimeParser\Message;
10
use ZBateson\MailMimeParser\Message\MimePart;
11
12
/**
13
 * Writes out a message in a mail-compliant format.
14
 * 
15
 * Provides a way of writing out a ZBateson\MailMimeParser\Message object to a
16
 * resource handle.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class MessageWriter extends MimePartWriter
21
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
22
    /**
23
     * Writes out a mime boundary to the passed $handle optionally writing out a
24
     * number of empty lines before it.
25
     * 
26
     * @param resource $handle
27
     * @param string $boundary
28
     * @param int $numLinesBefore
29
     * @param bool $isEnd
30
     */
31
    protected function writeBoundary($handle, $boundary, $numLinesBefore, $isEnd)
32
    {
33
        if ($numLinesBefore > 0) {
34
            fwrite($handle, str_repeat("\r\n", $numLinesBefore));
35
        }
36
        fwrite($handle, '--');
37
        fwrite($handle, $boundary);
38
        if ($isEnd) {
39
            fwrite($handle, "--\r\n");
40
        } else {
41
            fwrite($handle, "\r\n");
42
        }
43
    }
44
45
    /**
46
     * Writes out headers and content for the passed MimePart, then loops over
47
     * its child parts calling recursiveWriteParts on each part.
48
     * 
49
     * @param MimePart $part the current part to write out
50
     * @param resource $handle the handle to write out to
51
     * @return bool true if the part had children (and ended with writing a
52
     *      boundary)
53
     */
54
    protected function recursiveWriteParts(MimePart $part, $handle)
55
    {
56
        $this->writePartHeadersTo($part, $handle);
57
        $this->writePartContentTo($part, $handle);
58
        $ended = false;
59
        $boundary = $part->getHeaderParameter('Content-Type', 'boundary');
60
        foreach ($part->getChildParts() as $i => $child) {
61
            if ($boundary !== null) {
62
                $numLines = ($i !== 0 && !$ended) ? 2 : (int) $ended;
63
                $this->writeBoundary($handle, $boundary, $numLines, false);
64
            }
65
            $ended = $this->recursiveWriteParts($child, $handle);
66
        }
67
        if ($boundary !== null) {
68
            $this->writeBoundary($handle, $boundary, ($ended) ? 1 : 2, true);
69
            return true;
70
        }
71
        return false;
72
    }
73
74
    /**
75
     * Saves the message as a MIME message to the passed resource handle.
76
     * 
77
     * @param Message $message
78
     * @param resource $handle
79
     */
80
    public function writeMessageTo(Message $message, $handle)
81
    {
82
        if ($message->isMime()) {
83
            $this->recursiveWriteParts($message, $handle);
84
        } else {
85
            $this->writePartHeadersTo($message, $handle);
86
            foreach ($message->getChildParts() as $i => $child) {
87
                if ($i !== 0) {
88
                    fwrite($handle, "\r\n\r\n");
89
                }
90
                $this->writePartContentTo($child, $handle);
91
            }
92
        }
93
    }
94
95
    /**
96
     * Returns the content part of a signed message for a signature to be
97
     * calculated on the message.
98
     * 
99
     * @param Message $message
100
     * @return string
101
     */
102
    public function getSignableBody(Message $message)
103
    {
104
        if (!$message->isMime()) {
105
            return null;
106
        }
107
        $handle = fopen('php://temp', 'r+');
108
        $ended = $this->recursiveWriteParts($message->getChild(0), $handle);
0 ignored issues
show
Bug introduced by
It seems like $message->getChild(0) can be null; however, recursiveWriteParts() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
109
        rewind($handle);
110
        $str = stream_get_contents($handle);
111
        fclose($handle);
112
        if (!$ended) {
113
            $str .= "\r\n";
114
        }
115
        return $str;
116
    }
117
}
118