Files
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

178 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Form\Validator;
use Mautic\FormBundle\Entity\Form;
use Mautic\PageBundle\Entity\Page;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectType;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\UtmTaxonomy;
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
use MauticPlugin\MT3SMarketingProgramBundle\Service\UtmConfigurationProvider;
/**
* Business rules of the Smart Link form. The form type calls these methods from Callback
* constraints; each method returns translation keys (validators domain) and parameters.
*/
final class SmartLinkValidator
{
public const ERROR_SLUG_TAKEN = 'mt3s.smart_links.smart_links.error.slug_taken';
public const ERROR_SLUG_REQUIRED = 'mt3s.smart_links.smart_links.error.slug_required';
public const ERROR_SLUG_LENGTH = 'mt3s.smart_links.smart_links.error.slug_length';
public const ERROR_DESTINATION_REQUIRED = 'mt3s.smart_links.smart_links.error.destination_required';
public const ERROR_DESTINATION_REQUIRED_MAUTIC = 'mt3s.smart_links.smart_links.error.destination_required_mautic';
public const ERROR_DESTINATION_INVALID = 'mt3s.smart_links.smart_links.error.destination_invalid';
public const ERROR_UTM_VALUE_INVALID = 'mt3s.smart_links.smart_links.error.utm_value_invalid';
public const ERROR_UTM_VALUE_NOT_ALLOWED = 'mt3s.smart_links.smart_links.error.utm_value_not_allowed';
public const ERROR_TAXONOMY_PARAMETER_UNKNOWN = 'mt3s.smart_links.smart_links.error.taxonomy_parameter_unknown';
public function __construct(
private readonly SmartLinkModel $smartLinkModel,
private readonly UtmConfigurationProvider $utmConfiguration,
) {
}
/**
* Unique slug. The rules (lowercase, separator replacement) are applied by the model
* before this check, so the comparison uses the final slug.
*
* @return list<array{0: string, 1: array<string, string>}>
*/
public function validateSlug(string $slug, ?int $ignoreId = null): array
{
if ('' === $slug) {
return [[self::ERROR_SLUG_REQUIRED, []]];
}
if ($this->smartLinkModel->isSlugTaken($slug, $ignoreId)) {
return [[self::ERROR_SLUG_TAKEN, ['%slug%' => $slug]]];
}
return [];
}
/**
* A destination is required for every redirect type: an URL for external ones, a landing
* page or a standalone form for Mautic ones. Only http(s) URLs are accepted.
*
* @return list<array{0: string, 1: array<string, string>}>
*/
public function validateDestination(SmartLinkRedirectType $redirectType, ?string $url, ?Page $page = null, ?Form $form = null): array
{
if ($redirectType->isMautic()) {
if (null === $page && null === $form) {
return [[self::ERROR_DESTINATION_REQUIRED_MAUTIC, []]];
}
return [];
}
$url = trim((string) $url);
if ('' === $url) {
return [[self::ERROR_DESTINATION_REQUIRED, []]];
}
if (!filter_var($url, FILTER_VALIDATE_URL)
|| !in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)) {
return [[self::ERROR_DESTINATION_INVALID, ['%url%' => $url]]];
}
return [];
}
/**
* UTM values follow the Smart Links Settings the admin defines. With a taxonomy chosen:
* every parameter the taxonomy defines limits the value -- the checkbox subset of the
* admin's values for a dropdown, the taxonomy's own list for a free parameter. The
* values the link carried before the edit stay acceptable (they were chosen when the
* taxonomy — or, before the feature, the global configuration — allowed them), so a plain
* re-save never errors. Without a taxonomy (links that predate the feature, before their
* next edit), the values follow the rules of Smart Links Settings.
*
* @param array<string, string> $utmParameters
* @param array<string, string> $stored the values the link carried before the edit
*
* @return list<array{0: string, 1: array<string, string>}>
*/
public function validateUtmParameters(array $utmParameters, ?UtmTaxonomy $taxonomy = null, array $stored = []): array
{
if (null === $taxonomy) {
return $this->validateAgainstGlobalConfiguration($utmParameters, $stored);
}
$errors = [];
foreach ($utmParameters as $key => $value) {
$parameter = $this->utmConfiguration->getParameter($key);
if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) {
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
continue;
}
// Every parameter of the taxonomy limits the value: a dropdown subset selected
// among the admin's values, or the taxonomy's own list for a free parameter.
$allowed = $taxonomy->getParameterValues($key);
if ([] === $allowed) {
$errors[] = [self::ERROR_TAXONOMY_PARAMETER_UNKNOWN, ['%key%' => $key, '%name%' => (string) $taxonomy->getName()]];
continue;
}
if (in_array($value, array_column($allowed, 'value'), true) || ($stored[$key] ?? null) === $value) {
continue;
}
$errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]];
}
return $errors;
}
/**
* The rules of Smart Links Settings: the values belong to enabled parameters, respect the
* technical pattern, and dropdown values stay within the configured list — unless the link
* already carried them.
*
* @param array<string, string> $utmParameters
* @param array<string, string> $stored
*
* @return list<array{0: string, 1: array<string, string>}>
*/
private function validateAgainstGlobalConfiguration(array $utmParameters, array $stored): array
{
$valuePattern = UtmSettingsValidator::VALUE_PATTERN;
$errors = [];
foreach ($utmParameters as $key => $value) {
$parameter = $this->utmConfiguration->getParameter($key);
if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) {
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
continue;
}
if (1 !== preg_match($valuePattern, $value)) {
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
continue;
}
$inList = UtmParameterMode::DROPDOWN !== $parameter['mode']
|| in_array($value, array_column($this->utmConfiguration->getDropdownValues($key), 'value'), true);
if (!$inList && ($stored[$key] ?? null) !== $value) {
$errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]];
}
}
return $errors;
}
}