178 lines
5.0 KiB
PHP
Executable File
178 lines
5.0 KiB
PHP
Executable File
<?php
|
|
namespace Kai\Banner\Api;
|
|
|
|
use Kai\Banner\Api\BannerRepositoryInterface;
|
|
|
|
/**
|
|
* @api
|
|
*/
|
|
class BannerRepository implements BannerRepositoryInterface
|
|
{
|
|
protected $kaiBannerModel;
|
|
protected $kaiBannerResourceModel;
|
|
protected $kaiBannerCollection;
|
|
|
|
function __construct(
|
|
\Kai\Banner\Model\KaiBanner $kaiBannerModel,
|
|
\Kai\Banner\Model\KaiBannerResourceModel $kaiBannerResourceModel,
|
|
\Kai\Banner\Model\KaiBannerCollection $kaiBannerCollection,
|
|
)
|
|
{
|
|
$this->kaiBannerModel = $kaiBannerModel;
|
|
$this->kaiBannerResourceModel = $kaiBannerResourceModel;
|
|
$this->kaiBannerCollection = $kaiBannerCollection;
|
|
$this->authorization();
|
|
}
|
|
|
|
private function authorization()
|
|
{
|
|
$headers = apache_request_headers();
|
|
$apiKey = \Kai\Banner\Api\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'])
|
|
);
|
|
}
|
|
|
|
private function responseOk(array $data)
|
|
{
|
|
header('HTTP/1.1 200 Ok');
|
|
header('Accept: application/json');
|
|
header('Content-Type: application/json');
|
|
die(json_encode($data));
|
|
}
|
|
|
|
private function responseFail(array $data)
|
|
{
|
|
header('HTTP/1.1 400 Bad request');
|
|
header('Accept: application/json');
|
|
header('Content-Type: application/json');
|
|
die(json_encode($data));
|
|
}
|
|
|
|
private function responseMethodFail()
|
|
{
|
|
header('HTTP/1.1 400 bad request');
|
|
header('Accept: application/json');
|
|
header('Content-Type: application/json');
|
|
die(json_encode([
|
|
'status' => false,
|
|
'message' => 'Param ?method=... not exist!'
|
|
]));
|
|
}
|
|
|
|
public function getData()
|
|
{
|
|
$method = $_GET['method'] ?? null;
|
|
|
|
if ($method === 'show') {
|
|
$id = $_GET['id'] ?? 0;
|
|
return $this->responseOk([
|
|
'status' => true,
|
|
'data' => $this
|
|
->kaiBannerCollection
|
|
->getItemById($id)
|
|
->getData()
|
|
]);
|
|
}
|
|
|
|
if ($method === 'all') {
|
|
return $this->responseOk([
|
|
'status' => true,
|
|
'data' => $this
|
|
->kaiBannerCollection
|
|
->getData()
|
|
]);
|
|
}
|
|
|
|
if ($method === 'delete') {
|
|
$id = $_GET['id'] ?? 0;
|
|
$model = $this->kaiBannerModel->setId($id);
|
|
|
|
try {
|
|
$this->kaiBannerResourceModel->delete($model);
|
|
return $this->responseOk([
|
|
'status' => true,
|
|
]);
|
|
} catch (\Exception $ex) {
|
|
$this->kaiBannerResourceModel->rollBack();
|
|
return $this->responseFail([
|
|
'status' => false,
|
|
'message' => $ex->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->responseMethodFail();
|
|
}
|
|
|
|
public function postData()
|
|
{
|
|
$method = $_GET['method'] ?? null;
|
|
parse_str(
|
|
string: file_get_contents('php://input'),
|
|
result: $payload
|
|
);
|
|
|
|
if ($method === 'create') {
|
|
$model = $this->kaiBannerModel;
|
|
try {
|
|
foreach ($payload as $key => $value) {
|
|
$model->setData($key, $value);
|
|
}
|
|
$this->kaiBannerResourceModel->save($model);
|
|
|
|
return $this->responseOk([
|
|
'status' => true,
|
|
'data' => $this
|
|
->kaiBannerCollection
|
|
->getItemById($model->getId())
|
|
->getData()
|
|
]);
|
|
} catch (\Exception $ex) {
|
|
return $this->responseFail([
|
|
'status' => false,
|
|
'message' => $ex->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
if ($method === 'update') {
|
|
$id = $_GET['id'] ?? 0;
|
|
$model = $this->kaiBannerModel->setId($id);
|
|
|
|
try {
|
|
foreach ($payload as $key => $value) {
|
|
$model->setData($key, $value);
|
|
}
|
|
$this->kaiBannerResourceModel->save($model);
|
|
|
|
return $this->responseOk([
|
|
'status' => true,
|
|
'data' => $this
|
|
->kaiBannerCollection
|
|
->getItemById($model->getId())
|
|
->getData()
|
|
]);
|
|
} catch (\Exception $ex) {
|
|
return $this->responseFail([
|
|
'status' => false,
|
|
'message' => $ex->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->responseMethodFail();
|
|
}
|
|
}
|