Skip to content

Commit

Permalink
Start contact payload parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeckel committed Oct 13, 2023
1 parent 1d11236 commit c9b4116
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 31 deletions.
5 changes: 5 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"symbol-whitelist": [
"toNullableDateTime"
]
}
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.34",
"infection/infection": "^0.27.4",
"maglnet/composer-require-checker": "^4.7",
"mockery/mockery": "^1.6",
"phpmd/phpmd": "^2.14",
"phpro/grumphp": "^2.1",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"phpunit/phpunit": "^10.4",
"symfony/var-dumper": "^6.3",
"phpro/grumphp": "^2.1",
"phpmd/phpmd": "^2.14",
"maglnet/composer-require-checker": "^4.7",
"infection/infection": "^0.27.4",
"roave/security-advisories": "dev-latest"
"roave/security-advisories": "dev-latest",
"symfony/var-dumper": "^6.3"
},
"autoload": {
"psr-4": {
"JeckelLab\\MauticWebhookParser\\": "src"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ grumphp:
use_grumphp_paths: true
composer_require_checker:
composer_file: 'composer.json'
config_file: ~
config_file: 'composer-require-checker.json'
ignore_parse_errors: false
triggered_by: [ 'composer.json', 'composer.lock', 'src/*.php' ]
git_branch_name:
Expand Down
34 changes: 34 additions & 0 deletions src/ContactParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser;

use DateTimeImmutable;
use Exception;
use JeckelLab\MauticWebhookParser\Exception\LogicException;
use JeckelLab\MauticWebhookParser\Model\Contact;

class ContactParser
{
/**
* @param array<string, mixed> $payload
* @return Contact
* @throws Exception|LogicException
*/
public function parse(array $payload): Contact
{
return new Contact(
dateAdded: is_string($payload['dateAdded']) ? new DateTimeImmutable($payload['dateAdded']) : throw new LogicException('Missing dateAdded'),
dateIdentified: is_string($payload['dateIdentified']) ? new DateTimeImmutable($payload['dateIdentified']) : throw new LogicException('Missing dateIdentified'),
dateModified: toNullableDateTime($payload['dateModified'] ?? null),
id: is_int($payload['id']) ? $payload['id'] : throw new LogicException('Missing id'),
isPublished: (bool) $payload['isPublished'],

Check warning on line 31 in src/ContactParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "CastBool": --- Original +++ New @@ @@ */ public function parse(array $payload) : Contact { - return new Contact(dateAdded: is_string($payload['dateAdded']) ? new DateTimeImmutable($payload['dateAdded']) : throw new LogicException('Missing dateAdded'), dateIdentified: is_string($payload['dateIdentified']) ? new DateTimeImmutable($payload['dateIdentified']) : throw new LogicException('Missing dateIdentified'), dateModified: toNullableDateTime($payload['dateModified'] ?? null), id: is_int($payload['id']) ? $payload['id'] : throw new LogicException('Missing id'), isPublished: (bool) $payload['isPublished']); + return new Contact(dateAdded: is_string($payload['dateAdded']) ? new DateTimeImmutable($payload['dateAdded']) : throw new LogicException('Missing dateAdded'), dateIdentified: is_string($payload['dateIdentified']) ? new DateTimeImmutable($payload['dateIdentified']) : throw new LogicException('Missing dateIdentified'), dateModified: toNullableDateTime($payload['dateModified'] ?? null), id: is_int($payload['id']) ? $payload['id'] : throw new LogicException('Missing id'), isPublished: $payload['isPublished']); } }
);
}
}
12 changes: 0 additions & 12 deletions src/Model/Client.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/Model/Contact.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;

readonly class Contact
{
public function __construct(
public DateTimeImmutable $dateAdded,
public DateTimeImmutable $dateIdentified,
public ?DateTimeImmutable $dateModified,
public int $id,
public bool $isPublished,
) {}
}
2 changes: 1 addition & 1 deletion src/Model/MauticEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
public function __construct(
public MauticEventType $eventType,
public Client $client,
public Contact $client,
public Lead $lead,
public DateTimeImmutable $timestamp
) {}
Expand Down
15 changes: 13 additions & 2 deletions src/PayloadParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
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
{
public function __construct(
private readonly ContactParser $clientParser,
) {}

/**
* @param array<string, array<array<string, mixed>>> $payload
* @return iterable<MauticEvent>
Expand All @@ -27,6 +30,9 @@ class PayloadParser
public function parse(array $payload): iterable
{
foreach ($payload as $eventName => $events) {
if ($eventName === 'timestamp') {
continue;

Check warning on line 34 in src/PayloadParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ { foreach ($payload as $eventName => $events) { if ($eventName === 'timestamp') { - continue; + break; } $event = MauticEventType::from($eventName); foreach ($events as $eventPayload) {
}
$event = MauticEventType::from($eventName);
foreach ($events as $eventPayload) {
yield $this->parseEvent($eventPayload, $event);
Expand All @@ -43,9 +49,14 @@ public function parseEvent(array $eventPayload, MauticEventType $event): MauticE
if (! is_string($eventPayload['timestamp'])) {
throw new LogicException('Missing timestamp');
}
if (! is_array($eventPayload['contact'])) {
throw new LogicException('Missing contact');
}
/** @var array<string, mixed> $contactPayload */
$contactPayload = $eventPayload['contact'];
return new MauticEvent(
eventType: $event,
client: new Client(),
client: $this->clientParser->parse($contactPayload),
lead: new Lead(),
timestamp: new DateTimeImmutable($eventPayload['timestamp'])
);
Expand Down
22 changes: 22 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser;

use DateTimeImmutable;
use Exception;

function toNullableDateTime(mixed $date): ?DateTimeImmutable
{
try {
return is_string($date) ? new DateTimeImmutable($date) : null;
} catch (Exception) {
return null;
}
}
15 changes: 7 additions & 8 deletions tests/PayloadParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
declare(strict_types=1);


use JeckelLab\MauticWebhookParser\ContactParser;
use JeckelLab\MauticWebhookParser\PayloadParser;
use JeckelLab\MauticWebhookParser\ValueObject\MauticEventType;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,14 +17,12 @@ class PayloadParserTest extends TestCase
{
public function testItExtractCorrectEvents(): void
{
$parser = new PayloadParser();
$payload = [
"mautic.lead_post_save_new" => [
[
"timestamp" => "2017-06-19T09:31:18+00:00",
],
],
];
$parser = new PayloadParser(new ContactParser());
/** @var string $payloadString */
$payloadString = file_get_contents(__DIR__ . '/fixtures/contact-identified.json');

/** @var array<string, array<array<string, mixed>>> $payload */
$payload = json_decode($payloadString, true);
$events = [];
foreach($parser->parse($payload) as $event) {
$events[] = $event;
Expand Down
File renamed without changes.

0 comments on commit c9b4116

Please sign in to comment.