Files
mautic-marketing-program-bu…/Form/Validator/SmartLinkValidator.php
T
Alexandre Gain 9d81d6c815 refactor(naming): normalise the Smart Links domain to the bundle's canonical names (0.18.1)
One bundle, two domains (Marketing Programs and Smart Links), one naming scheme.\nNothing in the database changed (no tables, columns, URLs or stored data);\nthe runtime was rebuilt and verified live.\n\n- Route names mautic_smartlinks_* -> mt3s_smartlinks_* (paths unchanged);\n  the menu ids follow\n- Model service aliases collapsed to one canonical alias per model:\n  mautic.mt3smarketingprogram.model.<entity> (core's mautic.<x>.model.<x>\n  pattern), so getModel('mt3smarketingprogram.smart_link'); the dead\n  mt3s.* and mautic.mt3smp.*-era aliases are gone\n- Translation keys mautic.smartlinks.* -> mt3s.smart_links.*\n  (mautic.config.tab.smartlinksconfig stays: core derives it)\n- Settings parameters smartlinks_utm_config/smartlinks_config ->\n  mt3s_smart_links_utm_config/mt3s_smart_links_config, with both legacy\n  keys (smartlinks_* of the standalone bundle, utm_manager_* of the\n  UtmManagerBundle era) still readable until the settings are saved again\n- JS loader prefixes smartlinksSmartLink/smartlinksUtmTaxonomy ->\n  mt3sMpSmartLink/mt3sMpUtmTaxonomy (the mt3sMp* family of the program\n  domain)\n- Integration/Support/ConfigSupport -> SmartlinksConfigSupport; integration\n  display names aligned ('MT3S Marketing Program' / 'Smart Links'); the\n  dead mautic.plugin.smartlinks.name catalog keys removed\n- phpunit suite renamed UtmManagerBundle -> MT3SMarketingProgramBundle;\n  version 0.18.1\n\nVerified: full php -l, 90/90 unit tests (phpunit 11.5 phar), cache clear +\nwarm + assets regenerate + plugins reload (as www-data), live: both lists\nand forms (mauticContent mt3sMpSmartLink/mt3sMpUtmTaxonomy), both config\ntabs, the menu labels, the program pages (regression), the public /l/y32b\nredirect through the legacy utm_manager_* settings (302 with the taxonomy's\nUTM values, wrong host and unknown slug 404), all ini catalogs parse, zero\nnew log errors.
2026-11-16 15:47:30 +01:00

178 lines
7.1 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\Service\UtmConfigurationProvider;
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
/**
* 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;
}
}