| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Factory\Security; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Services\Security\CaptchaServiceInterface; |
| 16 |
use IvyForms\Services\Security\RecaptchaService; |
| 17 |
use IvyForms\Services\Settings\SettingsService; |
| 18 |
|
| 19 |
/** |
| 20 |
* Simple factory for creating CAPTCHA service instances |
| 21 |
* |
| 22 |
* Focused on just creating service instances without caching or business logic. |
| 23 |
* Uses composition with Registry and Resolver for advanced functionality. |
| 24 |
* |
| 25 |
* @package IvyForms\Factory\Security |
| 26 |
*/ |
| 27 |
class CaptchaServiceFactory |
| 28 |
{ |
| 29 |
private SettingsService $settingsService; |
| 30 |
|
| 31 |
public function __construct(SettingsService $settingsService) |
| 32 |
{ |
| 33 |
$this->settingsService = $settingsService; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Create a CAPTCHA service instance for the given provider |
| 38 |
* |
| 39 |
* @param string $provider The CAPTCHA provider |
| 40 |
* @return CaptchaServiceInterface|null |
| 41 |
*/ |
| 42 |
public function create(string $provider): ?CaptchaServiceInterface |
| 43 |
{ |
| 44 |
switch ($provider) { |
| 45 |
case CaptchaProviderResolver::PROVIDER_RECAPTCHA: |
| 46 |
return new RecaptchaService($this->settingsService); |
| 47 |
|
| 48 |
case CaptchaProviderResolver::PROVIDER_HCAPTCHA: |
| 49 |
// Future implementation for hCaptcha |
| 50 |
// return new HCaptchaService($this->settingsService); |
| 51 |
return null; |
| 52 |
|
| 53 |
case CaptchaProviderResolver::PROVIDER_TURNSTILE: |
| 54 |
// Future implementation for Cloudflare Turnstile |
| 55 |
// return new TurnstileService($this->settingsService); |
| 56 |
return null; |
| 57 |
|
| 58 |
default: |
| 59 |
return null; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get all supported CAPTCHA providers |
| 65 |
* |
| 66 |
* @return array<string> |
| 67 |
*/ |
| 68 |
public function getSupportedProviders(): array |
| 69 |
{ |
| 70 |
return [ |
| 71 |
CaptchaProviderResolver::PROVIDER_RECAPTCHA, |
| 72 |
CaptchaProviderResolver::PROVIDER_HCAPTCHA, |
| 73 |
CaptchaProviderResolver::PROVIDER_TURNSTILE, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check if a provider is supported (has implementation) |
| 79 |
* |
| 80 |
* @param string $provider |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function isSupported(string $provider): bool |
| 84 |
{ |
| 85 |
return $this->create($provider) !== null; |
| 86 |
} |
| 87 |
} |
| 88 |
|