Skip to content

Commit

Permalink
Refactored to use new Templates package functionality (#5)
Browse files Browse the repository at this point in the history
* Refactored to use new Templates package functionality

* Update github actions

* Fix tests

* Test for config api
  • Loading branch information
pa-cholek authored Dec 15, 2021
1 parent 8a1600c commit 9d4d344
Show file tree
Hide file tree
Showing 42 changed files with 1,127 additions and 335 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test-cc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ jobs:
runs-on: ubuntu-latest
container:
image: escolalms/php:7.4

env:
MJML_API_ID: ${{ secrets.MJML_API_ID }}
MJML_API_SECRET: ${{ secrets.MJML_API_SECRET }}
services:
mysql:
image: mysql:5.7
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ jobs:
runs-on: ubuntu-latest
container:
image: escolalms/php:8

env:
MJML_API_ID: ${{ secrets.MJML_API_ID }}
MJML_API_SECRET: ${{ secrets.MJML_API_SECRET }}
services:
mysql:
image: mysql:5.7
Expand Down Expand Up @@ -43,7 +45,9 @@ jobs:
runs-on: ubuntu-latest
container:
image: escolalms/php:7.4

env:
MJML_API_ID: ${{ secrets.MJML_API_ID }}
MJML_API_SECRET: ${{ secrets.MJML_API_SECRET }}
services:
mysql:
image: mysql:5.7
Expand Down Expand Up @@ -79,7 +83,9 @@ jobs:
runs-on: ubuntu-latest
container:
image: escolalms/php:7.4

env:
MJML_API_ID: ${{ secrets.MJML_API_ID }}
MJML_API_SECRET: ${{ secrets.MJML_API_SECRET }}
services:
postgres:
image: postgres:12
Expand Down
23 changes: 16 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
"description": "Escola Headless LMS Templates for Emails",
"type": "package",
"require": {
"php": ">=7.4 | >=8.0",
"escolalms/auth": ">=0.1.44",
"escolalms/core": ">=1.0.0",
"escolalms/notifications": ">=0.0.11",
"laravel/framework": ">=8.0"
"php": "^7.4|^8.0",
"escolalms/core": "^1.2.2",
"escolalms/templates": "^0.1.1",
"ezyang/htmlpurifier": "^4.13",
"laravel/framework": "^8.0",
"qferr/mjml-php": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": ">=5.0"
"escolalms/auth": "^0.1.54",
"escolalms/courses": "^0.3.8",
"escolalms/settings": "^0.1.2",
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^9.0"
},
"suggest": {
"escolalms/auth": "^0.1.54",
"escolalms/courses": "^0.3.8",
"escolalms/settings": "^0.1.2"
},
"license": "MIT",
"authors": [
Expand Down
17 changes: 0 additions & 17 deletions database/seeders/NotificationsSeeder.php

This file was deleted.

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

namespace EscolaLms\TemplatesEmail\Database\Seeders;

use EscolaLms\Templates\Facades\Template;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use Illuminate\Database\Seeder;

class TemplatesEmailSeeder extends Seeder
{
public function run()
{
Template::createDefaultTemplatesForChannel(EmailChannel::class);
}
}
2 changes: 0 additions & 2 deletions env/codecov/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
2 changes: 0 additions & 2 deletions env/mysql/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
2 changes: 0 additions & 2 deletions env/postgres/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Notifications\EscolaLmsNotificationsServiceProvider
- EscolaLms\Templates\EscolaLmsTemplatesServiceProvider
- EscolaLms\TemplatesEmail\EscolaLmsTemplatesEmailServiceProvider
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
<?php

namespace EscolaLms\TemplatesEmail\Enums\Email;
namespace EscolaLms\TemplatesEmail\Auth;

use EscolaLms\Auth\Models\User;
use EscolaLms\Core\Enums\BasicEnum;
use EscolaLms\Notifications\Core\NotificationVariableContract;
use Illuminate\Support\Str;
use EscolaLms\Templates\Events\EventWrapper;
use EscolaLms\TemplatesEmail\Core\EmailVariables;

abstract class AbstractAuthEmailVariables extends BasicEnum implements NotificationVariableContract
abstract class CommonAuthVariables extends EmailVariables
{
//
const VAR_USER_EMAIL = "@VarUserEmail";
const VAR_USER_FIRST_NAME = "@VarStudentFirstName";
const VAR_USER_LAST_NAME = "@VarStudentLastName";
const VAR_USER_FULL_NAME = "@VarStudentFullName";
const VAR_ACTION_LINK = "@VarActionLink";

public static function getMockVariables(): array
public static function mockedVariables(): array
{
$faker = \Faker\Factory::create();
return [
return array_merge(parent::mockedVariables(), [
self::VAR_USER_EMAIL => $faker->email,
self::VAR_USER_FIRST_NAME => $faker->firstName,
self::VAR_USER_LAST_NAME => $faker->lastName,
self::VAR_USER_FULL_NAME => $faker->name,
self::VAR_ACTION_LINK => url('/'),
];
]);
}

public static function getVariablesFromContent(?User $user = null, ?string $action_link = null): array
public static function variablesFromEvent(EventWrapper $event): array
{
$user = $event->getUser();
return [
self::VAR_USER_EMAIL => $user->email,
self::VAR_USER_FIRST_NAME => $user->firstName,
self::VAR_USER_LAST_NAME => $user->lastName,
self::VAR_USER_FULL_NAME => $user->name,
self::VAR_ACTION_LINK => $action_link,
self::VAR_ACTION_LINK => static::getActionLink($event),
];
}

public static function getRequiredVariables(): array
abstract static function getActionLink(EventWrapper $event): string;

public static function requiredVariables(): array
{
return [
self::VAR_ACTION_LINK,
];
}

public static function isValid(string $content): bool
{
return Str::containsAll($content, self::getRequiredVariables());
}

public static function getRequiredTitleVariables(): array
public static function requiredVariablesInSection(string $sectionKey): array
{
if ($sectionKey === 'content') {
return [
self::VAR_ACTION_LINK
];
}
return [];
}

public static function titleIsValid(?string $title): bool
public static function assignableClass(): ?string
{
return !empty($title);
return null;
}
}
77 changes: 77 additions & 0 deletions src/Auth/ResetPasswordVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace EscolaLms\TemplatesEmail\Auth;

use EscolaLms\Templates\Events\EventWrapper;
use Illuminate\Support\Str;
use EscolaLms\Auth\Repositories\Contracts\UserRepositoryContract;
use Illuminate\Support\Facades\Lang;

class ResetPasswordVariables extends CommonAuthVariables
{
const VAR_ACTION_LINK_EXPIRATION = "@VarActionLinkExpiration";

public static function mockedVariables(): array
{
return array_merge(parent::mockedVariables(), [
self::VAR_ACTION_LINK_EXPIRATION => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire'),
]);
}

public static function variablesFromEvent(EventWrapper $event): array
{
return array_merge(parent::variablesFromEvent($event), [
self::VAR_ACTION_LINK_EXPIRATION => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire'),
]);
}

public static function getActionLink(EventWrapper $event): string
{
$notifiable = $event->getUser();

$token = Str::random(32);

app(UserRepositoryContract::class)->update([
'password_reset_token' => Str::random(32),
], $notifiable->getKey());

try {
$url = $event->getReturnUrl();
} catch (\Throwable $th) {
$url = null;
}

if (!empty($url)) {
return $url .
'?email=' . $notifiable->getEmailForPasswordReset() .
'&token=' . $token;
}
return url(route('password.reset', [
'token' => $token,
'email' => $notifiable->getEmailForPasswordReset(),
], false));
}

public static function defaultSectionsContent(): array
{
return [
'title' => Lang::get('Reset Password Notification'),
'content' => self::wrapWithMjml(
'<mj-text>'
. '<p>'
. Lang::get('You are receiving this email because we received a password reset request for your account.')
. '</p>'
. '</mj-text>'
. '<mj-button href="' . self::VAR_ACTION_LINK . '">' . Lang::get('Reset Password') . '</mj-button>'
. '<mj-text>'
. '<p>'
. Lang::get('This password reset link will expire in :count minutes.', ['count' => self::VAR_ACTION_LINK_EXPIRATION])
. '</p>'
. '<p>'
. Lang::get('If you did not request a password reset, no further action is required.')
. '</p>'
. '</mj-text>'
)
];
}
}
45 changes: 45 additions & 0 deletions src/Auth/VerifyEmailVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace EscolaLms\TemplatesEmail\Auth;

use EscolaLms\Templates\Events\EventWrapper;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;

class VerifyEmailVariables extends CommonAuthVariables
{
public static function getActionLink(EventWrapper $event): string
{
$notifiable = $event->getUser();

return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(config('auth.verification.expire', 60)),
[
'id' => $notifiable->getKey(),
'hash' => sha1($notifiable->getEmailForVerification()),
]
);
}

public static function defaultSectionsContent(): array
{
return [
'title' => Lang::get('Verify Email Address'),
'content' => self::wrapWithMjml(
'<mj-text>'
. '<p>'
. Lang::get('Please click the button below to verify your email address.')
. '</p>'
. '</mj-text>'
. '<mj-button href="' . self::VAR_ACTION_LINK . '">' . Lang::get('Verify Email Address') . '</mj-button>'
. '<mj-text>'
. '<p>'
. Lang::get('If you did not create an account, no further action is required.')
. '</p>'
. '</mj-text>'
)
];
}
}
Loading

0 comments on commit 9d4d344

Please sign in to comment.