escolalms/templates

Escola Headless LMS templates (for eg Certificates)

Installs: 13 798

Dependents: 8

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:package

0.2.37 2023-04-14 14:44 UTC

README

swagger codecov phpunit downloads downloads downloads

Purpose

General purpose of this package is to store various templates in database and assigning them to Events so that content based on these templates is automatically generated and/or sent to users.

Usage

Each template is defined by

  • channel: class defining how the template is handled
  • event: event to which the template is assigned

For every channel & event pair a single Variables definition is registered, which contains tokens that can be used in the template and replaced with values based on the data from the Event.

Analysing these three example files in tests:

  • TestChannel.php
  • TestVariables.php
  • TestEventWithGetters.php

and looking at the Template facade is simplest way to understand how this package works.

Facade

There is a Template facade declared, which is used to register Event-Channel-Variable sets and can be used in testing (as it can be replaced with a fake using Template::fake()).

To register Event-Channel-Variable set, Template::register($eventClass, $channelClass, $variableClass) must be called, where:

  • $eventClass can be any class that is dispatched as an event in any EscolaLms package
  • $channelClass must be a class implementing TemplateChannelContract interface declared in this package
  • $variableClass must be a class implementing TemplateVariableContract interface declared in this package

Channels & variables

This package has no Channels or Variables defined, everything should be created in separate packages.

Event Wrapper

EscolaLms\Templates\Events\EventWrapper is a class that wraps any event before trying to generate notification for it (and feed data from it to Template). This class contains methods for extracting necessary data from Event if that data is not available directly (for example User to which notification is being sent, and any other data that is required by registered Template).

Endpoints

All the endpoints are defined in swagger

Tests

Run ./vendor/bin/phpunit --filter 'EscolaLms\\Templates\\Tests' to run tests. See tests folder as it's quite good staring point as documentation appendix.

Test details: codecov phpunit

Listeners

  • EscolaLms\Templates\Listeners\TemplateEventListener this listeners listens to all EscolaLms namespace events, checks if event is registered for any Template, and passes the event wrapped in EventWrapper to processing (so if event is registered and has corresponding Template, notification using that Template will be sent)

Permissions

Permissions are defined in Enum and seeded in Seeder.