Test Failed
Push — 1.0.0 ( 30b11b...d49389 )
by Zaahid
02:27
created

PrivacyHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A overwrite8bitContentEncoding() 0 13 4
A setSignature() 0 12 2
A ensureHtmlPartFirstForSignedMessage() 0 10 5
A __construct() 0 10 1
A setMessageAsMultipartSigned() 0 11 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\Message\Helper;
8
9
use ZBateson\MailMimeParser\Message;
10
use ZBateson\MailMimeParser\Message\Part\Factory\MimePartFactory;
11
use ZBateson\MailMimeParser\Message\Part\Factory\PartBuilderFactory;
12
use ZBateson\MailMimeParser\Message\Part\Factory\UUEncodedPartFactory;
13
use ZBateson\MailMimeParser\Message\PartFilter;
14
15
/**
16
 * Provides routines to set or retrieve the signature part of a signed message.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class PrivacyHelper extends AbstractHelper
21
{
22
    /**
23
     * @var GenericHelper a GenericHelper instance
24
     */
25
    private $genericHelper;
26
27
    /**
28
     * @var MultipartHelper a MultipartHelper instance
29
     */
30
    private $multipartHelper;
31
32
    /**
33
     * @param MimePartFactory $mimePartFactory
34
     * @param UUEncodedPartFactory $uuEncodedPartFactory
35
     * @param PartBuilderFactory $partBuilderFactory
36
     * @param GenericHelper $genericHelper
37
     * @param MultipartHelper $multipartHelper
38
     */
39
    public function __construct(
40
        MimePartFactory $mimePartFactory,
41
        UUEncodedPartFactory $uuEncodedPartFactory,
42
        PartBuilderFactory $partBuilderFactory,
43
        GenericHelper $genericHelper,
44
        MultipartHelper $multipartHelper
45
    ) {
46
        parent::__construct($mimePartFactory, $uuEncodedPartFactory, $partBuilderFactory);
47
        $this->genericHelper = $genericHelper;
48
        $this->multipartHelper = $multipartHelper;
49
    }
50
51
    /**
52
     * The passed message is set as multipart/signed, and a new part is created
53
     * below it with content headers, content and children copied from the
54
     * message.
55
     *
56
     * @param Message $message
57
     * @param string $micalg
58
     * @param string $protocol
59
     */
60
    public function setMessageAsMultipartSigned(Message $message, $micalg, $protocol)
61
    {
62
        $this->multipartHelper->enforceMime($message);
63
        $messagePart = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart();
64
        $this->genericHelper->movePartContentAndChildren($message, $messagePart);
65
        $message->addChild($messagePart);
66
67
        $boundary = $this->multipartHelper->getUniqueBoundary('multipart/signed');
68
        $message->setRawHeader(
69
            'Content-Type',
70
            "multipart/signed;\r\n\tboundary=\"$boundary\";\r\n\tmicalg=\"$micalg\"; protocol=\"$protocol\""
71
        );
72
    }
73
74
    /**
75
     * Sets the signature of the message to $body, creating a signature part if
76
     * one doesn't exist.
77
     *
78
     * @param Message $message
79
     * @param string $body
80
     */
81
    public function setSignature(Message $message, $body)
82
    {
83
        $signedPart = $message->getSignaturePart();
84
        if ($signedPart === null) {
85
            $signedPart = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart();
86
            $message->addChild($signedPart);
87
        }
88
        $signedPart->setRawHeader(
0 ignored issues
show
Bug introduced by
The method setRawHeader() does not exist on ZBateson\MailMimeParser\Message\Part\MessagePart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Message\Part\MessagePart such as ZBateson\MailMimeParser\...e\Part\ParentHeaderPart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $signedPart->/** @scrutinizer ignore-call */ 
89
                     setRawHeader(
Loading history...
89
            'Content-Type',
90
            $message->getHeaderParameter('Content-Type', 'protocol')
91
        );
92
        $signedPart->setContent($body);
93
    }
94
95
    /**
96
     * Loops over parts of the message and sets the content-transfer-encoding
97
     * header to quoted-printable for text/* mime parts, and to base64
98
     * otherwise for parts that are '8bit' encoded.
99
     *
100
     * Used for multipart/signed messages which doesn't support 8bit transfer
101
     * encodings.
102
     *
103
     * @param Message $message
104
     */
105
    public function overwrite8bitContentEncoding(Message $message)
106
    {
107
        $parts = $message->getAllParts(new PartFilter([
108
            'headers' => [ PartFilter::FILTER_INCLUDE => [
109
                'Content-Transfer-Encoding' => '8bit'
110
            ] ]
111
        ]));
112
        foreach ($parts as $part) {
113
            $contentType = strtolower($part->getHeaderValue('Content-Type', 'text/plain'));
0 ignored issues
show
Bug introduced by
The method getHeaderValue() does not exist on ZBateson\MailMimeParser\Message\Part\MessagePart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Message\Part\MessagePart such as ZBateson\MailMimeParser\...e\Part\ParentHeaderPart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            $contentType = strtolower($part->/** @scrutinizer ignore-call */ getHeaderValue('Content-Type', 'text/plain'));
Loading history...
114
            if ($contentType === 'text/plain' || $contentType === 'text/html') {
115
                $part->setRawHeader('Content-Transfer-Encoding', 'quoted-printable');
116
            } else {
117
                $part->setRawHeader('Content-Transfer-Encoding', 'base64');
118
            }
119
        }
120
    }
121
122
    /**
123
     * Ensures a non-text part comes first in a signed multipart/alternative
124
     * message as some clients seem to prefer the first content part if the
125
     * client doesn't understand multipart/signed.
126
     *
127
     * @param Message $message
128
     */
129
    public function ensureHtmlPartFirstForSignedMessage(Message $message)
130
    {
131
        $alt = $message->getPartByMimeType('multipart/alternative');
132
        if ($alt !== null && $alt instanceof ParentPart) {
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\Helper\ParentPart 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...
133
            $cont = $this->multipartHelper->getContentPartContainerFromAlternative('text/html', $alt);
134
            $children = $alt->getChildParts();
0 ignored issues
show
Bug introduced by
The method getChildParts() does not exist on ZBateson\MailMimeParser\Message\Part\MessagePart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Message\Part\MessagePart such as ZBateson\MailMimeParser\Message\Part\ParentPart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
            /** @scrutinizer ignore-call */ 
135
            $children = $alt->getChildParts();
Loading history...
135
            $pos = array_search($cont, $children, true);
136
            if ($pos !== false && $pos !== 0) {
137
                $alt->removePart($children[0]);
0 ignored issues
show
Bug introduced by
The method removePart() does not exist on ZBateson\MailMimeParser\Message\Part\MessagePart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Message\Part\MessagePart such as ZBateson\MailMimeParser\Message\Part\ParentPart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
                $alt->/** @scrutinizer ignore-call */ 
138
                      removePart($children[0]);
Loading history...
138
                $alt->addChild($children[0]);
0 ignored issues
show
Bug introduced by
The method addChild() does not exist on ZBateson\MailMimeParser\Message\Part\MessagePart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Message\Part\MessagePart such as ZBateson\MailMimeParser\Message\Part\ParentPart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
                $alt->/** @scrutinizer ignore-call */ 
139
                      addChild($children[0]);
Loading history...
139
            }
140
        }
141
    }
142
}
143