ProxyServiceFactory.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Factory;
  3. use ModulesGarden\ProxmoxAddon\App\Helper\Logger;
  4. use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;
  5. use ModulesGarden\ProxmoxAddon\App\Repositories\ModuleSettingRepository;
  6. use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
  7. use ModulesGarden\ProxmoxAddon\App\Services\ProxyService;
  8. use ModulesGarden\ProxmoxAddon\App\Services\RestFullApi;
  9. class ProxyServiceFactory
  10. {
  11. /**
  12. * @param ModuleSettingRepository $modConf
  13. * @return ProxyService
  14. */
  15. public function fromModuleConfiguration(ModuleSettingRepository $modConf){
  16. if(!$modConf->consoleHost){
  17. throw new \InvalidArgumentException("Console Host is empty");
  18. }
  19. if(!$modConf->getConsoleApiKey()){
  20. throw new \InvalidArgumentException("Console API Key is empty");
  21. }
  22. $host = $modConf->consoleHost;
  23. if(!preg_match("/http/", $host)){
  24. $host ="https://".$host;
  25. }
  26. $api = new RestFullApi($host);
  27. //debug
  28. if(ModuleSettings::isDebug()){
  29. $api->setLogger(new Logger('ProxmoxVps',[$modConf->getConsoleApiKey()]));
  30. }
  31. return new ProxyService($modConf->getConsoleApiKey(), $modConf->consoleHost, $api);
  32. }
  33. }