-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathUpload.php
158 lines (136 loc) · 4.6 KB
/
Upload.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Manifest;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload;
use Fastly\Cdn\Model\Modly\Manifest;
use Fastly\Cdn\Model\ManifestFactory;
use Fastly\Cdn\Model\ResourceModel\Manifest as ManifestResource;
use Magento\Framework\Exception\LocalizedException;
class Upload extends Action
{
/**
* @var Http
*/
private $request;
/**
* @var JsonFactory
*/
private $resultJson;
/**
* @var Config
*/
private $config;
/**
* @var \Fastly\Cdn\Model\Api
*/
private $api;
/**
* @var Vcl
*/
private $vcl;
/**
* @var Manifest
*/
private $manifest;
/***
* @var ManifestFactory
*/
private $manifestFactory;
/**
* @var ManifestResource
*/
private $manifestResource;
/**
* @var CustomSnippetUpload
*/
private $customSnippetUpload;
/**
* Upload constructor.
* @param Context $context
* @param Http $request
* @param JsonFactory $resultJsonFactory
* @param Config $config
* @param Api $api
* @param Vcl $vcl
* @param CustomSnippetUpload $customSnippetUpload
* @param Manifest $manifest
* @param ManifestFactory $manifestFactory
* @param ManifestResource $manifestResource
*/
public function __construct(
Context $context,
Http $request,
JsonFactory $resultJsonFactory,
Config $config,
Api $api,
Vcl $vcl,
CustomSnippetUpload $customSnippetUpload,
Manifest $manifest,
ManifestFactory $manifestFactory,
ManifestResource $manifestResource
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
$this->config = $config;
$this->api = $api;
$this->vcl = $vcl;
$this->customSnippetUpload = $customSnippetUpload;
$this->manifest = $manifest;
$this->manifestFactory = $manifestFactory;
$this->manifestResource = $manifestResource;
parent::__construct($context);
}
public function execute()
{
$result = $this->resultJson->create();
try {
$activeVersion = $this->getRequest()->getParam('active_version');
$moduleId = $this->getRequest()->getParam('module_id');
$snippets = json_decode($this->getRequest()->getParam('snippets'));
$service = $this->api->checkServiceDetails();
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
$currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
$clone = $this->api->cloneVersion($currActiveVersion);
foreach ($snippets as $key => $value) {
$snippetData = [
'name' => Config::FASTLY_MODLY_MODULE . '_' . $moduleId . '_' . $value->type,
'type' => $value->type,
'dynamic' => "0",
'priority' => $value->priority,
'content' => $value->snippet
];
$this->api->uploadSnippet($clone->number, $snippetData);
}
$this->api->validateServiceVersion($clone->number);
$this->api->activateVersion($clone->number);
if ($this->config->areWebHooksEnabled() && $this->config->canPublishConfigChanges()) {
$this->api->sendWebHook(
'*Upload VCL has been initiated and activated in version ' . $clone->number . '*'
);
}
$comment = ['comment' => 'Magento Module uploaded the "'.$moduleId.'" Edge Module'];
$this->api->addComment($clone->number, $comment);
$moduleUploadDate = date("Y-m-d H:i:s");
$manifest = $this->manifestFactory->create();
$manifest->setManifestId($moduleId);
$manifest->setLastUploaded($moduleUploadDate);
$this->manifestResource->save($manifest);
return $result->setData([
'status' => true,
'active_version' => $clone->number,
'last_uploaded' => $moduleUploadDate
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}
}