Skip to content

Commit

Permalink
Merge pull request #4 from EscolaLMS/feature/refactor-to-use-escolalm…
Browse files Browse the repository at this point in the history
…s-notifications-package

Refactored to use EscolaLms/Notifications package functionality
  • Loading branch information
qunabu authored Nov 30, 2021
2 parents 9a2bad8 + 0d1c706 commit 8a1600c
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 204 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Package for editing email templates (for now only ResetPassword and VerifyEmail notifications)

[![swagger](https://img.shields.io/badge/documentation-swagger-green)](https://escolalms.github.io/TemplatesEmail/)
[![codecov](https://codecov.io/gh/EscolaLMS/TemplatesEmail/branch/main/graph/badge.svg?token=O91FHNKI6R)](https://codecov.io/gh/EscolaLMS/RepTemplatesEmailorts)
[![codecov](https://codecov.io/gh/EscolaLMS/TemplatesEmail/branch/main/graph/badge.svg?token=O91FHNKI6R)](https://codecov.io/gh/EscolaLMS/TemplatesEmail)
[![phpunit](https://github.com/EscolaLMS/TemplatesEmail/actions/workflows/test.yml/badge.svg)](https://github.com/EscolaLMS/TemplatesEmail/actions/workflows/test.yml)
[![downloads](https://img.shields.io/packagist/dt/escolalms/templates-email)](https://packagist.org/packages/escolalms/templates-email)
[![downloads](https://img.shields.io/packagist/v/escolalms/templates-email)](https://packagist.org/packages/escolalms/templates-email)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"require": {
"php": ">=7.4 | >=8.0",
"escolalms/auth": ">=0.1.44",
"escolalms/core": ">=0.1.6",
"escolalms/templates": ">=0.0",
"escolalms/core": ">=1.0.0",
"escolalms/notifications": ">=0.0.11",
"laravel/framework": ">=8.0"
},
"require-dev": {
Expand Down
19 changes: 0 additions & 19 deletions database/factories/TemplateFactory.php

This file was deleted.

30 changes: 0 additions & 30 deletions database/migrations/2021_10_13_000001_update_templates_table.php

This file was deleted.

17 changes: 17 additions & 0 deletions database/seeders/NotificationsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EscolaLms\TemplatesEmail\Database\Seeders;

use EscolaLms\Notifications\Facades\EscolaLmsNotifications;
use EscolaLms\TemplatesEmail\Notifications\ResetPassword;
use EscolaLms\TemplatesEmail\Notifications\VerifyEmail;
use Illuminate\Database\Seeder;

class NotificationsSeeder extends Seeder
{
public function run()
{
EscolaLmsNotifications::createDefaultTemplates(ResetPassword::class);
EscolaLmsNotifications::createDefaultTemplates(VerifyEmail::class);
}
}
1 change: 1 addition & 0 deletions env/codecov/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ providers:
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
1 change: 1 addition & 0 deletions env/mysql/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ providers:
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
1 change: 1 addition & 0 deletions env/postgres/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ providers:
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
16 changes: 8 additions & 8 deletions src/Enums/Email/AbstractAuthEmailVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use EscolaLms\Auth\Models\User;
use EscolaLms\Core\Enums\BasicEnum;
use EscolaLms\Templates\Enum\Contracts\TemplateVariableContract;
use EscolaLms\Notifications\Core\NotificationVariableContract;
use Illuminate\Support\Str;

abstract class AbstractAuthEmailVariables extends BasicEnum implements TemplateVariableContract
abstract class AbstractAuthEmailVariables extends BasicEnum implements NotificationVariableContract
{
//
const TYPE = 'email';

//
const VAR_USER_EMAIL = "@VarUserEmail";
const VAR_USER_FIRST_NAME = "@VarStudentFirstName";
Expand Down Expand Up @@ -54,10 +51,13 @@ public static function isValid(string $content): bool
return Str::containsAll($content, self::getRequiredVariables());
}

public static function getType(): string
public static function getRequiredTitleVariables(): array
{
return self::TYPE;
return [];
}

abstract public static function getVarSet(): string;
public static function titleIsValid(?string $title): bool
{
return !empty($title);
}
}
7 changes: 0 additions & 7 deletions src/Enums/Email/ResetPasswordVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

class ResetPasswordVariables extends AbstractAuthEmailVariables
{
const VARSET = 'reset-password';

const VAR_ACTION_LINK_EXPIRATION = "@VarActionLinkExpiration";

public static function getMockVariables(): array
Expand All @@ -23,9 +21,4 @@ public static function getVariablesFromContent(?User $user = null, ?string $acti
self::VAR_ACTION_LINK_EXPIRATION => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire'),
]);
}

public static function getVarSet(): string
{
return self::VARSET;
}
}
6 changes: 0 additions & 6 deletions src/Enums/Email/VerifyEmailVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@

class VerifyEmailVariables extends AbstractAuthEmailVariables
{
const VARSET = 'verify-email';

public static function getVarSet(): string
{
return self::VARSET;
}
}
23 changes: 2 additions & 21 deletions src/EscolaLmsTemplatesEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@

namespace EscolaLms\TemplatesEmail;

use EscolaLms\Templates\Services\Contracts\VariablesServiceContract;
use EscolaLms\Templates\Services\VariablesService;
use EscolaLms\TemplatesEmail\Enums\Email\ResetPasswordVariables;
use EscolaLms\TemplatesEmail\Enums\Email\VerifyEmailVariables;
use EscolaLms\TemplatesEmail\Providers\EventServiceProvider;
use EscolaLms\TemplatesEmail\Repositories\Contracts\EmailTemplateRepositoryContract;
use EscolaLms\TemplatesEmail\Repositories\EmailTemplateRepository;
use EscolaLms\TemplatesEmail\Providers\NotificationServiceProvider;
use Illuminate\Support\ServiceProvider;

/**
* SWAGGER_VERSION
*/
class EscolaLmsTemplatesEmailServiceProvider extends ServiceProvider
{
/**
* All of the container bindings that should be registered.
*
* @var array
*/
public $bindings = [
EmailTemplateRepositoryContract::class => EmailTemplateRepository::class,
];

public function register()
{
$this->app->register(EventServiceProvider::class);
$this->app->register(NotificationServiceProvider::class);
}

public function boot()
Expand All @@ -37,12 +24,6 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}

/** @var VariablesService $variablesService */
$variablesService = resolve(VariablesServiceContract::class);

$variablesService::addToken(ResetPasswordVariables::class, ResetPasswordVariables::getType(), ResetPasswordVariables::getVarSet());
$variablesService::addToken(VerifyEmailVariables::class, VerifyEmailVariables::getType(), VerifyEmailVariables::getVarSet());
}

public function bootForConsole()
Expand Down
25 changes: 0 additions & 25 deletions src/Models/Template.php

This file was deleted.

56 changes: 39 additions & 17 deletions src/Notifications/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,55 @@
namespace EscolaLms\TemplatesEmail\Notifications;

use EscolaLms\Auth\Notifications\ResetPassword as AuthResetPassword;
use EscolaLms\Notifications\Core\NotificationContract;
use EscolaLms\Notifications\Core\Traits\NotificationDefaultImplementation;
use EscolaLms\Notifications\Facades\EscolaLmsNotifications;
use EscolaLms\TemplatesEmail\Enums\Email\ResetPasswordVariables;
use EscolaLms\TemplatesEmail\Repositories\Contracts\EmailTemplateRepositoryContract;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\HtmlString;

class ResetPassword extends AuthResetPassword
class ResetPassword extends AuthResetPassword implements NotificationContract
{
private EmailTemplateRepositoryContract $templateRepository;

public function __construct(string $token, ?string $url)
{
parent::__construct($token, $url);
$this->templateRepository = app(EmailTemplateRepositoryContract::class);
use NotificationDefaultImplementation {
toMail as toMailTrait;
}

public function toMail($notifiable)
public function toMail($notifiable): MailMessage
{
$template = $this->templateRepository->findDefaultForTypeAndSubtype(ResetPasswordVariables::getType(), ResetPasswordVariables::getVarSet());
if ($template && $template->is_valid) {
$vars = ResetPasswordVariables::getVariablesFromContent($notifiable, $this->resetUrl($notifiable));
$body = strtr($template->content, $vars);
return (new MailMessage)
->subject(Lang::get('Reset Password Notification'))
->line(new HtmlString($body));
$template = EscolaLmsNotifications::findTemplateForNotification($this, 'mail');

if ($template && $template->is_valid && $template->title_is_valid) {
return $this->toMailTrait($notifiable);
}

return parent::toMail($notifiable);
}

public function additionalDataForVariables($notifiable): array
{
return [
$this->resetUrl($notifiable),
];
}

public static function defaultTitleTemplate(): string
{
return Lang::get('Reset Password Notification');
}

public static function defaultContentTemplate(): string
{
return Lang::get('You are receiving this email because we received a password reset request for your account.')
. PHP_EOL
. ResetPasswordVariables::VAR_ACTION_LINK
. PHP_EOL
. Lang::get('This password reset link will expire in :count minutes.', ['count' => ResetPasswordVariables::VAR_ACTION_LINK_EXPIRATION])
. PHP_EOL
. Lang::get('If you did not request a password reset, no further action is required.');
}

public static function templateVariablesClass(): string
{
return ResetPasswordVariables::class;
}
}
53 changes: 37 additions & 16 deletions src/Notifications/VerifyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,54 @@

namespace EscolaLms\TemplatesEmail\Notifications;

use EscolaLms\Notifications\Core\NotificationContract;
use EscolaLms\Notifications\Core\Traits\NotificationDefaultImplementation;
use EscolaLms\Notifications\Facades\EscolaLmsNotifications;
use EscolaLms\TemplatesEmail\Enums\Email\VerifyEmailVariables;
use EscolaLms\TemplatesEmail\Repositories\Contracts\EmailTemplateRepositoryContract;
use Illuminate\Auth\Notifications\VerifyEmail as LaravelVerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\HtmlString;

class VerifyEmail extends LaravelVerifyEmail
class VerifyEmail extends LaravelVerifyEmail implements NotificationContract
{
private EmailTemplateRepositoryContract $templateRepository;

public function __construct()
{
$this->templateRepository = app(EmailTemplateRepositoryContract::class);
use NotificationDefaultImplementation {
toMail as toMailTrait;
}

public function toMail($notifiable)
public function toMail($notifiable): MailMessage
{
$template = $this->templateRepository->findDefaultForTypeAndSubtype(VerifyEmailVariables::getType(), VerifyEmailVariables::getVarSet());
if ($template && $template->is_valid) {
$vars = VerifyEmailVariables::getVariablesFromContent($notifiable, $this->verificationUrl($notifiable));
$body = strtr($template->content, $vars);
return (new MailMessage)
->subject(Lang::get('Verify Email Address'))
->line(new HtmlString($body));
$template = EscolaLmsNotifications::findTemplateForNotification($this, 'mail');

if ($template && $template->is_valid && $template->title_is_valid) {
return $this->toMailTrait($notifiable);
}

return parent::toMail($notifiable);
}

public function additionalDataForVariables($notifiable): array
{
return [
$this->verificationUrl($notifiable),
];
}

public static function defaultTitleTemplate(): string
{
return Lang::get('Verify Email Address');
}

public static function defaultContentTemplate(): string
{
return Lang::get('Please click the button below to verify your email address.')
. PHP_EOL
. VerifyEmailVariables::VAR_ACTION_LINK
. PHP_EOL
. Lang::get('If you did not create an account, no further action is required.');
}

public static function templateVariablesClass(): string
{
return VerifyEmailVariables::class;
}
}
Loading

0 comments on commit 8a1600c

Please sign in to comment.