Skip to content

Commit

Permalink
Merge pull request #114 from mettle/feature/message-subject-tags
Browse files Browse the repository at this point in the history
Feature/message subject tags
  • Loading branch information
JonoB authored Mar 8, 2021
2 parents 60f107f + 2d3dbd0 commit 6c3eb2f
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 27 deletions.
2 changes: 1 addition & 1 deletion resources/views/messages/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function resizeTextArea(element) {
</tr>
<tr>
<td><b>{{ __('Subject') }}:</b></td>
<td>{{ $message->subject }}</td>
<td>{{ $subject }}</td>
</tr>
<tr>
<td><b>{{ __('From') }}:</b></td>
Expand Down
21 changes: 14 additions & 7 deletions src/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Sendportal\Base\Facades\Sendportal;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Repositories\Messages\MessageTenantRepositoryInterface;
use Sendportal\Base\Services\Content\MergeContent;
use Sendportal\Base\Services\Content\MergeContentService;
use Sendportal\Base\Services\Content\MergeSubjectService;
use Sendportal\Base\Services\Messages\DispatchMessage;

class MessagesController extends Controller
Expand All @@ -21,17 +22,22 @@ class MessagesController extends Controller
/** @var DispatchMessage */
protected $dispatchMessage;

/** @var MergeContent */
protected $mergeContent;
/** @var MergeContentService */
protected $mergeContentService;

/** @var MergeSubjectService */
protected $mergeSubjectService;

public function __construct(
MessageTenantRepositoryInterface $messageRepo,
DispatchMessage $dispatchMessage,
MergeContent $mergeContent
MergeContentService $mergeContentService,
MergeSubjectService $mergeSubjectService
) {
$this->messageRepo = $messageRepo;
$this->dispatchMessage = $dispatchMessage;
$this->mergeContent = $mergeContent;
$this->mergeContentService = $mergeContentService;
$this->mergeSubjectService = $mergeSubjectService;
}

/**
Expand Down Expand Up @@ -82,9 +88,10 @@ public function show(int $messageId): View
{
$message = $this->messageRepo->find(Sendportal::currentWorkspaceId(), $messageId);

$content = $this->mergeContent->handle($message);
$content = $this->mergeContentService->handle($message);
$subject = $this->mergeSubjectService->handle($message);

return view('sendportal::messages.show', compact('content', 'message'));
return view('sendportal::messages.show', compact('content', 'message', 'subject'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/Webview/WebviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use Illuminate\Contracts\View\View as ViewContract;
use Sendportal\Base\Http\Controllers\Controller;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Services\Content\MergeContent;
use Sendportal\Base\Services\Content\MergeContentService;

class WebviewController extends Controller
{
/** @var MergeContent */
/** @var MergeContentService */
private $merger;

public function __construct(MergeContent $merger)
public function __construct(MergeContentService $merger)
{
$this->merger = $merger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Sendportal\Pro\Repositories\AutomationScheduleRepository;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

class MergeContent
class MergeContentService
{
use NormalizeTags;

Expand Down Expand Up @@ -117,7 +117,6 @@ protected function compileTags(string $content): string
'webview_url'
];

// NOTE: regex doesn't seem to work here - I think it may be due to all the tags and inverted commas in html?
foreach ($tags as $tag) {
$content = $this->normalizeTags($content, $tag);
}
Expand Down
52 changes: 52 additions & 0 deletions src/Services/Content/MergeSubjectService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Sendportal\Base\Services\Content;

use Sendportal\Base\Models\Message;
use Sendportal\Base\Traits\NormalizeTags;

class MergeSubjectService
{
use NormalizeTags;

public function handle(Message $message): string
{
$messageSubject = $this->compileTags($message);

return $this->mergeSubscriberTags($messageSubject, $message);
}

protected function compileTags(Message $message): string
{
$tags = [
'email',
'first_name',
'last_name',
];

$messageSubject = $message->subject;

foreach ($tags as $tag) {
$messageSubject = $this->normalizeTags($messageSubject, $tag);
}

return $messageSubject;
}

protected function mergeSubscriberTags(string $messageSubject, Message $message): string
{
$tags = [
'email' => $message->recipient_email,
'first_name' => $message->subscriber ? $message->subscriber->first_name : '',
'last_name' => $message->subscriber ? $message->subscriber->last_name : '',
];

foreach ($tags as $key => $replace) {
$messageSubject = str_ireplace('{{' . $key . '}}', $replace, $messageSubject);
}

return $messageSubject;
}
}
37 changes: 29 additions & 8 deletions src/Services/Messages/DispatchMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Sendportal\Base\Models\CampaignStatus;
use Sendportal\Base\Models\EmailService;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Services\Content\MergeContent;
use Sendportal\Base\Services\Content\MergeContentService;
use Sendportal\Base\Services\Content\MergeSubjectService;

class DispatchMessage
{
Expand All @@ -20,21 +21,26 @@ class DispatchMessage
/** @var RelayMessage */
protected $relayMessage;

/** @var MergeContent */
protected $mergeContent;
/** @var MergeContentService */
protected $mergeContentService;

/** @var MergeSubjectService */
protected $mergeSubjectService;

/** @var MarkAsSent */
protected $markAsSent;

public function __construct(
MergeContent $mergeContent,
MergeContentService $mergeContentService,
MergeSubjectService $mergeSubjectService,
ResolveEmailService $resolveEmailService,
RelayMessage $relayMessage,
MarkAsSent $markAsSent
) {
$this->mergeContentService = $mergeContentService;
$this->mergeSubjectService = $mergeSubjectService;
$this->resolveEmailService = $resolveEmailService;
$this->relayMessage = $relayMessage;
$this->mergeContent = $mergeContent;
$this->markAsSent = $markAsSent;
}

Expand All @@ -49,6 +55,8 @@ public function handle(Message $message): ?string
return null;
}

$message = $this->mergeSubject($message);

$mergedContent = $this->getMergedContent($message);

$emailService = $this->getEmailService($message);
Expand All @@ -62,12 +70,25 @@ public function handle(Message $message): ?string
return $messageId;
}

/**
* The message's subject is merged and persisted to the database
* so that we have a permanent record of the merged tags at the
* time of dispatch.
*/
protected function mergeSubject(Message $message): Message
{
$message->subject = $this->mergeSubjectService->handle($message);
$message->save();

return $message;
}

/**
* @throws Exception
*/
protected function getMergedContent(Message $message): string
{
return $this->mergeContent->handle($message);
return $this->mergeContentService->handle($message);
}

/**
Expand Down Expand Up @@ -108,13 +129,13 @@ protected function isValidMessage(Message $message): bool
return false;
}

if (! $message->isCampaign()) {
if (!$message->isCampaign()) {
return true;
}

$campaign = Campaign::find($message->source_id);

if (! $campaign) {
if (!$campaign) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Services/Messages/DispatchTestMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sendportal\Base\Models\EmailService;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Repositories\Campaigns\CampaignTenantRepositoryInterface;
use Sendportal\Base\Services\Content\MergeContent;
use Sendportal\Base\Services\Content\MergeContentService;

class DispatchTestMessage
{
Expand All @@ -20,15 +20,15 @@ class DispatchTestMessage
/** @var RelayMessage */
protected $relayMessage;

/** @var MergeContent */
/** @var MergeContentService */
protected $mergeContent;

/** @var CampaignTenantRepositoryInterface */
protected $campaignTenant;

public function __construct(
CampaignTenantRepositoryInterface $campaignTenant,
MergeContent $mergeContent,
MergeContentService $mergeContent,
ResolveEmailService $resolveEmailService,
RelayMessage $relayMessage
) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Content/MergeContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sendportal\Base\Models\Campaign;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Models\Template;
use Sendportal\Base\Services\Content\MergeContent;
use Sendportal\Base\Services\Content\MergeContentService;
use Tests\TestCase;

class MergeContentTest extends TestCase
Expand Down Expand Up @@ -145,8 +145,8 @@ private function generateCampaignMessage(?string $campaignContent, ?string $temp

private function mergeContent(Message $message): string
{
/** @var MergeContent $mergeContent */
$mergeContent = app(MergeContent::class);
/** @var MergeContentService $mergeContent */
$mergeContent = app(MergeContentService::class);

return $mergeContent->handle($message);
}
Expand Down
Loading

0 comments on commit 6c3eb2f

Please sign in to comment.