value. * * @var array */ 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 */ public function getUtmParameters(): array { return $this->utmParameters; } /** * @param array|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 ?? ''); } }