- Add REST endpoint GET /roi-theme/v1/adsense-placement/visibility - Add Domain layer: UserContext, VisibilityDecision, AdsenseSettings VOs - Add Application layer: CheckAdsenseVisibilityUseCase - Add Infrastructure: AdsenseVisibilityChecker, Controller, Enqueuer - Add JavaScript controller with localStorage caching - Add test plan for production validation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace ROITheme\Public\AdsensePlacement\Domain\ValueObjects;
|
|
|
|
/**
|
|
* Value Object que representa la decision de mostrar o no anuncios.
|
|
*
|
|
* Inmutable despues de construccion. Sin dependencias de WordPress.
|
|
* El timestamp se inyecta en toArray() para mantener Domain puro.
|
|
*
|
|
* @package ROITheme\Public\AdsensePlacement\Domain\ValueObjects
|
|
*/
|
|
final class VisibilityDecision
|
|
{
|
|
private const DEFAULT_CACHE_SECONDS = 300; // 5 minutos
|
|
|
|
/**
|
|
* @param bool $showAds Si se deben mostrar los anuncios
|
|
* @param array<string> $reasons Razones de la decision (para debugging)
|
|
* @param int $cacheSeconds Tiempo de cache en segundos
|
|
*/
|
|
public function __construct(
|
|
private bool $showAds,
|
|
private array $reasons = [],
|
|
private int $cacheSeconds = self::DEFAULT_CACHE_SECONDS
|
|
) {
|
|
}
|
|
|
|
public function shouldShowAds(): bool
|
|
{
|
|
return $this->showAds;
|
|
}
|
|
|
|
/**
|
|
* @return array<string>
|
|
*/
|
|
public function getReasons(): array
|
|
{
|
|
return $this->reasons;
|
|
}
|
|
|
|
public function getCacheSeconds(): int
|
|
{
|
|
return $this->cacheSeconds;
|
|
}
|
|
|
|
/**
|
|
* Factory: Crea decision positiva (mostrar ads).
|
|
*/
|
|
public static function show(int $cacheSeconds = self::DEFAULT_CACHE_SECONDS): self
|
|
{
|
|
return new self(
|
|
showAds: true,
|
|
reasons: ['all_conditions_passed'],
|
|
cacheSeconds: $cacheSeconds
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Factory: Crea decision negativa (ocultar ads).
|
|
*
|
|
* @param array<string> $reasons
|
|
*/
|
|
public static function hide(array $reasons, int $cacheSeconds = self::DEFAULT_CACHE_SECONDS): self
|
|
{
|
|
return new self(
|
|
showAds: false,
|
|
reasons: $reasons,
|
|
cacheSeconds: $cacheSeconds
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Serializa a array para respuesta JSON.
|
|
*
|
|
* El timestamp se inyecta aqui (no en constructor) para mantener Domain puro.
|
|
*
|
|
* @param int $timestamp Unix timestamp actual
|
|
* @return array{show_ads: bool, reasons: array<string>, cache_seconds: int, timestamp: int}
|
|
*/
|
|
public function toArray(int $timestamp): array
|
|
{
|
|
return [
|
|
'show_ads' => $this->showAds,
|
|
'reasons' => $this->reasons,
|
|
'cache_seconds' => $this->cacheSeconds,
|
|
'timestamp' => $timestamp,
|
|
];
|
|
}
|
|
}
|