Files
mautic-marketing-program-bu…/Entity/SmartLink.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

305 lines
9.2 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
use Mautic\FormBundle\Entity\Form;
use Mautic\PageBundle\Entity\Page;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectCode;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* A Smart Link: a short link ({shortLinkDomain}/l/{slug}) that carries the UTM parameters.
*
* The slug is applied when the link is created (spaces replaced, lowercase enforced) and is
* immutable afterwards. Destination URLs only exist for external redirects; Mautic redirects
* are not processed yet.
*/
class SmartLink extends FormEntity
{
public const TABLE_NAME = 'smart_links';
public const SLUG_MAX_LENGTH = 9; // "text length < 10"
private ?int $id = null;
private ?string $slug = null;
private SmartLinkRedirectType $redirectType = SmartLinkRedirectType::External;
private SmartLinkRedirectCode $redirectCode = SmartLinkRedirectCode::Found;
private ?string $destinationUrl = null;
/**
* Values of the UTM parameters of the link: parameter key => value.
*
* @var array<string, string>
*/
private array $utmParameters = [];
/**
* The landing page of a Mautic redirect (null for external ones).
*/
private ?Page $page = null;
/**
* The standalone form of a Mautic redirect (null for external ones).
*/
private ?Form $form = null;
/**
* The UTM Taxonomy of the link: every UTM value is chosen among its allowed values.
* Null only on links that predate the feature; the form then demands one.
*/
private ?UtmTaxonomy $taxonomy = null;
public static function loadMetadata(OrmClassMetadata $metadata): void
{
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(self::TABLE_NAME)
->setCustomRepositoryClass(SmartLinkRepository::class)
->addIndex(['slug'], 'smart_link_slug');
$builder->addId();
$builder->createField('slug', Types::STRING)
->length(self::SLUG_MAX_LENGTH)
->unique()
->build();
// Mapped on the raw metadata: ClassMetadataBuilder::addField() forces VARCHAR(191) on strings
// and doesn't accept enum types.
$metadata->mapField([
'fieldName' => 'redirectType',
'columnName'=> 'redirect_type',
'type' => Types::STRING,
'length' => 20,
'enumType' => SmartLinkRedirectType::class,
'options' => ['default' => SmartLinkRedirectType::External->value],
]);
$metadata->mapField([
'fieldName' => 'redirectCode',
'columnName'=> 'redirect_code',
'type' => Types::INTEGER,
'enumType' => SmartLinkRedirectCode::class,
'options' => ['default' => SmartLinkRedirectCode::Found->value],
]);
$metadata->mapField([
'fieldName' => 'destinationUrl',
'columnName'=> 'destination_url',
'type' => Types::TEXT,
'nullable' => true,
]);
$metadata->mapField([
'fieldName' => 'utmParameters',
'columnName'=> 'utm_parameters',
'type' => Types::JSON,
]);
// Mautic destinations: a landing page or a standalone form. Deleting the page or the
// form keeps the link (the column becomes null, the link then 404s until reconfigured).
$builder->createManyToOne('page', Page::class)
->addJoinColumn('page_id', 'id', true, false, 'SET NULL')
->build();
$builder->createManyToOne('form', Form::class)
->addJoinColumn('form_id', 'id', true, false, 'SET NULL')
->build();
// The UTM Taxonomy of the link. Deleting the taxonomy keeps the link (the column becomes
// null): it keeps redirecting with the values it already carries, and its next edit
// demands a new taxonomy.
$builder->createManyToOne('taxonomy', UtmTaxonomy::class)
->addJoinColumn('utm_taxonomy_id', 'id', true, false, 'SET NULL')
->build();
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('slug', new NotBlank(message: 'mt3s.smart_links.smart_links.error.slug_required'));
$metadata->addPropertyConstraint('slug', new Length(
max: self::SLUG_MAX_LENGTH,
maxMessage: 'mt3s.smart_links.smart_links.error.slug_length',
));
}
public function getId(): ?int
{
return $this->id;
}
/**
* Core's standard list actions and flash messages call getName(); the slug is the
* link's name.
*/
public function getName(): string
{
return (string) $this->slug;
}
/**
* The short link, for example https://lnk.example.com/l/ab3x. Empty when no domain is set.
*/
public function getShortLink(string $domain): string
{
$domain = trim((string) $domain);
return '' === $domain || '' === (string) $this->slug ? '' : 'https://'.rtrim($domain, '/').'/l/'.$this->slug;
}
public function getSlug(): ?string
{
return $this->slug;
}
/**
* The slug of a saved link is immutable: once set at creation time, it can't change.
*/
public function setSlug(?string $slug): static
{
$immutable = null !== $this->id && null !== $this->slug && '' !== $this->slug;
if ($immutable && null !== $slug && $slug !== $this->slug) {
throw new \LogicException('The slug of a saved smart link cannot be changed.');
}
$this->isChanged('slug', $slug);
$this->slug = $slug;
return $this;
}
public function getRedirectType(): SmartLinkRedirectType
{
return $this->redirectType;
}
public function setRedirectType(SmartLinkRedirectType $redirectType): static
{
if ($redirectType !== $this->redirectType) {
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('redirectType', [$this->redirectType->value, $redirectType->value]);
$this->redirectType = $redirectType;
}
// The destination only exists for external redirects.
if ($redirectType->isMautic()) {
$this->setDestinationUrl(null);
}
return $this;
}
public function getRedirectCode(): SmartLinkRedirectCode
{
return $this->redirectCode;
}
public function setRedirectCode(SmartLinkRedirectCode $redirectCode): static
{
if ($redirectCode !== $this->redirectCode) {
$this->addChange('redirectCode', [$this->redirectCode->value, $redirectCode->value]);
$this->redirectCode = $redirectCode;
}
return $this;
}
public function getDestinationUrl(): ?string
{
return $this->destinationUrl;
}
public function setDestinationUrl(?string $destinationUrl): static
{
if (null !== $destinationUrl && '' === trim($destinationUrl)) {
$destinationUrl = null;
}
$this->isChanged('destinationUrl', $destinationUrl);
$this->destinationUrl = $destinationUrl;
return $this;
}
/**
* @return array<string, string>
*/
public function getUtmParameters(): array
{
return $this->utmParameters;
}
/**
* @param array<string, string>|null $utmParameters
*/
public function setUtmParameters(?array $utmParameters): static
{
$this->isChanged('utmParameters', $utmParameters);
$this->utmParameters = $utmParameters ?? [];
return $this;
}
public function getPage(): ?Page
{
return $this->page;
}
public function setPage(?Page $page): static
{
$this->isChanged('page', $page);
$this->page = $page;
return $this;
}
public function getForm(): ?Form
{
return $this->form;
}
public function setForm(?Form $form): static
{
$this->isChanged('form', $form);
$this->form = $form;
return $this;
}
public function getTaxonomy(): ?UtmTaxonomy
{
return $this->taxonomy;
}
public function setTaxonomy(?UtmTaxonomy $taxonomy): static
{
$this->isChanged('taxonomy', $taxonomy);
$this->taxonomy = $taxonomy;
return $this;
}
/**
* The title of the destination, whatever its type: the slug for external links, the page
* title or the form name for Mautic ones. Used by the list and the flash messages.
*/
public function getDestinationLabel(): string
{
if ($this->redirectType->isMautic()) {
return $this->page?->getTitle() ?? $this->form?->getName() ?? '';
}
return (string) ($this->destinationUrl ?? '');
}
}