WizdomWeb/app/Services/UnsubscribeTokenService.php

52 lines
1.3 KiB
PHP

<?php
/**
* File: UnsubscribeTokenService.php
* Version: 1.0
* Path: app/Services/
* Purpose: Wrapper for generating and validating unsubscribe tokens using TokenService.
*/
namespace WizdomNetworks\WizeWeb\Services;
use WizdomNetworks\WizeWeb\Services\TokenService;
class UnsubscribeTokenService
{
private TokenService $tokenService;
private string $secret;
private int $ttl;
public function __construct(TokenService $tokenService)
{
$this->tokenService = $tokenService;
$this->secret = $_ENV['UNSUBSCRIBE_SECRET'] ?? 'changeme';
$this->ttl = 86400; // default: 24 hours
}
/**
* Create an unsubscribe token.
*
* @param string $email
* @param int $timestamp
* @return string
*/
public function generate(string $email, int $timestamp): string
{
return $this->tokenService->generate($email . $timestamp, $this->secret);
}
/**
* Validate an unsubscribe token.
*
* @param string $email
* @param int $timestamp
* @param string $token
* @return bool
*/
public function isValid(string $email, int $timestamp, string $token): bool
{
$data = $email . $timestamp;
return $this->tokenService->isValid($data, $token, $this->secret, $timestamp, $this->ttl);
}
}