← All changes
|
backend/src/Factory/Security/CaptchaServiceFactory.php
+80
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,80 @@ | ||
| 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 | +use IvyForms\Services\Security\Common\CaptchaServiceInterface; | |
| 11 | +use IvyForms\Services\Security\HCaptcha\HCaptchaService; | |
| 12 | +use IvyForms\Services\Security\Recaptcha\RecaptchaService; | |
| 13 | +use IvyForms\Services\Security\Turnstile\TurnstileService; | |
| 14 | +use IvyForms\Services\Settings\SettingsService; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Simple factory for creating CAPTCHA service instances | |
| 18 | + * | |
| 19 | + * Focused on just creating service instances without caching or business logic. | |
| 20 | + * Uses composition with Registry and Resolver for advanced functionality. | |
| 21 | + * | |
| 22 | + * @package IvyForms\Factory\Security | |
| 23 | + */ | |
| 24 | +class CaptchaServiceFactory | |
| 25 | +{ | |
| 26 | + private SettingsService $settingsService; | |
| 27 | + | |
| 28 | + public function __construct(SettingsService $settingsService) | |
| 29 | + { | |
| 30 | + $this->settingsService = $settingsService; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Create a CAPTCHA service instance for the given provider | |
| 35 | + * | |
| 36 | + * @param string $provider The CAPTCHA provider | |
| 37 | + * @return CaptchaServiceInterface|null | |
| 38 | + */ | |
| 39 | + public function create(string $provider): ?CaptchaServiceInterface | |
| 40 | + { | |
| 41 | + switch ($provider) { | |
| 42 | + case CaptchaProviderResolver::PROVIDER_RECAPTCHA: | |
| 43 | + return new RecaptchaService($this->settingsService); | |
| 44 | + | |
| 45 | + case CaptchaProviderResolver::PROVIDER_HCAPTCHA: | |
| 46 | + return new HCaptchaService($this->settingsService); | |
| 47 | + | |
| 48 | + case CaptchaProviderResolver::PROVIDER_TURNSTILE: | |
| 49 | + return new TurnstileService($this->settingsService); | |
| 50 | + | |
| 51 | + default: | |
| 52 | + return null; | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Get all supported CAPTCHA providers | |
| 58 | + * | |
| 59 | + * @return array<string> | |
| 60 | + */ | |
| 61 | + public function getSupportedProviders(): array | |
| 62 | + { | |
| 63 | + return [ | |
| 64 | + CaptchaProviderResolver::PROVIDER_RECAPTCHA, | |
| 65 | + CaptchaProviderResolver::PROVIDER_HCAPTCHA, | |
| 66 | + CaptchaProviderResolver::PROVIDER_TURNSTILE, | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Check if a provider is supported (has implementation) | |
| 72 | + * | |
| 73 | + * @param string $provider | |
| 74 | + * @return bool | |
| 75 | + */ | |
| 76 | + public function isSupported(string $provider): bool | |
| 77 | + { | |
| 78 | + return $this->create($provider) !== null; | |
| 79 | + } | |
| 80 | +} | |