refactor(admin): Migrate AdminAjaxHandler to Clean Architecture
- Move AdminAjaxHandler to Admin/Shared/Infrastructure/Api/Wordpress/ - Create FieldMapperInterface for decentralized field mapping - Create FieldMapperRegistry for module discovery - Create FieldMapperProvider for auto-registration of 12 mappers - Add FieldMappers for all components: - ContactFormFieldMapper (46 fields) - CtaBoxSidebarFieldMapper (32 fields) - CtaLetsTalkFieldMapper - CtaPostFieldMapper - FeaturedImageFieldMapper (15 fields) - FooterFieldMapper (31 fields) - HeroFieldMapper - NavbarFieldMapper - RelatedPostFieldMapper (34 fields) - SocialShareFieldMapper - TableOfContentsFieldMapper - TopNotificationBarFieldMapper (17 fields) - Update functions.php bootstrap with FieldMapperProvider - AdminAjaxHandler reduced from ~700 to 145 lines - Follows SRP, OCP, DIP principles BACKUP BEFORE: Removing CTA A/B Testing legacy system 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Admin\Shared\Infrastructure\FieldMapping;
|
||||
|
||||
use ROITheme\Admin\Shared\Domain\Contracts\FieldMapperInterface;
|
||||
|
||||
/**
|
||||
* Provider para auto-registro de Field Mappers
|
||||
*
|
||||
* RESPONSABILIDAD:
|
||||
* - Descubrir automaticamente FieldMappers en cada modulo
|
||||
* - Registrarlos en el FieldMapperRegistry
|
||||
*
|
||||
* BENEFICIO:
|
||||
* - Agregar nuevo componente = crear FieldMapper (sin tocar functions.php)
|
||||
* - Eliminar componente = borrar carpeta (limpieza automatica)
|
||||
*/
|
||||
final class FieldMapperProvider
|
||||
{
|
||||
private const MODULES = [
|
||||
'TopNotificationBar',
|
||||
'Navbar',
|
||||
'CtaLetsTalk',
|
||||
'Hero',
|
||||
'FeaturedImage',
|
||||
'TableOfContents',
|
||||
'CtaBoxSidebar',
|
||||
'SocialShare',
|
||||
'CtaPost',
|
||||
'RelatedPost',
|
||||
'ContactForm',
|
||||
'Footer',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly FieldMapperRegistry $registry
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Registra todos los FieldMappers disponibles
|
||||
*/
|
||||
public function registerAll(): void
|
||||
{
|
||||
foreach (self::MODULES as $module) {
|
||||
$this->registerIfExists($module);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registra un mapper si existe la clase
|
||||
*/
|
||||
private function registerIfExists(string $module): void
|
||||
{
|
||||
$className = sprintf(
|
||||
'ROITheme\\Admin\\%s\\Infrastructure\\FieldMapping\\%sFieldMapper',
|
||||
$module,
|
||||
$module
|
||||
);
|
||||
|
||||
if (class_exists($className)) {
|
||||
$mapper = new $className();
|
||||
if ($mapper instanceof FieldMapperInterface) {
|
||||
$this->registry->register($mapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Admin\Shared\Infrastructure\FieldMapping;
|
||||
|
||||
use ROITheme\Admin\Shared\Domain\Contracts\FieldMapperInterface;
|
||||
|
||||
/**
|
||||
* Registro central de Field Mappers
|
||||
*
|
||||
* RESPONSABILIDAD:
|
||||
* - Registrar mappers de cada modulo
|
||||
* - Resolver mapper por nombre de componente
|
||||
*
|
||||
* PRINCIPIOS:
|
||||
* - OCP: Nuevos mappers se registran sin modificar esta clase
|
||||
* - SRP: Solo gestiona el registro, no contiene mapeos
|
||||
*/
|
||||
final class FieldMapperRegistry
|
||||
{
|
||||
/** @var array<string, FieldMapperInterface> */
|
||||
private array $mappers = [];
|
||||
|
||||
/**
|
||||
* Registra un mapper
|
||||
*/
|
||||
public function register(FieldMapperInterface $mapper): void
|
||||
{
|
||||
$this->mappers[$mapper->getComponentName()] = $mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene un mapper por nombre de componente
|
||||
*
|
||||
* @throws \InvalidArgumentException Si no existe mapper para el componente
|
||||
*/
|
||||
public function getMapper(string $componentName): FieldMapperInterface
|
||||
{
|
||||
if (!isset($this->mappers[$componentName])) {
|
||||
throw new \InvalidArgumentException(
|
||||
"No field mapper registered for component: {$componentName}"
|
||||
);
|
||||
}
|
||||
|
||||
return $this->mappers[$componentName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica si existe mapper para un componente
|
||||
*/
|
||||
public function hasMapper(string $componentName): bool
|
||||
{
|
||||
return isset($this->mappers[$componentName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene todos los mappers registrados
|
||||
*
|
||||
* @return array<string, FieldMapperInterface>
|
||||
*/
|
||||
public function getAllMappers(): array
|
||||
{
|
||||
return $this->mappers;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user