Completed
Push — master ( a00b79...08e4ae )
by Freek
01:12
created

PrepareEmailHtmlAction::replacePlaceholders()   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
        if ($campaign->track_clicks) {
21
            $this->trackClicks($campaign);
22
        }
23
24
        if ($campaign->track_opens) {
25
            $this->trackOpens($campaign);
26
        }
27
28
        $this->replacePlaceholders($campaign);
29
30
31
        $campaign->save();
32
    }
33
34
    protected function trackClicks(Campaign $campaign)
35
    {
36
        $dom = new DOMDocument('1.0', 'UTF-8');
37
38
        try {
39
            $dom->loadHTML($campaign->email_html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOWARNING);
40
        } 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...
41
            throw CampaignCouldNotBeSent::invalidContent($campaign, $errorException);
42
        }
43
44
        collect($dom->getElementsByTagName('a'))
45
            ->filter(function (DOMElement $linkElement) {
46
                return Str::startsWith(
47
                    $linkElement->getAttribute('href'),
48
                    ['http://', 'https://']
49
                );
50
            })
51
            ->each(function (DOMElement $linkElement) use ($campaign) {
52
                $originalHref = $linkElement->getAttribute('href');
53
54
                $campaignLink = $campaign->links()->create([
55
                    'original_link' => $originalHref,
56
                ]);
57
58
                $linkElement->setAttribute('href', $campaignLink->url);
59
            });
60
61
        $campaign->email_html = $dom->saveHtml();
62
    }
63
64
    protected function trackOpens(Campaign $campaign)
65
    {
66
        $webBeaconUrl = action(TrackOpensController::class, '@@campaignSendUuid@@');
67
68
        $webBeaconHtml = "<img alt='beacon' src='{$webBeaconUrl}' />";
69
70
        $campaign->email_html = Str::replaceLast('</body>', $webBeaconHtml.'</body>', $campaign->email_html);
71
    }
72
73
    protected function replacePlaceholders(Campaign $campaign): void
74
    {
75
        $webviewUrl = url(action(CampaignWebviewController::class, $campaign->uuid));
76
77
        $campaign->email_html = str_replace('@@webviewUrl@@', $webviewUrl, $campaign->email_html);
78
    }
79
}
80