|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Domain\Configuration\Service; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Configuration\Model\ConfigOption; |
| 8 | +use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider; |
| 9 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeValueRepository; |
| 10 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository; |
| 11 | +use PhpList\Core\Domain\Subscription\Service\Resolver\AttributeValueResolver; |
| 12 | + |
| 13 | +class UserPersonalizer |
| 14 | +{ |
| 15 | + private const PHP_SPACE = ' '; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + private readonly ConfigProvider $config, |
| 19 | + private readonly LegacyUrlBuilder $urlBuilder, |
| 20 | + private readonly SubscriberRepository $subscriberRepository, |
| 21 | + private readonly SubscriberAttributeValueRepository $attributesRepository, |
| 22 | + private readonly AttributeValueResolver $attributeValueResolver |
| 23 | + ) {} |
| 24 | + |
| 25 | + public function personalize(string $value, string $email): string |
| 26 | + { |
| 27 | + $user = $this->subscriberRepository->findOneByEmail($email); |
| 28 | + if (!$user) { |
| 29 | + return $value; |
| 30 | + } |
| 31 | + |
| 32 | + $resolver = new PlaceholderResolver(); |
| 33 | + $resolver->register('EMAIL', fn() => $user->getEmail()); |
| 34 | + |
| 35 | + $resolver->register('UNSUBSCRIBEURL', function () use ($user) { |
| 36 | + $base = $this->config->getValue(ConfigOption::UnsubscribeUrl) ?? ''; |
| 37 | + return $this->urlBuilder->withUid($base, $user->getUniqueId()) . self::PHP_SPACE; |
| 38 | + }); |
| 39 | + |
| 40 | + $resolver->register('CONFIRMATIONURL', function () use ($user) { |
| 41 | + $base = $this->config->getValue(ConfigOption::ConfirmationUrl) ?? ''; |
| 42 | + return $this->urlBuilder->withUid($base, $user->getUniqueId()) . self::PHP_SPACE; |
| 43 | + }); |
| 44 | + $resolver->register('PREFERENCESURL', function () use ($user) { |
| 45 | + $base = $this->config->getValue(ConfigOption::PreferencesUrl) ?? ''; |
| 46 | + return $this->urlBuilder->withUid($base, $user->getUniqueId()) . self::PHP_SPACE; |
| 47 | + }); |
| 48 | + |
| 49 | + $resolver->register( |
| 50 | + 'SUBSCRIBEURL', |
| 51 | + fn() => ($this->config->getValue(ConfigOption::SubscribeUrl) ?? '') . self::PHP_SPACE |
| 52 | + ); |
| 53 | + $resolver->register('DOMAIN', fn() => $this->config->getValue(ConfigOption::Domain) ?? ''); |
| 54 | + $resolver->register('WEBSITE', fn() => $this->config->getValue(ConfigOption::Website) ?? ''); |
| 55 | + |
| 56 | + $userAttributes = $this->attributesRepository->getForSubscriber($user); |
| 57 | + foreach ($userAttributes as $userAttribute) { |
| 58 | + $resolver->register( |
| 59 | + strtoupper($userAttribute->getAttributeDefinition()->getName()), |
| 60 | + fn() => $this->attributeValueResolver->resolve($userAttribute) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + $out = $resolver->resolve($value); |
| 65 | + |
| 66 | + return (string) $out; |
| 67 | + } |
| 68 | +} |
0 commit comments