Skip to content

Commit

Permalink
Add capability to listen to regular donor events
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneskod committed Aug 16, 2019
1 parent cb94137 commit ca17e6c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ subject: Only sent if there is a commment in donor

### Triggering templates

Templates are stored in the `templates` dir postfixed with the donor state name
that should trigger message creation. For example:
Templates are stored in the `templates` dir postfixed with the donor event or
state name that should trigger message creation. For example:

* `foo_template.MANDATE_APPROVED`
* `bar_template.INACTIVE`
* `baz_template.PAUSED`
* `foo_template.MANDATE_SENT`
* `bar_template.REVOCATION_SENT`
* `baz_template.DONOR_POSTAL_ADDRESS_UPDATED`

## The mail queue

Expand Down
4 changes: 2 additions & 2 deletions src/DependenciesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function register(Container $container)
return new MailerStatusConsole($c[QueueInterface::CLASS]);
};

$container[DonorStateListener::CLASS] = function ($c) {
return new DonorStateListener(
$container[DonorEventListener::CLASS] = function ($c) {
return new DonorEventListener(
$c[TemplateReader::CLASS],
$c[MessageFactory::CLASS],
$c[QueueInterface::CLASS],
Expand Down
19 changes: 11 additions & 8 deletions src/DonorStateListener.php → src/DonorEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace byrokrat\giroappmailerplugin;

use byrokrat\giroapp\Event\DonorEvent;
use byrokrat\giroapp\Event\DonorStateUpdated;
use byrokrat\giroapp\Event\Listener\ListenerInterface;
use Psr\Log\LoggerInterface;
use byrokrat\giroapp\Utils\ClassIdExtractor;
use Genkgo\Mail\Queue\QueueInterface;
use Psr\Log\LoggerInterface;

final class DonorStateListener implements ListenerInterface
final class DonorEventListener implements ListenerInterface
{
/** @var TemplateReader */
private $templateReader;
Expand All @@ -35,14 +37,16 @@ public function __construct(
$this->logger = $logger;
}

public function __invoke(DonorStateUpdated $event)
public function __invoke(DonorEvent $event)
{
$donor = $event->getDonor();

$stateId = $event->getNewState()->getStateId();
$templateId = $event instanceof DonorStateUpdated
? $event->getNewState()->getStateId()
: (string)new ClassIdExtractor($event);

foreach ($this->templateReader->readTemplates($stateId) as $tmpl) {
$message = $this->messageFactory->createMessage($tmpl, $donor);
foreach ($this->templateReader->readTemplates($templateId) as $template) {
$message = $this->messageFactory->createMessage($template, $donor);

if (!$message) {
continue;
Expand All @@ -57,8 +61,7 @@ public function __invoke(DonorStateUpdated $event)
"Queued message '%s' to '%s'",
$headers->readHeader('subject'),
$headers->readHeader('to')
),
['new_state' => $stateId]
)
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/TemplateReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function __construct(Finder $finder)
$this->finder = $finder;
}

/**
* @return iterable & string[]
*/
public function readTemplates(string $postfix): iterable
{
foreach ($this->finder as $file) {
Expand Down
2 changes: 1 addition & 1 deletion stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function loadPlugin(EnvironmentInterface $env): void
$env->registerConsoleCommand($container[MailerSendConsole::CLASS]);
$env->registerConsoleCommand($container[MailerStatusConsole::CLASS]);

$env->registerListener($container[DonorStateListener::CLASS]);
$env->registerListener($container[DonorEventListener::CLASS]);
}
};

Expand Down

0 comments on commit ca17e6c

Please sign in to comment.