- 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>
83 lines
2.0 KiB
PHP
83 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace ROITheme\Public\AdsensePlacement\Domain\ValueObjects;
|
|
|
|
/**
|
|
* Value Object que encapsula el contexto del usuario para decisiones de visibilidad.
|
|
*
|
|
* Inmutable despues de construccion. Sin dependencias de WordPress.
|
|
*
|
|
* @package ROITheme\Public\AdsensePlacement\Domain\ValueObjects
|
|
*/
|
|
final class UserContext
|
|
{
|
|
/**
|
|
* @param bool $isLoggedIn Si el usuario tiene sesion activa
|
|
* @param bool $isMobile Si el dispositivo es movil (viewport < 992px)
|
|
* @param array<int> $userRoles IDs de roles del usuario
|
|
*/
|
|
public function __construct(
|
|
private bool $isLoggedIn,
|
|
private bool $isMobile,
|
|
private array $userRoles = []
|
|
) {
|
|
}
|
|
|
|
public function isLoggedIn(): bool
|
|
{
|
|
return $this->isLoggedIn;
|
|
}
|
|
|
|
public function isMobile(): bool
|
|
{
|
|
return $this->isMobile;
|
|
}
|
|
|
|
public function isDesktop(): bool
|
|
{
|
|
return !$this->isMobile;
|
|
}
|
|
|
|
/**
|
|
* @return array<int>
|
|
*/
|
|
public function getUserRoles(): array
|
|
{
|
|
return $this->userRoles;
|
|
}
|
|
|
|
public function hasRole(int $roleId): bool
|
|
{
|
|
return in_array($roleId, $this->userRoles, true);
|
|
}
|
|
|
|
/**
|
|
* Crea instancia desde array (para deserializacion).
|
|
*
|
|
* @param array{is_logged_in: bool, is_mobile: bool, user_roles?: array<int>} $data
|
|
*/
|
|
public static function fromArray(array $data): self
|
|
{
|
|
return new self(
|
|
isLoggedIn: $data['is_logged_in'] ?? false,
|
|
isMobile: $data['is_mobile'] ?? false,
|
|
userRoles: $data['user_roles'] ?? []
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Serializa a array para respuesta JSON.
|
|
*
|
|
* @return array{is_logged_in: bool, is_mobile: bool, user_roles: array<int>}
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'is_logged_in' => $this->isLoggedIn,
|
|
'is_mobile' => $this->isMobile,
|
|
'user_roles' => $this->userRoles,
|
|
];
|
|
}
|
|
}
|