-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
682 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,4 @@ | |
|
||
namespace JeckelLab\MauticWebhookParser\Model; | ||
|
||
class Client | ||
{ | ||
} | ||
class Client {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / PHP 8.2
|
||
{ | ||
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']) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.