Passed
Push — master ( 00f43d...a0d391 )
by Malte
02:56
created

MessageMask::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
* File: MessageMask.php
4
* Category: Mask
5
* Author: M.Goldenbaum
6
* Created: 14.03.19 20:49
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\IMAP\Support\Masks;
14
15
use Webklex\IMAP\Attachment;
16
use Webklex\IMAP\Message;
17
18
/**
19
 * Class MessageMask
20
 *
21
 * @package Webklex\IMAP\Support\Masks
22
 */
23
class MessageMask extends Mask {
24
25
    /** @var Message $parent */
26
    protected $parent;
27
28
    /**
29
     * Get the message html body
30
     * @return null
31
     */
32
    public function getHtmlBody(){
33
        $bodies = $this->parent->getBodies();
34
        if (!isset($bodies['html'])) {
35
            return null;
36
        }
37
38
        return $bodies['html']->content;
39
    }
40
41
    /**
42
     * Get the Message html body filtered by an optional callback
43
     *
44
     * @return string|null
45
     */
46
    public function getCustomHTMLBody($callback = false) {
47
        $body = $this->getHtmlBody();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $body is correct as $this->getHtmlBody() targeting Webklex\IMAP\Support\Mas...sageMask::getHtmlBody() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
        if($body === null) return null;
0 ignored issues
show
introduced by
The condition $body === null is always true.
Loading history...
49
50
        if ($callback !== false) {
51
            $aAttachment = $this->parent->getAttachments();
52
            $aAttachment->each(function($oAttachment) use(&$body, $callback) {
53
                /** @var Attachment $oAttachment */
54
                if(is_callable($callback)) {
55
                    $body = $callback($body, $oAttachment);
56
                }elseif(is_string($callback)) {
57
                    call_user_func($callback, [$body, $oAttachment]);
58
                }
59
            });
60
        }
61
62
        return $body;
63
    }
64
65
    /**
66
     * Get the Message html body with embedded base64 images
67
     * the resulting $body.
68
     *
69
     * @return string|null
70
     */
71
    public function getHTMLBodyWithEmbeddedBase64Images() {
72
        return $this->getCustomHTMLBody(function($body, $oAttachment){
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCustomHTMLBody...ion(...) { /* ... */ }) targeting Webklex\IMAP\Support\Mas...sk::getCustomHTMLBody() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
            /** @var \Webklex\IMAP\Attachment $oAttachment */
74
            if ($oAttachment->id && $oAttachment->getImgSrc() != null) {
0 ignored issues
show
Deprecated Code introduced by
The function Webklex\IMAP\Attachment::getImgSrc() has been deprecated: 1.4.0:2.0.0 No longer needed. Use AttachmentMask::getImageSrc() instead ( Ignorable by Annotation )

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

74
            if ($oAttachment->id && /** @scrutinizer ignore-deprecated */ $oAttachment->getImgSrc() != null) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75
                $body = str_replace('cid:'.$oAttachment->id, $oAttachment->getImgSrc(), $body);
0 ignored issues
show
Deprecated Code introduced by
The function Webklex\IMAP\Attachment::getImgSrc() has been deprecated: 1.4.0:2.0.0 No longer needed. Use AttachmentMask::getImageSrc() instead ( Ignorable by Annotation )

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

75
                $body = str_replace('cid:'.$oAttachment->id, /** @scrutinizer ignore-deprecated */ $oAttachment->getImgSrc(), $body);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
            }
77
78
            return $body;
79
        });
80
    }
81
82
    /**
83
     * Get the Message html body with embedded image urls
84
     * the resulting $body.
85
     *
86
     * @param string $route_name
87
     * @param array $params
88
     *
89
     * @return null|string
90
     */
91
    public function getHTMLBodyWithEmbeddedUrlImages($route_name, $params = []) {
92
        return $this->getCustomHTMLBody(function($body, $oAttachment) use($route_name, $params){
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCustomHTMLBody...ion(...) { /* ... */ }) targeting Webklex\IMAP\Support\Mas...sk::getCustomHTMLBody() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
            /** @var \Webklex\IMAP\Attachment $oAttachment */
94
            if ($oAttachment->id && $oAttachment->getImgSrc() != null) {
0 ignored issues
show
Deprecated Code introduced by
The function Webklex\IMAP\Attachment::getImgSrc() has been deprecated: 1.4.0:2.0.0 No longer needed. Use AttachmentMask::getImageSrc() instead ( Ignorable by Annotation )

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

94
            if ($oAttachment->id && /** @scrutinizer ignore-deprecated */ $oAttachment->getImgSrc() != null) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
95
                $oMessage = $oAttachment->getMessage();
96
97
                $image_url = route($route_name, array_merge([
98
                    'muid' => urlencode($oMessage->uid),
99
                    'mid' => urlencode($oMessage->message_id),
100
                    'mti' => urlencode($oMessage->date->timestamp),
101
                    'aid' => urlencode($oAttachment->id)
102
                ], $params));
103
104
                $body = str_replace('cid:'.$oAttachment->id, $image_url, $body);
105
            }
106
107
            return $body;
108
        });
109
    }
110
111
    /**
112
     * Alias method for better code reading
113
     *
114
     * @return Message
115
     */
116
    public function getMessage(){
117
        return $this->parent;
118
    }
119
120
}