Mailer::getMailableViewFile()   A
last analyzed

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\Mailer as MailerContract;
6
use Illuminate\Contracts\Queue\ShouldQueue;
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 Mailer extends LaravelMailer implements MailerContract
15
{
16
    public function send($view, array $data = [], $callback = null)
17
    {
18
        if ($view instanceof Mailable
19
            && ! $view instanceof ShouldQueue
20
        ) {
21
            $this->applyDebugHeaders($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 = $view = $viewContent = $viewData = null;
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$viewFile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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