Skip to content

Commit

Permalink
(events) move event related code to the Event connector, resolve todo
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDPC committed Aug 21, 2024
1 parent a4c1c9a commit cf5ad41
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/Connector/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ class Event extends Base
*/
protected $results = [];

/**
* Given a MailgunEvent model, check if the message linked to the event is delivered
*/
public function isDelivered(MailgunEvent $event): bool

Check failure on line 24 in src/Connector/Event.php

View workflow job for this annotation

GitHub Actions / PHPStan (analyse) / phpstan

Parameter $event of method NSWDPC\Messaging\Mailgun\Connector\Event::isDelivered() has invalid type NSWDPC\Messaging\Mailgun\MailgunEvent.
{
return $this->isDeliveredMessage((string)$event->MessageId, (string)$event->Recipient);

Check failure on line 26 in src/Connector/Event.php

View workflow job for this annotation

GitHub Actions / PHPStan (analyse) / phpstan

Access to property $MessageId on an unknown class NSWDPC\Messaging\Mailgun\MailgunEvent.

Check failure on line 26 in src/Connector/Event.php

View workflow job for this annotation

GitHub Actions / PHPStan (analyse) / phpstan

Access to property $Recipient on an unknown class NSWDPC\Messaging\Mailgun\MailgunEvent.
}

/**
* Given a message id and recipient, check if the message linked to the event is delivered
*/
public function isDeliveredMessage(string $msgId, string $recipient): bool
{

if($msgId === '') {
throw new \UnexpectedValueException("Empty message id when checking isDelivered");
}

// poll for delivered events, MG stores them for up to 30 days
$timeframe = 'now -30 days';
$begin = Base::DateTime($timeframe);
$event_filter = MailgunEvent::DELIVERED;

Check failure on line 42 in src/Connector/Event.php

View workflow job for this annotation

GitHub Actions / PHPStan (analyse) / phpstan

Access to constant DELIVERED on an unknown class NSWDPC\Messaging\Mailgun\MailgunEvent.
$extra_params = [
'limit' => 25,
'message-id' => $msgId,
'recipient' => $recipient
];

$events = $this->pollEvents($begin, $event_filter, $extra_params);
return $events !== [];
}

/**
* @param string $begin an RFC 2822 formatted UTC datetime OR empty string for no begin datetime
* @param string $event_filter see https://documentation.mailgun.com/en/latest/api-events.html#event-types can also be a filter expression e.g "failed OR rejected"
Expand Down
29 changes: 0 additions & 29 deletions src/Connector/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use NSWDPC\Messaging\Mailgun\Jobs\SendJob;
use NSWDPC\Messaging\Mailgun\Models\MailgunEvent;
use NSWDPC\Messaging\Mailgun\Services\Logger;
use NSWDPC\Messaging\Mailgun\Connector\Event as EventConnector;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\File;
use SilverStripe\Security\Group;
Expand Down Expand Up @@ -210,34 +209,6 @@ protected function queueAndSend(string $domain, array $parameters, mixed $in): ?
}
}

/**
* Lookup all events for the submission linked to this event
*/
public function isDelivered(MailgunEvent $event, $cleanup = true): bool
{

// Query will be for this MessageId and a delivered status
if (empty($event->MessageId)) {
throw new \Exception("Tried to query a message based on MailgunEvent #{$event->ID} with no linked MessageId");
}

// poll for delivered events, MG stores them for up to 30 days
// @TODO an API sending key in the DSN cannot be used here as it can only send emails
$connector = EventConnector::create($this->dsn);
$timeframe = 'now -30 days';
$begin = Base::DateTime($timeframe);

$event_filter = MailgunEvent::DELIVERED;// no we don't want to resubmit
$extra_params = [
'limit' => 25,
'message-id' => $event->MessageId,
'recipient' => $event->Recipient,// match against the recipient of the event
];

$events = $connector->pollEvents($begin, $event_filter, $extra_params);
return $events !== [];
}

/**
* Trim < and > from message id
* @param string $message_id
Expand Down

0 comments on commit cf5ad41

Please sign in to comment.