Created
December 29, 2017 08:34
-
-
Save antonshell/a720c37007ac6ef459cf1155c0429d19 to your computer and use it in GitHub Desktop.
Magento Database Config Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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