12-cactus/espinoso

View on GitHub
app/Handlers/SettingsHandler.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
 
namespace App\Handlers;
 
use Unisharp\Setting\SettingFacade as Setting;
 
class SettingsHandler extends MultipleCommand
{
/**
* @var bool
*/
protected $ignorePrefix = true;
protected $patterns = [
[
'name' => 'get',
'pattern' => "(?'command'get)\s+(?'key'\w+)",
], [
'name' => 'set',
'pattern' => "(?'command'set)\s+(?'key'\w+)+(?'value'.+)$",
],
];
protected $signature = "[espi] get|set key [value]";
protected $description = "Get-Set, yo solo quiero ser del Get-Set";
 
/**
* Handle message when match with 'espi get <key>'
*/
protected function handleGet(): void
{
$key = $this->matches['key'];
$chat = $this->message->getChat()->getId();
$value = Setting::get("{$chat}.{$key}");
 
if (empty($value)) {
$this->replyNotFound();
return;
}
 
$this->espinoso->reply($value);
}
 
/**
* Handle message when match with 'espi set <key> <value>'
*/
protected function handleSet(): void
{
$key = $this->matches['key'];
$chat = $this->message->getChat()->getId();
$value = trim($this->matches['value']);
 
Setting::set("{$chat}.{$key}", $value);
 
$this->espinoso->reply(trans('messages.settings.saved'));
}
}