ldc-supportweb/app/Services/BrevoWebookParser.php

35 lines
923 B
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Dto\BrevoWebHookDto;
use App\Exceptions\BrevoParseFailure;
class BrevoWebookParser
{
/** @throws BrevoParseFailure */
public function parseRawHtml(string $rawHtml): BrevoWebHookDto
{
$data = [];
$plainText = strip_tags($rawHtml);
$tblRegexPatterns = [
'user' => '/^Matricule et Nom du demandeur\s*\r?\n(.*)$/im',
'email' => '/^Email du demandeur\s*\r?\n(.*)$/im',
'brand' => '/^Marque\s*\/\s*Filiale\s*\r?\n(.*)$/im',
];
foreach ($tblRegexPatterns as $field => $regex) {
if (preg_match($regex, $plainText, $matches)) {
$data[$field] = trim($matches[1]);
}
}
return new BrevoWebHookDto(
plainTextContent: $plainText,
brand: $data['brand'] ?? null
);
}
}