Skip to content

Commit

Permalink
Issue #28: Improve documentation and remove obsolete dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Oct 3, 2017
1 parent a8fa7e6 commit a57b709
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
42 changes: 36 additions & 6 deletions docs/04-notifications.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
# Notifications

Notifications coming from the Poetry service will be turned into Symfony events. Host application
must register its own event listeners onto the Poetry Client library event dispatcher service:
Notifications coming from the Poetry service will be turned into Symfony events.

## Expose a notification endpoint

Host application must expose a notification endpoint and handle incoming requests to such endpoint using the Poetry
Client server.

Say that you wish to use the following URL in order to receive Poetry notifications:

```
http://my-site.europa.eu/poetry
```

Make sure that the following code is executed when a request hits the URL above:

```php
<?php
use EC\Poetry\Poetry;

$poetry = new Poetry(...);
$poetry->getServer()->handle();
```

## Register notification listeners

In order to handle incoming notifications you need to register your event listeners onto the Poetry Client library event
dispatcher service.

The core above might then become:

```php
<?php
Expand All @@ -11,6 +38,7 @@ use EC\Poetry\Events\Notifications\TranslationReceivedEvent;
$poetry = new Poetry(...);
$poetry->getEventDispatcher()
->addListener(TranslationReceivedEvent::NAME, ['MyClass', 'onTranslationReceived']);
$poetry->getServer()->handle();
```

Event listeners will receive the incoming notification as an actual notification message object:
Expand Down Expand Up @@ -42,6 +70,7 @@ $poetry->getEventDispatcher()
->addListener(TranslationReceivedEvent::NAME, function (TranslationReceived $message) {
// ...
});
$poetry->getServer()->handle();
```

Or having a class subscribe to multiple events:
Expand All @@ -51,9 +80,9 @@ Or having a class subscribe to multiple events:

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EC\Poetry\Events\Notifications\TranslationReceivedEvent;
use EC\Poetry\Events\Notifications\TranslationChangedEvent;
use EC\Poetry\Events\Notifications\StatusUpdatedEvent;
use EC\Poetry\Messages\Notifications\TranslationReceived;
use EC\Poetry\Messages\Notifications\TranslationChanged;
use EC\Poetry\Messages\Notifications\StatusUpdated;

class MySubscriber implements EventSubscriberInterface
{
Expand All @@ -64,7 +93,7 @@ class MySubscriber implements EventSubscriberInterface
{
return [
TranslationReceivedEvent::NAME => 'onTranslationReceivedEvent',
TranslationChangedEvent::NAME => 'onTranslationChangedEvent',
StatusUpdatedEvent::NAME => 'onStatusUpdatedEvent',
];
}

Expand All @@ -73,7 +102,7 @@ class MySubscriber implements EventSubscriberInterface
// ...
}

public function onTranslationChangedEvent(TranslationChanged $message)
public function onStatusUpdatedEvent(StatusUpdated $message)
{
// ...
}
Expand All @@ -89,6 +118,7 @@ use EC\Poetry\Poetry;
$poetry = new Poetry(...);
$poetry->getEventDispatcher()
->addSubscriber(new MySubscriber());
$poetry->getServer()->handle();
```


Expand Down
11 changes: 1 addition & 10 deletions src/NotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use EC\Poetry\Exceptions\Notifications\CannotAuthenticateException;
use EC\Poetry\Exceptions\ParsingException;
use EC\Poetry\Messages\Traits\ParserAwareTrait;
use EC\Poetry\Messages\Responses\Status;
use EC\Poetry\Services\Parser;
use EC\Poetry\Services\Settings;
use Symfony\Component\EventDispatcher\EventDispatcher;

Expand All @@ -30,23 +28,16 @@ class NotificationHandler
*/
private $eventDispatcher;

/**
* @var \EC\Poetry\Services\Parser
*/
private $parser;

/**
* NotificationHandler constructor.
*
* @param \EC\Poetry\Services\Settings $settings
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher
* @param \EC\Poetry\Services\Parser $parser
*/
public function __construct(Settings $settings, EventDispatcher $eventDispatcher, Parser $parser)
public function __construct(Settings $settings, EventDispatcher $eventDispatcher)
{
$this->settings = $settings;
$this->eventDispatcher = $eventDispatcher;
$this->parser = $parser;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Providers/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function register(Container $container)
};

$container['notification_handler'] = function (Container $container) {
return new NotificationHandler($container['settings'], $container['event_dispatcher'], $container['parser']);
return new NotificationHandler($container['settings'], $container['event_dispatcher']);
};

$container['soap_server'] = function (Container $container) {
Expand Down

0 comments on commit a57b709

Please sign in to comment.