magento2-docker/app/code/IpSupply/SyncOrder/Api/Repository.php

83 lines
1.7 KiB
PHP
Executable File

<?php
namespace IpSupply\SyncOrder\Api;
use IpSupply\SyncOrder\Api\RepositoryInterface;
/**
* @api
*/
class Repository implements RepositoryInterface
{
function __construct()
{
$this->_authorization();
}
private function _authorization()
{
$headers = apache_request_headers();
$apiKey = \IpSupply\SyncOrder\Helper::API_KEY;
foreach ($headers as $key => $value) {
$key = strtolower($key);
if ('api_key' === $key && $value === $apiKey) {
return true;
}
}
header('HTTP/1.1 401 Unauthorized');
header('Accept: application/json');
header('Content-Type: application/json');
die(json_encode(['message' => 'unauthorized']));
}
static function getForm()
{
$data = [
'url' => '',
];
$config = \IpSupply\SyncOrder\Config\Getter::get('config');
if ($config) {
$data = json_decode($config, true);
}
return [
[
'label' => 'API',
'name' => 'url',
'value' => $data['url'] ?? ''
]
];
}
public function getData()
{
return \IpSupply\SyncOrder\Helper::responseOk([
'status' => true,
'data' => []
]);
}
public function postData()
{
parse_str(
string: file_get_contents('php://input'),
result: $payload,
);
unset($payload['form_key']);
\IpSupply\SyncOrder\Config\Setter::set(
path: 'config',
value: json_encode($payload),
);
return \IpSupply\SyncOrder\Helper::responseOk([
'status' => true,
]);
}
}