Skip to content

Commit

Permalink
fix Haste error
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Aug 13, 2024
1 parent 8ab8494 commit 00ca050
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
26 changes: 22 additions & 4 deletions src/Gateway/KlickTippGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Fenepedia\ContaoKlickTippGateway\Gateway;

use Codefog\HasteBundle\StringParser;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\StringUtil;
use Contao\Validator;
use Fenepedia\ContaoKlickTippGateway\Config\KlickTippConfig;
use Fenepedia\ContaoKlickTippGateway\Exception\KlickTippGatewayException;
use Fenepedia\ContaoKlickTippGateway\Parcel\Stamp\KlickTippConfigStamp;
use Fenepedia\ContaoKlickTippGateway\Util;
use Haste\Util\StringUtil as HasteStringUtil;
use Kazin8\KlickTipp\Connector;
use Psr\Log\LoggerInterface;
use Terminal42\NotificationCenterBundle\Exception\Parcel\CouldNotDeliverParcelException;
Expand All @@ -29,6 +30,7 @@ public function __construct(
private readonly Connector $connector,
private readonly LoggerInterface $contaoGeneralLogger,
private readonly LoggerInterface $contaoErrorLogger,
private readonly StringParser|null $stringParser = null,
) {
}

Expand Down Expand Up @@ -75,7 +77,7 @@ private function createKlickTippConfigStamp(Parcel $parcel): KlickTippConfigStam

$messageConfig = $parcel->getMessageConfig();
$tokens = $parcel->getStamp(TokenCollectionStamp::class)->tokenCollection->forSimpleTokenParser();
$email = Util::recursiveReplaceTokensAndTags($messageConfig->getString('kt_email'), $tokens);
$email = $this->recursiveReplaceTokensAndTags($messageConfig->getString('kt_email'), $tokens);

if (!$email || !Validator::isEmail($email)) {
throw new KlickTippGatewayException('Invalid email address "'.$email.'" given.');
Expand All @@ -84,8 +86,8 @@ private function createKlickTippConfigStamp(Parcel $parcel): KlickTippConfigStam
$parameters = [];

foreach (StringUtil::deserialize($messageConfig->getString('kt_parameters'), true) as $param) {
$key = Util::recursiveReplaceTokensAndTags((string) $param['key'], $tokens);
$value = Util::recursiveReplaceTokensAndTags((string) $param['value'], $tokens);
$key = $this->recursiveReplaceTokensAndTags((string) $param['key'], $tokens);
$value = $this->recursiveReplaceTokensAndTags((string) $param['value'], $tokens);

// Do some type casting
if (is_numeric($value)) {
Expand Down Expand Up @@ -203,4 +205,20 @@ private function getTagId(string $tag): string|null

return $tagId;
}

/**
* Compatibility layer to support both codefog/contao-haste v4 and v5.
*/
private function recursiveReplaceTokensAndTags(string $text, array $tokens, int $textFlags = 0): string
{
if ($this->stringParser) {
return $this->stringParser->recursiveReplaceTokensAndTags($text, $tokens, $textFlags);
}

if (class_exists(HasteStringUtil::class)) {
return HasteStringUtil::recursiveReplaceTokensAndTags($text, $tokens, $textFlags);
}

throw new \RuntimeException('Cannot replace tokens and tags due to missing dependencies.');
}
}
31 changes: 0 additions & 31 deletions src/Util.php

This file was deleted.

0 comments on commit 00ca050

Please sign in to comment.