cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Service/K8sServiceFactory.php
modules/cloud_service_providers/k8s/src/Service/K8sServiceFactory.php
<?php
namespace Drupal\k8s\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Traits\EntityTrait;
/**
* K8sService service interacts with the K8s API.
*/
class K8sServiceFactory {
use StringTranslationTrait;
use EntityTrait;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Cloud context string.
*
* @var string
*/
protected $cloudContext;
/**
* A logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* The config factory.
*
* Subclasses should use the self::config() method, which may be overridden to
* address specific needs when loading config, rather than this property
* directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
* this.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The cloud service provider plugin manager (CloudConfigPluginManager).
*
* @var \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Field type plugin manager.
*
* @var \Drupal\core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypePluginManager;
/**
* Entity field manager interface.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* TRUE to run the operation. FALSE to run the operation in validation mode.
*
* @var bool
*/
private $dryRun;
/**
* The lock interface.
*
* @var \Drupal\Core\Lock\LockBackendInterface
*/
protected $lock;
/**
* Constructs a new K8sService object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* An entity type manager instance.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* A logger instance.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A configuration factory.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud service provider plugin manager (CloudConfigPluginManager).
* @param \Drupal\core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager
* The field type plugin manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock interface.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
LoggerChannelFactoryInterface $logger_factory,
ConfigFactoryInterface $config_factory,
Messenger $messenger,
TranslationInterface $string_translation,
AccountInterface $current_user,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
FieldTypePluginManagerInterface $field_type_plugin_manager,
EntityFieldManagerInterface $entity_field_manager,
LockBackendInterface $lock
) {
// Setup the entity type manager for querying entities.
$this->entityTypeManager = $entity_type_manager;
// Setup the logger.
$this->loggerFactory = $logger_factory;
// Setup the configuration factory.
$this->configFactory = $config_factory;
// Setup the messenger.
$this->messenger = $messenger;
// Setup the $this->t()
$this->stringTranslation = $string_translation;
$this->currentUser = $current_user;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
$this->fieldTypePluginManager = $field_type_plugin_manager;
$this->entityFieldManager = $entity_field_manager;
$this->lock = $lock;
}
/**
* Create service.
*
* @return \Drupal\k8s\Service\K8sServiceInterface
* K8s service.
*/
public function create() {
$test_mode = (bool) $this->configFactory->get('k8s.settings')->get('k8s_test_mode');
$class_name = K8sService::class;
if ($test_mode) {
$class_name = K8sServiceMock::class;
}
return new $class_name(
$this->entityTypeManager,
$this->loggerFactory,
$this->configFactory,
$this->messenger,
$this->stringTranslation,
$this->currentUser,
$this->cloudConfigPluginManager,
$this->fieldTypePluginManager,
$this->entityFieldManager,
$this->lock
);
}
}
