-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGraphqlModel.php
executable file
·105 lines (90 loc) · 3.27 KB
/
GraphqlModel.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Vuefront\Vuefront\Model;
use Magento\Framework\App\Action\Context;
use Vuefront\Vuefront\Api\GraphqlInterface;
class GraphqlModel implements GraphqlInterface
{
private $scopeConfig;
private $context;
private $response;
private $redirect;
private $url;
private $driver;
protected $request;
protected $startup;
protected $collectionFactory;
public function __construct(
Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\UrlInterface $url,
\Magento\Framework\Webapi\Rest\Response $response,
\Magento\Framework\Webapi\Rest\Request $request,
\Magento\Framework\Filesystem\Driver\File $driver,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
\Vuefront\Vuefront\Model\Api\System\Startup $startup
) {
$this->scopeConfig = $scopeConfig;
$this->context = $context;
$this->response = $response;
$this->request = $request;
$this->redirect = $context->getRedirect();
$this->url = $url;
$this->startup = $startup;
$this->driver = $driver;
$this->collectionFactory = $productFactory;
}
public function checkCors()
{
if (!empty($this->request->get('cors'))) {
if (!empty($this->request->getServer('HTTP_ORIGIN'))) {
$this->response->setHeader('Access-Control-Allow-Origin', $this->request->getServer('HTTP_ORIGIN'));
} else {
$this->response->setHeader('Access-Control-Allow-Origin', '*');
}
$this->response->setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS, GET');
$this->response->setHeader('Access-Control-Allow-Credentials', 'true');
$this->response->setHeader(
'Access-Control-Allow-Headers',
'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,' .
'Content-Type,Range,Token,token,Cookie,cookie,content-type,Authorization'
);
}
}
public function cors()
{
$this->checkCors();
return '';
}
public function graphql()
{
$this->checkCors();
$enable = $this->scopeConfig
->getValue('vuefront/general/enable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if ($enable) {
$output = $this->startup->start($this->request->getBodyParams(), $this->driver);
return $output;
} else {
$norouteUrl = $this->url->getUrl('noroute');
$this->response->setRedirect($norouteUrl);
return;
}
}
public function callback()
{
$this->checkCors();
$enable = $this->scopeConfig
->getValue('vuefront/general/enable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if ($enable) {
$output = $this->startup->callback(
$this->request->getBodyParams(),
$this->driver,
$this->request->getQueryValue('order_id')
);
return $output;
} else {
$norouteUrl = $this->url->getUrl('noroute');
$this->response->setRedirect($norouteUrl);
return;
}
}
}