Skip to content

Instantly share code, notes, and snippets.

@antonshell
Created December 29, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonshell/a720c37007ac6ef459cf1155c0429d19 to your computer and use it in GitHub Desktop.
Save antonshell/a720c37007ac6ef459cf1155c0429d19 to your computer and use it in GitHub Desktop.
Magento Database Config Helper
<?php
namespace Antonshell\OrdersWebhooks\Services;
use Antonshell\OrdersWebhooks\Observer\MOrderSaveObserver;
use Magento\Framework\Event\ConfigInterface;
use Magento\Framework\Event\InvokerInterface;
use ReflectionClass;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* Class Config
* @package Antonshell\OrdersWebhooks\Services
*/
class Config
{
const PATH_URL = 'orders_webhook/webhook_url';
const PATH_TOKEN = 'orders_webhook/webhook_token';
protected $url;
protected $token;
/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected $connection;
protected $configWriter;
/**
* Config constructor.
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
*/
public function __construct(
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter
) {
$resources = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\ResourceConnection');
$this->connection = $resources->getConnection();
$this->configWriter = $configWriter;
}
/**
* @return string
* @throws \Exception
*/
public function getUrl(){
if(!$this->url){
$this->url = $this->getOption(self::PATH_URL);
}
return $this->url;
}
/**
* @param $url
*/
public function setUrl($url){
$this->setOption(self::PATH_URL,$url);
$this->url = $url;
}
/**
* @return string
* @throws \Exception
*/
public function getToken(){
if(!$this->token){
$this->token = $this->getOption(self::PATH_TOKEN);
}
return $this->token;
}
/**
* @param $token
*/
public function setToken($token){
$this->setOption(self::PATH_TOKEN,$token);
$this->token = $token;
}
/**
* @param $path
* @return mixed
* @throws \Exception
*/
private function getOption($path){
$sql = "SELECT * FROM core_config_data WHERE path = :path";
$binds = [ 'path' => $path ];
$row = $this->connection->fetchAll($sql,$binds);
if(!isset($row[0]['value'])){
throw new \Exception('Can\'t get webhook Token');
}
return $row[0]['value'];
}
/**
* @param $option
* @param $value
*/
private function setOption($option, $value){
$this->configWriter->save($option, $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment