Files
kupshop/bundles/KupShop/BonusProgramBundle/Email/BonusPointsExpiryEmail.php
2025-08-02 16:30:27 +02:00

98 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\BonusProgramBundle\Email;
use KupShop\BonusProgramBundle\Utils\BonusProvider;
use KupShop\KupShopBundle\Email\BaseEmail;
use Symfony\Contracts\Service\Attribute\Required;
class BonusPointsExpiryEmail extends BaseEmail
{
private const PLACEHOLDER_POINTS_TOTAL = 'BODY_CELKEM';
private const PLACEHOLDER_EXPIRY_DATE = 'DATUM_EXPIRACE_BODU';
private const PLACEHOLDER_REMOVE_TIME = 'PLATNOST_BODU';
/**
* @deprecated use PLACEHOLDER_POINTS_TOTAL
*/
private const PLACEHOLDER_POINTS_LOST = 'ZTRACENE_BODY';
protected static $type = 'BONUS_PROGRAM_POINTS_EXPIRY';
protected static $priority = 1;
protected $subject = 'Expirace bodů';
protected $template = 'email/email_bonus_points_expiry.tpl';
protected $defaultEnabled = 'Y';
private BonusProvider $bonusProvider;
private \User $user;
public static function getPlaceholders(): array
{
$placeholders = [
self::PLACEHOLDER_POINTS_TOTAL => [
'text' => translate('bonusProgramPointsRemaining', 'emails'),
],
self::PLACEHOLDER_EXPIRY_DATE => [
'text' => translate('bonusProgramExpiryDate', 'emails'),
],
self::PLACEHOLDER_REMOVE_TIME => [
'text' => translate('bonusProgramNullPoints', 'emails'),
],
];
return [self::$type => $placeholders] + (parent::getPlaceholders() ?? []);
}
public function replacePlaceholdersItem($placeholder)
{
$expiryTime = \Settings::getDefault()->bonus_program['expiry_email_time'] ?? '0';
$expiryTime = $expiryTime != '' ? $expiryTime : '0';
$expiryInterval = new \DateInterval("P{$expiryTime}D");
return match ($placeholder) {
self::PLACEHOLDER_POINTS_TOTAL, self::PLACEHOLDER_POINTS_LOST => $this->bonusProvider->getActivePointsAmount($this->user),
self::PLACEHOLDER_EXPIRY_DATE => (new \DateTimeImmutable())->add($expiryInterval)->format('d.m.Y'),
self::PLACEHOLDER_REMOVE_TIME => $expiryTime,
default => parent::replacePlaceholdersItem($placeholder),
};
}
public function getEmail($replacements = []): array
{
return parent::getEmail($replacements) + ['to' => $this->user->email];
}
public function setUser(\User $user): self
{
$this->user = $user;
return $this;
}
#[Required]
final public function setBonusProvider(BonusProvider $bonusProvider): void
{
$this->bonusProvider = $bonusProvider;
}
public function testEmail(): array
{
$id = sqlQueryBuilder()
->select('id')
->from('users')
->setMaxResults(1)
->execute()->fetchFirstColumn();
$this->user = \User::createFromId($id[0]);
return parent::testEmail();
}
public function getUser(): \User
{
return $this->user;
}
}