Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse fields into Field VO #5

Merged
merged 8 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"analyze": "@analyse",
"cs-fix": "vendor/bin/php-cs-fixer fix --diff --verbose",
"phpmd": "vendor/bin/phpmd src,tests text ruleset.xml",
"test": "vendor/bin/phpunit --testdox",
"test": "vendor/bin/phpunit --testdox --display-warnings",
"test-coverage": [
"Composer\\Config::disableProcessTimeout",
"XDEBUG_MODE=coverage vendor/bin/phpunit --testdox --coverage-html=.build/coverage"
Expand Down
10 changes: 10 additions & 0 deletions src/Builder/ContactBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use JeckelLab\MauticWebhookParser\Identity\ContactId;
use JeckelLab\MauticWebhookParser\Model\Contact;

use JeckelLab\MauticWebhookParser\Model\FieldCollection;
use JeckelLab\MauticWebhookParser\Model\User;

use function JeckelLab\MauticWebhookParser\toNullableDateTime;
Expand All @@ -27,6 +28,7 @@ class ContactBuilder
private ?DateTimeImmutable $dateAdded = null;
private ?DateTimeImmutable $dateIdentified = null;
private ?DateTimeImmutable $dateModified = null;
private ?FieldCollection $fields = null;

public function __construct()
{
Expand All @@ -42,6 +44,7 @@ public function reset(): self
$this->dateAdded = null;
$this->dateIdentified = null;
$this->dateModified = null;
$this->fields = null;
return $this;
}

Expand Down Expand Up @@ -80,6 +83,12 @@ public function withDates(
return $this;
}

public function withFields(FieldCollection $fields): self
{
$this->fields = $fields;
return $this;
}

public function build(): Contact
{
$result = new Contact(
Expand All @@ -90,6 +99,7 @@ public function build(): Contact
isPublished: $this->isPublished,
owner: $this->owner,
points: $this->points,
fields: $this->fields ?? new FieldCollection([])
);
$this->reset();
return $result;
Expand Down
38 changes: 38 additions & 0 deletions src/Builder/FieldCollectionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Builder;

use JeckelLab\MauticWebhookParser\Model\FieldCollection;
use JeckelLab\MauticWebhookParser\ValueObject\Field\Field;

class FieldCollectionBuilder
{
/**
* @var array<string, Field> $fields
*/
private array $fields = [];

public function reset(): self
{
$this->fields = [];
return $this;
}

public function withField(Field $field): self
{
$this->fields[$field->alias()] = $field;
return $this;
}

public function build(): FieldCollection
{
return new FieldCollection($this->fields);
}
}
27 changes: 19 additions & 8 deletions src/ContactParser.php → src/Factory/ContactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,47 @@

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser;
namespace JeckelLab\MauticWebhookParser\Factory;

use Exception;
use JeckelLab\MauticWebhookParser\Builder\ContactBuilder;
use JeckelLab\MauticWebhookParser\Director\UserDirector;
use JeckelLab\MauticWebhookParser\Exception\InvalidArgumentException;
use JeckelLab\MauticWebhookParser\Exception\LogicException;
use JeckelLab\MauticWebhookParser\Model\Contact;

class ContactParser
use function JeckelLab\MauticWebhookParser\toDateTime;
use function JeckelLab\MauticWebhookParser\toNullableDateTime;

class ContactFactory
{
private UserDirector $userDirector;
private UserFactory $userFactory;
private FieldCollectionFactory $fieldsFactory;

public function __construct(?UserDirector $userDirector = null)
public function __construct(?UserFactory $userFactory = null, ?FieldCollectionFactory $fieldsFactory = null)
{
$this->userDirector = $userDirector ?? new UserDirector();
$this->userFactory = $userFactory ?? new UserFactory();
$this->fieldsFactory = $fieldsFactory ?? new FieldCollectionFactory();
}

/**
* @param array<string, mixed> $payload
* @return Contact
* @throws Exception|LogicException
*/
public function parse(array $payload): Contact
public function constructFromJson(array $payload): Contact
{
$owner = null;
if (is_array($payload["owner"])) {
/** @var array<string, mixed> $ownerData */
$ownerData = $payload["owner"];
$owner = $this->userDirector->constructFromJson($ownerData);
$owner = $this->userFactory->constructFromJson($ownerData);
}
if (! is_array($payload['fields'])) {
throw new InvalidArgumentException('Missing fields');
}
/** @var array<string, mixed> $fieldsData */
$fieldsData = $payload['fields'];
$fields = $this->fieldsFactory->constructFromJson($fieldsData);

return (new ContactBuilder())
->withId(
Expand All @@ -51,6 +61,7 @@ public function parse(array $payload): Contact
toDateTime($payload['dateIdentified']),
toNullableDateTime($payload['dateModified'])
)
->withFields($fields)
->build();
}
}
54 changes: 54 additions & 0 deletions src/Factory/FieldCollectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Factory;

use JeckelLab\MauticWebhookParser\Builder\FieldCollectionBuilder;
use JeckelLab\MauticWebhookParser\Model\FieldCollection;
use JeckelLab\MauticWebhookParser\Parser\FieldParser;

class FieldCollectionFactory
{
private FieldCollectionBuilder $fieldCollectionBuilder;
private FieldParser $fieldParser;

public function __construct(
?FieldCollectionBuilder $fieldCollectionBuilder = null,
?FieldParser $fieldParser = null
) {
$this->fieldCollectionBuilder = $fieldCollectionBuilder ?? new FieldCollectionBuilder();
$this->fieldParser = $fieldParser ?? new FieldParser();
}

/**
* @param array<string, mixed> $jsonData
* @return FieldCollection
*/
public function constructFromJson(array $jsonData): FieldCollection
{
$this->fieldCollectionBuilder->reset();
foreach ($jsonData as $fields) {
if (! is_array($fields)) {
continue;
}
foreach ($fields as $fieldData) {
if (! is_array($fieldData)) {
continue;
}
/** @var array{alias: string, group: string, id: int, label: string, type: string, value: mixed} $fieldData */
$field = $this->fieldParser->parseFieldValue($fieldData);
if (null === $field) {
continue;
}
$this->fieldCollectionBuilder->withField($field);
}
}
return $this->fieldCollectionBuilder->build();
}
}
41 changes: 41 additions & 0 deletions src/Factory/FieldTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Factory;

use JeckelLab\MauticWebhookParser\Identity\FieldTypeId;
use JeckelLab\MauticWebhookParser\ValueObject\FieldType;
use JeckelLab\MauticWebhookParser\ValueObject\FieldTypeGroup;
use JeckelLab\MauticWebhookParser\ValueObject\FieldTypeType;

class FieldTypeFactory
{
/**
* @var array<int, FieldType>
*/
private array $fieldTypes = [];

/**
* @param array{alias: string, group: string, id: int, label: string, type: string} $fieldData
* @return FieldType
*/
public function constructFromJson(array $fieldData): FieldType
{
if (! isset($this->fieldTypes[$fieldData['id']])) {
$this->fieldTypes[$fieldData['id']] = new FieldType(
FieldTypeType::from($fieldData['type']),
$fieldData['alias'],
FieldTypeGroup::from($fieldData['group']),
FieldTypeId::from($fieldData['id']),
$fieldData['label']
);
}
return $this->fieldTypes[$fieldData['id']];
}
}
4 changes: 2 additions & 2 deletions src/Director/UserDirector.php → src/Factory/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Director;
namespace JeckelLab\MauticWebhookParser\Factory;

use JeckelLab\MauticWebhookParser\Builder\UserBuilder;
use JeckelLab\MauticWebhookParser\Exception\InvalidArgumentException;
use JeckelLab\MauticWebhookParser\Model\User;

class UserDirector
class UserFactory
{
private UserBuilder $userBuilder;

Expand Down
14 changes: 14 additions & 0 deletions src/Identity/FieldTypeId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Identity;

use JeckelLab\IdentityContract\AbstractIntIdentity;

final readonly class FieldTypeId extends AbstractIntIdentity {}
3 changes: 2 additions & 1 deletion src/Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
public ContactId $id,
public bool $isPublished,
public ?User $owner,
public int $points
public int $points,
public FieldCollection $fields,
) {}
}
12 changes: 0 additions & 12 deletions src/Model/Contact/CoreFields.php

This file was deleted.

39 changes: 39 additions & 0 deletions src/Model/FieldCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\Model;

use Countable;
use JeckelLab\MauticWebhookParser\ValueObject\Field\Field;

readonly class FieldCollection implements Countable
{
/**
* @param array<string, Field> $fields
*/
public function __construct(private array $fields) {}

public function get(string $name): ?Field
{
return $this->fields[$name] ?? null;
}

/**
* @return string[]
*/
public function getAliases(): array
{
return array_keys($this->fields);
}

public function count(): int
{
return count($this->fields);
}
}
Loading