PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Factory / Security / SecurityServiceFactory.php

SecurityServiceFactory.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Factory/Security/SecurityServiceFactory.php

168 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Common\Exceptions\ForbiddenException;
11 use IvyForms\Services\Security\Common\CaptchaServiceInterface;
12 use IvyForms\Services\Settings\SettingsService;
13
14 /**
15 * Main facade for security service management
16 *
17 * Provides a simplified interface for creating and managing CAPTCHA services
18 * while delegating specific responsibilities to specialized classes.
19 *
20 * @package IvyForms\Factory\Security
21 */
22 class SecurityServiceFactory
23 {
24 private CaptchaServiceFactory $factory;
25
26 private CaptchaProviderRegistry $registry;
27
28 private CaptchaProviderResolver $resolver;
29
30 public function __construct(SettingsService $settingsService)
31 {
32 $this->factory = new CaptchaServiceFactory($settingsService);
33 $this->registry = new CaptchaProviderRegistry();
34 $this->resolver = new CaptchaProviderResolver($settingsService);
35 }
36
37 /**
38 * Get the active CAPTCHA provider from settings
39 *
40 * Note: This returns the first configured provider, but actual usage
41 * depends on which CAPTCHA field is present in the specific form.
42 *
43 * @return string
44 */
45 public function getActiveCaptchaProvider(): string
46 {
47 return $this->resolver->getActiveProvider();
48 }
49
50 /**
51 * Determine CAPTCHA provider based on form fields and current settings
52 *
53 * @param array<object> $formFields Array of form field objects
54 * @return string The CAPTCHA provider to use for this form
55 */
56 public function getCaptchaProviderForForm(array $formFields): string
57 {
58 return $this->resolver->resolveProviderForForm($formFields);
59 }
60
61 /**
62 * Create the appropriate CAPTCHA service based on active provider
63 *
64 * @param array<object>|null $formFields Optional form fields to determine provider
65 * @return CaptchaServiceInterface|null Returns the service instance or null if no provider is active
66 */
67 public function createActiveCaptchaService(?array $formFields = null): ?CaptchaServiceInterface
68 {
69 $provider = ($formFields !== null)
70 ? $this->getCaptchaProviderForForm($formFields)
71 : $this->getActiveCaptchaProvider();
72
73 if ($provider === CaptchaProviderResolver::PROVIDER_NONE) {
74 return null;
75 }
76
77 // Use cached instance if available
78 if ($this->registry->has($provider)) {
79 return $this->registry->get($provider);
80 }
81
82 $service = $this->factory->create($provider);
83 if ($service !== null) {
84 $this->registry->register($provider, $service);
85 }
86
87 return $service;
88 }
89
90 /**
91 * Validate form submission with the appropriate CAPTCHA provider based on form fields
92 *
93 * @param array<string, mixed> $submissionData The form submission data
94 * @param array<object> $formFields The form field objects
95 * @return bool
96 * @throws ForbiddenException
97 */
98 public function validateFormSubmission(array $submissionData, array $formFields): bool
99 {
100 $service = $this->createActiveCaptchaService($formFields);
101
102 if ($service === null) {
103 // No CAPTCHA service active for this form, validation passes
104 return true;
105 }
106
107 return $service->validateFormSubmission($submissionData, $formFields);
108 }
109
110 /**
111 * Get all supported CAPTCHA providers
112 *
113 * @return array<string>
114 */
115 public function getSupportedProviders(): array
116 {
117 return $this->factory->getSupportedProviders();
118 }
119
120 /**
121 * Get all configured CAPTCHA providers from database
122 *
123 * @return array<string>
124 */
125 public function getConfiguredProviders(): array
126 {
127 return $this->resolver->getConfiguredProviders();
128 }
129
130 /**
131 * Get the resolver instance for advanced operations
132 *
133 * @return CaptchaProviderResolver
134 */
135 public function getResolver(): CaptchaProviderResolver
136 {
137 return $this->resolver;
138 }
139
140 /**
141 * Get the registry instance for cache management
142 *
143 * @return CaptchaProviderRegistry
144 */
145 public function getRegistry(): CaptchaProviderRegistry
146 {
147 return $this->registry;
148 }
149
150 /**
151 * Get the factory instance for direct service creation
152 *
153 * @return CaptchaServiceFactory
154 */
155 public function getFactory(): CaptchaServiceFactory
156 {
157 return $this->factory;
158 }
159
160 /**
161 * Clear all cached service instances
162 */
163 public function clearCache(): void
164 {
165 $this->registry->clear();
166 }
167 }
168