class Trigger in Build Hooks 8
Same name and namespace in other branches
- 8.2 src/Trigger.php \Drupal\build_hooks\Trigger
- 3.x src/Trigger.php \Drupal\build_hooks\Trigger
Class Trigger.
Hierarchy
- class \Drupal\build_hooks\Trigger implements TriggerInterface
Expanded class hierarchy of Trigger
1 string reference to 'Trigger'
1 service uses Trigger
File
- src/
Trigger.php, line 16
Namespace
Drupal\build_hooksView source
class Trigger implements TriggerInterface {
/**
* The config.factory service
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The http_client service
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* The current_user service
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The string_translation service
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $stringTranslation;
/**
* The messenger service
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The logger.factory service
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $logger;
/**
* Constructs a new Trigger object.
*/
public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, AccountProxyInterface $currentUser, TranslationManager $stringTranslation, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger) {
$this->configFactory = $configFactory;
$this->httpClient = $httpClient;
$this->currentUser = $currentUser;
$this->stringTranslation = $stringTranslation;
$this->messenger = $messenger;
$this->logger = $logger;
}
public function execute() {
if (!$this
->isValidUser()) {
$message = $this->stringTranslation
->translate('Insufficient user acess', []);
$this->logger
->get('build_hooks')
->error($message);
return FALSE;
}
try {
$hook = $this->configFactory
->get('build_hooks.settings')
->get('build_hook');
$this->httpClient
->post($hook . '-lorem');
$message = 'Build Hook Executed';
$this
->processMessage($message);
$this
->processLog($message);
} catch (RequestException $exception) {
$message = $this->stringTranslation
->translate('Build Hook execute error: "%error"', [
'%error' => $exception
->getMessage(),
]);
$this->messenger
->addError($message);
$this->logger
->get('build_hooks')
->error($message);
return FALSE;
}
return TRUE;
}
public function executeCron() {
$execute = (bool) $this->configFactory
->get('build_hooks.settings')
->get('triggers.cron');
if ($execute) {
$this
->execute();
}
}
public function executeNode($nodeType) {
$execute = (bool) $this->configFactory
->get('build_hooks.settings')
->get('triggers.node.' . $nodeType);
if ($execute) {
$this
->execute();
}
}
public function showMenu() {
if (!$this
->isValidUser()) {
return FALSE;
}
return (bool) $this->configFactory
->get('build_hooks.settings')
->get('triggers.menu');
}
private function isValidUser() {
return $this->currentUser
->hasPermission('access admin');
}
private function processMessage($message, $args = []) {
$show = (bool) $this->configFactory
->get('build_hooks.settings')
->get('messages.show');
if (!$show) {
return;
}
$this->messenger
->addMessage($this->stringTranslation
->translate($message, $args));
}
private function processLog($message, $args = []) {
$log = (bool) $this->configFactory
->get('build_hooks.settings')
->get('messages.log');
if (!$log) {
return;
}
$this->logger
->get('build_hooks')
->info($this->stringTranslation
->translate($message, $args));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Trigger:: |
protected | property | The config.factory service | |
Trigger:: |
protected | property | The current_user service | |
Trigger:: |
protected | property | The http_client service | |
Trigger:: |
protected | property | The logger.factory service | |
Trigger:: |
protected | property | The messenger service | |
Trigger:: |
protected | property | The string_translation service | |
Trigger:: |
public | function |
return void Overrides TriggerInterface:: |
|
Trigger:: |
public | function |
return void Overrides TriggerInterface:: |
|
Trigger:: |
public | function |
Overrides TriggerInterface:: |
|
Trigger:: |
private | function | ||
Trigger:: |
private | function | ||
Trigger:: |
private | function | ||
Trigger:: |
public | function |
return void Overrides TriggerInterface:: |
|
Trigger:: |
public | function | Constructs a new Trigger object. |