-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrationInfoProvider.php
70 lines (65 loc) · 2.68 KB
/
IntegrationInfoProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Queueit\KnownUser;
require_once( __DIR__ .'\IntegrationInfoProviderInterface.php');
/**
* Update IntegraionInfo.
*@api
*/
class IntegrationInfoProvider implements IntegrationInfoProviderInterface
{
const CACHE_KEY = "_queueit_integrationinfo";
const CONFIG_SECRETKEY = 'queueit_knownuser/configuration/secretkey';
public function getIntegrationInfo($useCache)
{
$om = \Magento\Framework\App\ObjectManager::getInstance();
$cacheInterface = $om->get('Magento\Framework\App\CacheInterface');
$integrationConfig = $cacheInterface->load(self::CACHE_KEY);
if($integrationConfig && $useCache)
{
$result = $integrationConfig;
}
else
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('queueit_integrationinfo'); //gives table name with prefix
//Select Data from table
$sql = "Select * FROM " . $tableName . ' order by id desc limit 1' ;
$result = $connection->fetchAll($sql)[0]['info'];
$cacheInterface->save($result, self::CACHE_KEY, array(),5*60);
}
return hex2bin($result);
}
/**
* Update IntegraionInfo.
* @api
* @param string $integrationInfo
* @param string $hash
* @return
*/
public function updateIntegrationInfo($integrationInfo,$hash)
{
if($this->isValidRequest($integrationInfo,$hash))
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('queueit_integrationinfo'); //gives table name with prefix
//Insert Data into table
$sql = "Insert Into " . $tableName . " (info) Values ('" . $integrationInfo ."')";
$connection->query($sql);
}
}
private function isValidRequest($integrationInfo,$hash)
{
$om = \Magento\Framework\App\ObjectManager::getInstance();
$scopeConfig = $om->get('\Magento\Framework\App\Config\ScopeConfigInterface');
$secretKey = $scopeConfig->getValue(
self::CONFIG_SECRETKEY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$calculatedHash = hash_hmac('sha256', $integrationInfo, $secretKey);
return $calculatedHash == $hash;
}
}