Skip to content

Commit

Permalink
Start loop on event objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jeckel committed Oct 13, 2023
1 parent 3b69a22 commit 1d11236
Show file tree
Hide file tree
Showing 9 changed files with 682 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Exception;

use LogicException as LogicExceptionCore;

class LogicException extends LogicExceptionCore implements MauticParserException {}
12 changes: 12 additions & 0 deletions src/Exception/MauticParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 13/10/2023
*/

namespace JeckelLab\MauticWebhookParser\Exception;

use Throwable;

interface MauticParserException extends Throwable {}
4 changes: 1 addition & 3 deletions src/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@

namespace JeckelLab\MauticWebhookParser\Model;

class Client
{
}
class Client {}
12 changes: 12 additions & 0 deletions src/Model/Lead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Model;

class Lead {}
23 changes: 23 additions & 0 deletions src/Model/MauticEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Model;

use DateTimeImmutable;
use JeckelLab\MauticWebhookParser\ValueObject\MauticEventType;

readonly class MauticEvent
{
public function __construct(
public MauticEventType $eventType,
public Client $client,
public Lead $lead,
public DateTimeImmutable $timestamp
) {}
}
39 changes: 32 additions & 7 deletions src/PayloadParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,45 @@

namespace JeckelLab\MauticWebhookParser;

use JeckelLab\MauticWebhookParser\ValueObject\MauticEvent;
use DateTimeImmutable;
use Exception;
use JeckelLab\MauticWebhookParser\Exception\LogicException;
use JeckelLab\MauticWebhookParser\Model\Client;
use JeckelLab\MauticWebhookParser\Model\Lead;
use JeckelLab\MauticWebhookParser\Model\MauticEvent;
use JeckelLab\MauticWebhookParser\ValueObject\MauticEventType;

class PayloadParser
{
/**
* @param array<string, mixed> $payload
* @return iterable<array{event: MauticEvent}>
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @param array<string, array<array<string, mixed>>> $payload
* @return iterable<MauticEvent>
* @throws Exception
*/
public function parse(array $payload): iterable
{
foreach ($payload as $eventName => $value) {
$event = MauticEvent::from($eventName);
yield ['event' => $event];
foreach ($payload as $eventName => $events) {
$event = MauticEventType::from($eventName);
foreach ($events as $eventPayload) {
yield $this->parseEvent($eventPayload, $event);
}
}
}

/**
* @param array<string, mixed> $eventPayload
* @throws Exception
*/
public function parseEvent(array $eventPayload, MauticEventType $event): MauticEvent

Check warning on line 41 in src/PayloadParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * @param array<string, mixed> $eventPayload * @throws Exception */ - public function parseEvent(array $eventPayload, MauticEventType $event) : MauticEvent + protected function parseEvent(array $eventPayload, MauticEventType $event) : MauticEvent { if (!is_string($eventPayload['timestamp'])) { throw new LogicException('Missing timestamp');
{
if (! is_string($eventPayload['timestamp'])) {
throw new LogicException('Missing timestamp');
}
return new MauticEvent(
eventType: $event,
client: new Client(),
lead: new Lead(),
timestamp: new DateTimeImmutable($eventPayload['timestamp'])
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace JeckelLab\MauticWebhookParser\ValueObject;

enum MauticEvent: string
enum MauticEventType: string
{
case LEAD_POST_SAVE_NEW = "mautic.lead_post_save_new";
}
9 changes: 5 additions & 4 deletions tests/PayloadParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


use JeckelLab\MauticWebhookParser\PayloadParser;
use JeckelLab\MauticWebhookParser\ValueObject\MauticEvent;
use JeckelLab\MauticWebhookParser\ValueObject\MauticEventType;
use PHPUnit\Framework\TestCase;

class PayloadParserTest extends TestCase
Expand All @@ -19,8 +19,8 @@ public function testItExtractCorrectEvents(): void
$parser = new PayloadParser();
$payload = [
"mautic.lead_post_save_new" => [
"data" => [
"id" => 123,
[
"timestamp" => "2017-06-19T09:31:18+00:00",
],
],
];
Expand All @@ -29,6 +29,7 @@ public function testItExtractCorrectEvents(): void
$events[] = $event;
}
self::assertCount(1, $events);
self::assertEquals(MauticEvent::LEAD_POST_SAVE_NEW, $events[0]['event']);
self::assertEquals(MauticEventType::LEAD_POST_SAVE_NEW, $events[0]->eventType);
self::assertEquals("2017-06-19T09:31:18+00:00", $events[0]->timestamp->format("c"));
}
}
583 changes: 582 additions & 1 deletion tests/samples/contact-identified.json

Large diffs are not rendered by default.

0 comments on commit 1d11236

Please sign in to comment.