Files
mautic-marketing-program-bu…/Service/SmartLinkShortUrlBuilder.php
T
Alexandre Gain dce849610c Merge the Smartlinks plugin (SmartlinksBundle 0.6.0) into the bundle as the Smart Links domain (0.18.0)
Two domains, one MT3S bundle: Marketing Programs (unchanged) and the
imported Smart Links (smart links + UTM taxonomies), each keeping its
own menu, tables, routes and translations.

- Code moved from MauticPlugin\SmartlinksBundle\ to
  MauticPlugin\MT3SMarketingProgramBundle\; template namespace
  @Smartlinks/ -> @MT3SMarketingProgram/; smartlinks.js ->
  Assets/js/mt3s-mp-smartlinks.js; icon -> Assets/img/smartlinks-icon.svg.
- Incoming migrations renamed Version_0_X_0_SmartLinks.php to avoid
  the class-name collision; schema untouched, data intact.
- Permissions folded into MT3SMarketingProgramPermissions:
  smart_links + taxonomies under the mt3smarketingprogram: base
  (roles that had the old smartlinks: grants must be re-granted).
- config.php: Smart Links routes + menu group, public /l/{slug},
  SmartlinksIntegration services, smartlinks_* parameters (legacy
  utm_manager_* keys still readable), version 0.18.0. The 0.17.0
  placeholder /s/marketing-program-utm-taxonomies is replaced by the
  real /s/utm-taxonomies.
- Translations merged (en_US) and added (fr_FR); Tests/ + phpunit moved
  in; readme documents both domains and the 0.18.0 merge history.

Verified live: menus, both config tabs, both lists + forms, program
pages, public redirect with taxonomy UTM values, host + unknown-slug
404s, 90/90 unit tests, full php -l, all ini catalogs parse.
2026-11-16 13:48:13 +01:00

71 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink;
use MauticPlugin\MT3SMarketingProgramBundle\Form\Type\SmartLinksConfigType;
/**
* Builds the public form of a Smart Link: https://{short_link_domain}/l/{slug}.
* The domain is set in Settings > Configuration > Smart Links Settings; the form shows it
* as a reminder (getShortLinkDomain(), without the scheme).
*/
final class SmartLinkShortUrlBuilder
{
public const PATH_PREFIX = '/l/';
public function __construct(
private readonly CoreParametersHelper $coreParametersHelper,
) {
}
/**
* The configured short link domain, without scheme or trailing slash, for example lnk.example.com.
* Empty when nothing is configured yet.
*/
public function getShortLinkDomain(): string
{
$stored = $this->coreParametersHelper->get(SmartLinksConfigType::CONFIG_PARAMETER, []);
// Upgrades from < 0.5.0 keep the domain in local.php under the legacy key; serve it until
// the Configuration page is saved again (which writes the new key).
if (!is_array($stored) || '' === trim((string) ($stored['short_link_domain'] ?? ''))) {
$legacy = $this->coreParametersHelper->get(SmartLinksConfigType::LEGACY_CONFIG_PARAMETER, []);
if (is_array($legacy) && '' !== trim((string) ($legacy['short_link_domain'] ?? ''))) {
$stored = $legacy;
}
}
$domain = trim(is_array($stored) ? (string) ($stored['short_link_domain'] ?? '') : '');
return '' === $domain ? '' : rtrim($domain, '/');
}
/**
* The short link of a Smart Link, for example https://lnk.example.com/l/ab3x.
* Empty when no domain is configured, or when the link has no slug yet.
*/
public function buildShortUrl(SmartLink $smartLink): string
{
return $this->buildShortUrlFromSlug((string) ($smartLink->getSlug() ?? ''));
}
/**
* Same as buildShortUrl(), for a slug that was just typed in the form (live preview).
*/
public function buildShortUrlFromSlug(string $slug): string
{
$slug = trim($slug);
$domain = $this->getShortLinkDomain();
if ('' === $domain || '' === $slug) {
return '';
}
return 'https://'.$domain.self::PATH_PREFIX.$slug;
}
}