| Conditions | 24 |
| Paths | 108 |
| Total Lines | 128 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | public function __construct( |
||
| 31 | private readonly HttpClientInterface $httpClient, |
||
| 32 | private readonly SettingsManager $settingsManager, |
||
| 33 | private readonly AiRequestsRepository $aiRequestsRepository, |
||
| 34 | private readonly Security $security |
||
| 35 | ) { |
||
| 36 | $config = $this->readProvidersConfig(); |
||
| 37 | |||
| 38 | // Default provider = first key in config (if any) |
||
| 39 | $this->defaultProvider = array_key_first($config) ?? 'openai'; |
||
| 40 | |||
| 41 | // Provider name -> class prefix |
||
| 42 | $possibleProviders = [ |
||
| 43 | 'openai' => 'OpenAi', |
||
| 44 | 'deepseek' => 'DeepSeek', |
||
| 45 | 'grok' => 'Grok', |
||
| 46 | 'mistral' => 'Mistral', |
||
| 47 | 'gemini' => 'Gemini', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | // Suffix used only if you create type-specific classes (optional) |
||
| 51 | $typeSuffix = [ |
||
| 52 | 'text' => '', |
||
| 53 | 'image' => 'Image', |
||
| 54 | 'video' => 'Video', |
||
| 55 | 'document' => 'Document', |
||
| 56 | 'document_process' => 'DocumentProcess', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | // Expected interface per known type |
||
| 60 | $typeInterface = [ |
||
| 61 | 'text' => AiProviderInterface::class, |
||
| 62 | 'image' => AiImageProviderInterface::class, |
||
| 63 | 'video' => AiVideoProviderInterface::class, |
||
| 64 | 'document' => AiDocumentProviderInterface::class, |
||
| 65 | // 'document_process' intentionally not validated (no interface yet) |
||
| 66 | ]; |
||
| 67 | |||
| 68 | $this->providers = []; |
||
| 69 | $this->providersByType = []; |
||
| 70 | |||
| 71 | foreach ($config as $providerName => $providerConfig) { |
||
| 72 | if (!isset($possibleProviders[$providerName])) { |
||
| 73 | error_log('[AI] Unsupported provider in config: "'.$providerName.'". Skipping.'); |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | if (!\is_array($providerConfig)) { |
||
| 78 | error_log('[AI] Provider config for "'.$providerName.'" must be an array. Skipping.'); |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | $providerPrefix = $possibleProviders[$providerName]; |
||
| 83 | |||
| 84 | // Base provider class (e.g. OpenAiProvider) |
||
| 85 | $baseClass = 'Chamilo\\CoreBundle\\AiProvider\\'.$providerPrefix.'Provider'; |
||
| 86 | $baseObject = $this->instantiateProvider($baseClass); |
||
| 87 | |||
| 88 | // If base class cannot be instantiated, we can still allow type-specific classes. |
||
| 89 | if (!$baseObject && !class_exists($baseClass)) { |
||
| 90 | error_log('[AI] Base provider class not found: '.$baseClass.'.'); |
||
| 91 | } elseif (!$baseObject) { |
||
| 92 | error_log('[AI] Base provider class exists but could not be instantiated: '.$baseClass.'.'); |
||
| 93 | } |
||
| 94 | |||
| 95 | foreach ($typeSuffix as $type => $suffix) { |
||
| 96 | // Only types explicitly present in provider config are considered enabled |
||
| 97 | if (!array_key_exists($type, $providerConfig)) { |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | |||
| 101 | $typeClass = 'Chamilo\\CoreBundle\\AiProvider\\'.$providerPrefix.$suffix.'Provider'; |
||
| 102 | |||
| 103 | // Known types: we can fallback to base provider if it implements the right interface |
||
| 104 | if (isset($typeInterface[$type])) { |
||
| 105 | $iface = $typeInterface[$type]; |
||
| 106 | $obj = null; |
||
| 107 | |||
| 108 | // 1) Prefer type-specific class if exists (optional architecture) |
||
| 109 | if ($typeClass !== $baseClass && class_exists($typeClass)) { |
||
| 110 | $obj = $this->instantiateProvider($typeClass); |
||
| 111 | |||
| 112 | if ($obj && !($obj instanceof $iface)) { |
||
| 113 | error_log('[AI] Provider "'.$providerName.'" type-class "'.$typeClass.'" does not implement '.$iface.'. Falling back to base provider.'); |
||
| 114 | $obj = null; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | // 2) Fallback to base provider |
||
| 119 | if (!$obj) { |
||
| 120 | if ($baseObject && ($baseObject instanceof $iface)) { |
||
| 121 | $obj = $baseObject; |
||
| 122 | |||
| 123 | if ($typeClass !== $baseClass && !class_exists($typeClass)) { |
||
| 124 | error_log('[AI] Provider "'.$providerName.'" uses base provider for type "'.$type.'" (no dedicated type class found).'); |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | error_log('[AI] Provider "'.$providerName.'" is configured for type "'.$type.'" but no usable implementation was found (expected '.$iface.').'); |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->providers[$providerName][$type] = $obj; |
||
| 133 | $this->providersByType[$type][$providerName] = $obj; |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Unknown type (e.g. document_process): do NOT fallback to base provider. |
||
| 138 | // Require a dedicated class to avoid claiming support when methods don't exist. |
||
| 139 | if ($typeClass !== $baseClass && class_exists($typeClass)) { |
||
| 140 | $obj = $this->instantiateProvider($typeClass); |
||
| 141 | |||
| 142 | if (!$obj) { |
||
| 143 | error_log('[AI] Provider "'.$providerName.'" is configured for unknown type "'.$type.'" but could not instantiate "'.$typeClass.'".'); |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->providers[$providerName][$type] = $obj; |
||
| 148 | $this->providersByType[$type][$providerName] = $obj; |
||
| 149 | } else { |
||
| 150 | error_log('[AI] Provider "'.$providerName.'" is configured for unknown type "'.$type.'" but no dedicated class was found (expected "'.$typeClass.'").'); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // Ensure default provider exists when config is not empty |
||
| 156 | if (!empty($config) && !isset($this->providers[$this->defaultProvider])) { |
||
| 157 | throw new InvalidArgumentException("The default AI provider '{$this->defaultProvider}' is not configured properly."); |
||
| 158 | } |
||
| 239 |