Skip to content

Commit

Permalink
Extract event from payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jeckel committed Oct 13, 2023
1 parent 389c954 commit 3b69a22
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 71 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
"email": "[email protected]"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Jeckel-Lab/etl"
}
],
"require": {
"php": "~8.2"
"php": "~8.2",
"jeckel-lab/etl": "dev-main"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.34",
Expand Down
28 changes: 14 additions & 14 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ grumphp:
- composer_require_checker
- phpmd
- phpstan
- phpcsfixer
# - phpcsfixer
tasks:
phpcsfixer:
allow_risky: ~
cache_file: ~
config: ~
rules:
line_ending: true
array_syntax:
syntax: short
using_cache: ~
config_contains_finder: false
verbose: true
diff: false
triggered_by: ['php']
# phpcsfixer:
# allow_risky: ~
# cache_file: ~
# config: ~
# rules:
# line_ending: true
# array_syntax:
# syntax: short
# using_cache: ~
# config_contains_finder: false
# verbose: true
# diff: false
# triggered_by: ['php']
phpmd:
whitelist_patterns:
- /^src\/(.*)/
Expand Down
14 changes: 14 additions & 0 deletions src/Model/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @author: Julien Mercier-Rojas <[email protected]>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Model;

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

/**
* @author: Julien Mercier-Rojas <[email protected]>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser;

use JeckelLab\MauticWebhookParser\ValueObject\MauticEvent;

class PayloadParser
{
/**
* @param array<string, mixed> $payload
* @return iterable<array{event: MauticEvent}>
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function parse(array $payload): iterable
{
foreach ($payload as $eventName => $value) {
$event = MauticEvent::from($eventName);
yield ['event' => $event];
}
}
}
25 changes: 0 additions & 25 deletions src/Placeholder.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/ValueObject/MauticEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @author: Julien Mercier-Rojas <[email protected]>
* Created at: 13/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\ValueObject;

enum MauticEvent: string
{
case LEAD_POST_SAVE_NEW = "mautic.lead_post_save_new";
}
34 changes: 34 additions & 0 deletions tests/PayloadParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @author: Julien Mercier-Rojas <[email protected]>
* Created at: 13/10/2023
*/

declare(strict_types=1);


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

class PayloadParserTest extends TestCase
{
public function testItExtractCorrectEvents(): void
{
$parser = new PayloadParser();
$payload = [
"mautic.lead_post_save_new" => [
"data" => [
"id" => 123,
],
],
];
$events = [];
foreach($parser->parse($payload) as $event) {
$events[] = $event;
}
self::assertCount(1, $events);
self::assertEquals(MauticEvent::LEAD_POST_SAVE_NEW, $events[0]['event']);
}
}
31 changes: 0 additions & 31 deletions tests/PlaceholderTest.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/samples/contact-identified.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"mautic.lead_post_save_new":[{"lead":{"isPublished":true,"dateAdded":"2017-06-19T09:31:18+00:00","dateModified":null,"createdBy":1,"createdByUser":"John Doe","modifiedBy":null,"modifiedByUser":null,"id":52,"points":0,"color":null,"fields":{"core":{"timezone1":{"id":46,"group":"core","label":"timezone","alias":"timezone1","type":"timezone","value":"Europe/Prague"},"datetime":{"id":45,"group":"core","label":"datetime","alias":"datetime","type":"datetime","value":""},"boolean":{"id":44,"group":"core","label":"boolean","alias":"boolean","type":"boolean","value":null},"f_select":{"id":43,"group":"core","label":"select","alias":"f_select","type":"select","value":null},"multiselect":{"id":42,"group":"core","label":"Multiselect","alias":"multiselect","type":"multiselect","value":"php|js"},"title":{"id":1,"group":"core","label":"Title","alias":"title","type":"lookup","value":"Mr."},"firstname":{"id":2,"group":"core","label":"First Name","alias":"firstname","type":"text","value":"John"},"lastname":{"id":3,"group":"core","label":"Last Name","alias":"lastname","type":"text","value":"Doe"},"company":{"id":4,"group":"core","label":"Company","alias":"company","type":"text","value":null},"position":{"id":5,"group":"core","label":"Position","alias":"position","type":"text","value":null},"email":{"id":6,"group":"core","label":"Email","alias":"email","type":"email","value":"[email protected]"},"mobile":{"id":7,"group":"core","label":"Mobile","alias":"mobile","type":"tel","value":null},"phone":{"id":8,"group":"core","label":"Phone","alias":"phone","type":"tel","value":null},"fax":{"id":9,"group":"core","label":"Fax","alias":"fax","type":"tel","value":null},"address1":{"id":10,"group":"core","label":"Address Line 1","alias":"address1","type":"text","value":null},"address2":{"id":11,"group":"core","label":"Address Line 2","alias":"address2","type":"text","value":null},"city":{"id":12,"group":"core","label":"City","alias":"city","type":"text","value":"Prague"},"state":{"id":13,"group":"core","label":"State","alias":"state","type":"region","value":null},"zipcode":{"id":14,"group":"core","label":"Zip Code","alias":"zipcode","type":"text","value":"16000"},"country":{"id":15,"group":"core","label":"Country","alias":"country","type":"country","value":"Czech Republic"},"preferred_locale":{"id":16,"group":"core","label":"Preferred Locale","alias":"preferred_locale","type":"locale","value":"cs_CZ"},"attribution_date":{"id":17,"group":"core","label":"Attribution Date","alias":"attribution_date","type":"datetime","value":"2017-06-14 11:30:00"},"attribution":{"id":18,"group":"core","label":"Attribution","alias":"attribution","type":"number","value":32},"website":{"id":19,"group":"core","label":"Website","alias":"website","type":"url","value":null}},"social":{"facebook":{"id":20,"group":"social","label":"Facebook","alias":"facebook","type":"text","value":null},"foursquare":{"id":21,"group":"social","label":"Foursquare","alias":"foursquare","type":"text","value":null},"googleplus":{"id":22,"group":"social","label":"Google+","alias":"googleplus","type":"text","value":null},"instagram":{"id":23,"group":"social","label":"Instagram","alias":"instagram","type":"text","value":null},"linkedin":{"id":24,"group":"social","label":"LinkedIn","alias":"linkedin","type":"text","value":null},"skype":{"id":25,"group":"social","label":"Skype","alias":"skype","type":"text","value":null},"twitter":{"id":26,"group":"social","label":"Twitter","alias":"twitter","type":"text","value":null}},"personal":[],"professional":[]},"lastActive":null,"owner":{"createdByUser":null,"modifiedByUser":null,"id":1,"username":"admin","firstName":"John","lastName":"Doe"},"ipAddresses":[],"tags":[[]],"utmtags":null,"stage":null,"dateIdentified":"2017-06-19T09:31:18+00:00","preferredProfileImage":"gravatar","doNotContact":[],"frequencyRules":[]},"contact":{"isPublished":true,"dateAdded":"2017-06-19T09:31:18+00:00","dateModified":null,"createdBy":1,"createdByUser":"John Doe","modifiedBy":null,"modifiedByUser":null,"id":52,"points":0,"color":null,"fields":{"core":{"timezone1":{"id":46,"group":"core","label":"timezone","alias":"timezone1","type":"timezone","value":"Europe/Prague"},"datetime":{"id":45,"group":"core","label":"datetime","alias":"datetime","type":"datetime","value":""},"boolean":{"id":44,"group":"core","label":"boolean","alias":"boolean","type":"boolean","value":null},"f_select":{"id":43,"group":"core","label":"select","alias":"f_select","type":"select","value":null},"multiselect":{"id":42,"group":"core","label":"Multiselect","alias":"multiselect","type":"multiselect","value":"php|js"},"title":{"id":1,"group":"core","label":"Title","alias":"title","type":"lookup","value":"Mr."},"firstname":{"id":2,"group":"core","label":"First Name","alias":"firstname","type":"text","value":"John"},"lastname":{"id":3,"group":"core","label":"Last Name","alias":"lastname","type":"text","value":"Doe"},"company":{"id":4,"group":"core","label":"Company","alias":"company","type":"text","value":null},"position":{"id":5,"group":"core","label":"Position","alias":"position","type":"text","value":null},"email":{"id":6,"group":"core","label":"Email","alias":"email","type":"email","value":"[email protected]"},"mobile":{"id":7,"group":"core","label":"Mobile","alias":"mobile","type":"tel","value":null},"phone":{"id":8,"group":"core","label":"Phone","alias":"phone","type":"tel","value":null},"fax":{"id":9,"group":"core","label":"Fax","alias":"fax","type":"tel","value":null},"address1":{"id":10,"group":"core","label":"Address Line 1","alias":"address1","type":"text","value":null},"address2":{"id":11,"group":"core","label":"Address Line 2","alias":"address2","type":"text","value":null},"city":{"id":12,"group":"core","label":"City","alias":"city","type":"text","value":"Prague"},"state":{"id":13,"group":"core","label":"State","alias":"state","type":"region","value":null},"zipcode":{"id":14,"group":"core","label":"Zip Code","alias":"zipcode","type":"text","value":"16000"},"country":{"id":15,"group":"core","label":"Country","alias":"country","type":"country","value":"Czech Republic"},"preferred_locale":{"id":16,"group":"core","label":"Preferred Locale","alias":"preferred_locale","type":"locale","value":"cs_CZ"},"attribution_date":{"id":17,"group":"core","label":"Attribution Date","alias":"attribution_date","type":"datetime","value":"2017-06-14 11:30:00"},"attribution":{"id":18,"group":"core","label":"Attribution","alias":"attribution","type":"number","value":32},"website":{"id":19,"group":"core","label":"Website","alias":"website","type":"url","value":null}},"social":{"facebook":{"id":20,"group":"social","label":"Facebook","alias":"facebook","type":"text","value":null},"foursquare":{"id":21,"group":"social","label":"Foursquare","alias":"foursquare","type":"text","value":null},"googleplus":{"id":22,"group":"social","label":"Google+","alias":"googleplus","type":"text","value":null},"instagram":{"id":23,"group":"social","label":"Instagram","alias":"instagram","type":"text","value":null},"linkedin":{"id":24,"group":"social","label":"LinkedIn","alias":"linkedin","type":"text","value":null},"skype":{"id":25,"group":"social","label":"Skype","alias":"skype","type":"text","value":null},"twitter":{"id":26,"group":"social","label":"Twitter","alias":"twitter","type":"text","value":null}},"personal":[],"professional":[]},"lastActive":null,"owner":{"createdByUser":null,"modifiedByUser":null,"id":1,"username":"admin","firstName":"John","lastName":"Doe"},"ipAddresses":[],"tags":[[]],"utmtags":null,"stage":null,"dateIdentified":"2017-06-19T09:31:18+00:00","preferredProfileImage":"gravatar","doNotContact":[],"frequencyRules":[]},"timestamp":"2017-06-19T09:31:18+00:00"}],"timestamp":"2023-10-13T14:38:45+00:00"}

0 comments on commit 3b69a22

Please sign in to comment.