Usage

This page describes how the ContentSync plugin works and how to use it.

Naviga Web provides a cloud hosted service called "Content Sync". The service will keep track of events from one specific Open Content and POST specified events to a configured list of sites.

The plugin exposes the endpoint where these events can be posted. The events will then be validated and dispatched through PSR-14 standards to any listeners that might be interested.

We will refer to contenttype in this documentation. This is a property that every object has in Open Content, and it describes the type of content, i.e. Article, Concept and List.

Registering a Listener

You register your listener to \Everyware\Plugin\ContentSync\ContentEvents::listen().

Description

ContentEvents::listen(string $contentType, ContentEventListener $listener, int $priority = 10): void

Parameters

$contentType (string) - Specifies which contenttype you are interested in.

$listener (ContentEventListener) - The listener that will handle the "Event" needs to implement the \Everyware\Plugin\ContentSync\Contracts\ContentEventListener interface.

$priority (int) - The order in which the listeners are triggered. Lower numbers correspond with earlier trigger, and listeners with the same priority are triggered in the order in which they were added. Default value: 10

Prioritize your actions

While registering your listener you have the option to add a priority for when your listener should be triggered. By default, the listeners are triggered in the order they were added with a start priority of 10.

Sometimes you'll want to make sure that your listener is triggered at the very beginning. You can then give your listener a priority of 1-9. Then your listener will be triggered before all other events with a lower priority.

There is no certain way to make sure that your listener is triggered last but the rule of thumb is: the higher the priority, the more likely it is that it is to be triggered last.

The order in which the listener is added does still apply if multiple listeners are added with the same priority.

Events

There are three types off supported Events: ADD, DELETE and UPDATE. Every event relates to one object from Open Content with a specific contenttype.

Events will be dispatched as \Everyware\Plugin\ContentSync\Event objects that implement the \Everyware\Plugin\ContentSync\Contracts\ContentEvent interface.

The Everyware\Plugin\ContentSync\Contracts\ContentEvent interface provides methods for extracting useful data from the event.

interface ContentEvent
{
    public function getContent(): array;

    public function getContentType(): string;

    public function getContentUuid(): string;

    public function getContentVersion(): int;

    public function getId(): int;

    public function getType(): string;

    public function isAdd(): bool;

    public function isDelete(): bool;

    public function isUpdate(): bool;

    public function raw(): string;
}

Examples

Let's say you wanted to listen for Article events.

Your class might look like this:

use Everyware\Plugin\ContentSync\Contracts\ContentEvent;
use Everyware\Plugin\ContentSync\Contracts\ContentEventListener;

class MyArticleEventListener implements ContentEventListener
{
    public function handle(ContentEvent $event): void
    {
        // Handle article event
    }
}

Then you'd register it like this:

use Everyware\Plugin\ContentSync\ContentEvents;

ContentEvents::listen('Article', new MyArticleEventListener());

Let's say that you'd like to separate how you handle the different types of events.

Well you could do something like this:

use Everyware\Plugin\ContentSync\Contracts\ContentEvent;
use Everyware\Plugin\ContentSync\Contracts\ContentEventListener;

class MyArticleEventListener implements ContentEventListener
{
    public function onAdd(ContentEvent $event): void
    {
        // Handle ADD events
    }

    public function onDelete(ContentEvent $event): void
    {
        // Handle DELETE events
    }

    public function onUpdate(ContentEvent $event): void
    {
        // Handle UPDATE events
    }
    
    public function handle(ContentEvent $event): void
    {
        if ($event->isAdd()) {
            $this->onAdd($event);
        } elseif ($event->isDelete()) {
            $this->onDelete($event);
        } elseif ($event->isUpdate()) {
            $this->onUpdate($event);
        }
    }
}

Last updated