Completed
Push — master ( 51babb...a00b79 )
by Freek
01:47
created

PrepareEmailHtmlAction::prepareHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EmailCampaigns\Actions;
4
5
use DOMElement;
6
use DOMDocument;
7
use ErrorException;
8
use Illuminate\Support\Str;
9
use Spatie\EmailCampaigns\Http\Controllers\CampaignWebviewController;
10
use Spatie\EmailCampaigns\Models\Campaign;
11
use Spatie\EmailCampaigns\Exceptions\CampaignCouldNotBeSent;
12
use Spatie\EmailCampaigns\Http\Controllers\TrackOpensController;
13
14
class PrepareEmailHtmlAction
15
{
16
    public function execute(Campaign $campaign)
17
    {
18
        $campaign->email_html = $campaign->html;
19
20
        $this->prepareHtml($campaign);
21
22
        if ($campaign->track_clicks) {
23
            $this->trackClicks($campaign);
24
        }
25
26
        if ($campaign->track_opens) {
27
            $this->trackOpens($campaign);
28
        }
29
30
        $campaign->save();
31
    }
32
33
    protected function trackClicks(Campaign $campaign)
34
    {
35
        $dom = new DOMDocument('1.0', 'UTF-8');
36
37
        try {
38
            $dom->loadHTML($campaign->email_html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOWARNING);
39
        } catch (ErrorException $errorException) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
40
            throw CampaignCouldNotBeSent::invalidContent($campaign, $errorException);
41
        }
42
43
        collect($dom->getElementsByTagName('a'))
44
            ->filter(function (DOMElement $linkElement) {
45
                return Str::startsWith(
46
                    $linkElement->getAttribute('href'),
47
                    ['http://', 'https://']
48
                );
49
            })
50
            ->each(function (DOMElement $linkElement) use ($campaign) {
51
                $originalHref = $linkElement->getAttribute('href');
52
53
                $campaignLink = $campaign->links()->create([
54
                    'original_link' => $originalHref,
55
                ]);
56
57
                $linkElement->setAttribute('href', $campaignLink->url);
58
            });
59
60
        $campaign->email_html = $dom->saveHtml();
61
    }
62
63
    protected function trackOpens(Campaign $campaign)
64
    {
65
        $webBeaconUrl = action(TrackOpensController::class, '@@campaignSendUuid@@');
66
67
        $webBeaconHtml = "<img alt='beacon' src='{$webBeaconUrl}' />";
68
69
        $campaign->email_html = Str::replaceLast('</body>', $webBeaconHtml.'</body>', $campaign->email_html);
70
    }
71
72
    protected function prepareHtml(Campaign $campaign): void
73
    {
74
        $webviewUrl = url(action(CampaignWebviewController::class, $campaign->uuid));
75
76
        $campaign->email_html = str_replace('@@webviewUrl@@', $webviewUrl, $campaign->email_html);
77
    }
78
}
79