Completed
Pull Request — master (#11)
by
unknown
01:10
created

Laravel6Mailer::getMailableViewFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace BeyondCode\HeloLaravel;
4
5
use Illuminate\Contracts\Mail\Mailable as MailableContract;
6
use Illuminate\Contracts\Mail\Mailer as MailerContract;
7
use Illuminate\Mail\Mailable;
8
use Illuminate\Mail\Mailer as LaravelMailer;
9
use Illuminate\Support\Facades\View;
10
use ReflectionClass;
11
use Symfony\Component\VarDumper\Cloner\VarCloner;
12
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
13
14
class Laravel6Mailer extends LaravelMailer implements MailerContract
15
{
16
    public function send($view, array $data = [], $callback = null)
17
    {
18
        if ($view instanceof MailableContract) {
19
            $this->applyDebugHeaders($view);
0 ignored issues
show
Compatibility introduced by
$view of type object<Illuminate\Contracts\Mail\Mailable> is not a sub-type of object<Illuminate\Mail\Mailable>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Mail\Mailable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
20
21
            return $this->sendMailable($view);
22
        }
23
24
        parent::send($view, $data, $callback);
25
    }
26
27
    protected function applyDebugHeaders(Mailable $mailable)
28
    {
29
        $mailable->withSwiftMessage(function (\Swift_Message $swiftMessage) use ($mailable) {
30
            $viewFile = $this->getMailableViewFile($mailable);
31
            $view = $this->getMailableView($viewFile);
32
            $viewContent = $this->getMailableViewContent($view);
33
34
            $viewData = $this->getMailableViewData($mailable);
35
36
            /**
37
             * We need to base64 encode the data, as the SMTP header mime encoding could add unwanted
38
             * CLRF line breaks.
39
             */
40
            $headers = $swiftMessage->getHeaders();
41
            $headers->addTextHeader('X-HELO-View', base64_encode($viewContent));
42
            $headers->addTextHeader('X-HELO-View-File', base64_encode($viewFile));
43
            $headers->addTextHeader('X-HELO-View-Data', base64_encode($viewData));
44
        });
45
    }
46
47
    protected function getMailableProperty($mailable, string $property)
48
    {
49
        $reflection = new ReflectionClass($mailable);
50
        $property = $reflection->getProperty($property);
51
52
        $property->setAccessible(true);
53
54
        return $property->getValue($mailable);
55
    }
56
57
    protected function getMailableViewFile(Mailable $mailable)
58
    {
59
        if (!is_null($markdown = $this->getMailableProperty($mailable, 'markdown'))) {
60
            return $markdown;
61
        }
62
63
        return $this->getMailableProperty($mailable, 'view');
64
    }
65
66
    protected function getMailableView(string $viewFile)
67
    {
68
        return View::make($viewFile);
69
    }
70
71
    protected function getMailableViewContent($view)
72
    {
73
        return file_get_contents($view->getPath());
74
    }
75
76
    protected function getMailableViewData(Mailable $mailable)
77
    {
78
        $dumper = new HtmlDumper();
79
        $cloner = new VarCloner();
80
        $clonedData = $cloner->cloneVar($mailable->buildViewData());
81
82
        return $dumper->dump($clonedData, true, [
83
            'maxDepth' => 3,
84
            'maxStringLength' => 160,
85
        ]);
86
    }
87
}
88