Skip to content

Commit ea2bd2f

Browse files
authored
Merge pull request #249 from Inchoo/feature/#215-snippets-removal
Added code for deleting custom snippets from the Fastly service
2 parents 359a194 + 40f9cea commit ea2bd2f

File tree

4 files changed

+282
-31
lines changed

4 files changed

+282
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Fastly CDN for Magento
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Fastly CDN for Magento End User License Agreement
8+
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
9+
*
10+
* DISCLAIMER
11+
*
12+
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
13+
* versions in the future. If you wish to customize this module for your
14+
* needs please refer to http://www.magento.com for more information.
15+
*
16+
* @category Fastly
17+
* @package Fastly_Cdn
18+
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
19+
* @license BSD, see LICENSE_FASTLY_CDN.txt
20+
*/
21+
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\CustomSnippet;
22+
23+
use Magento\Framework\App\Action\Action;
24+
use Magento\Framework\App\Action\Context;
25+
use Magento\Framework\App\Response\Http\FileFactory;
26+
use Magento\Framework\App\Filesystem\DirectoryList;
27+
use Magento\Framework\Filesystem\Directory\WriteFactory;
28+
use Magento\Framework\Controller\Result\JsonFactory;
29+
use Magento\Framework\Filesystem;
30+
use Fastly\Cdn\Model\Config;
31+
use Fastly\Cdn\Model\Api;
32+
use Fastly\Cdn\Helper\Vcl;
33+
34+
/**
35+
* Class CheckCustomSnippet
36+
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\CustomSnippet
37+
*/
38+
class CheckCustomSnippet extends Action
39+
{
40+
/**
41+
* @var FileFactory
42+
*/
43+
private $fileFactory;
44+
/**
45+
* @var DirectoryList
46+
*/
47+
private $directoryList;
48+
/**
49+
* @var WriteFactory
50+
*/
51+
private $writeFactory;
52+
/**
53+
* @var JsonFactory
54+
*/
55+
private $resultJson;
56+
/**
57+
* @var Filesystem
58+
*/
59+
private $filesystem;
60+
/**
61+
* @var Api
62+
*/
63+
private $api;
64+
/**
65+
* @var Vcl
66+
*/
67+
private $vcl;
68+
69+
/**
70+
* DeleteCustomSnippet constructor.
71+
* @param Context $context
72+
* @param FileFactory $fileFactory
73+
* @param DirectoryList $directoryList
74+
* @param WriteFactory $writeFactory
75+
* @param JsonFactory $resultJsonFactory
76+
* @param Filesystem $filesystem
77+
* @param Api $api
78+
* @param Vcl $vcl
79+
*/
80+
public function __construct(
81+
Context $context,
82+
FileFactory $fileFactory,
83+
DirectoryList $directoryList,
84+
WriteFactory $writeFactory,
85+
JsonFactory $resultJsonFactory,
86+
Filesystem $filesystem,
87+
Api $api,
88+
Vcl $vcl
89+
) {
90+
$this->fileFactory = $fileFactory;
91+
$this->directoryList = $directoryList;
92+
$this->writeFactory = $writeFactory;
93+
$this->resultJson = $resultJsonFactory;
94+
$this->filesystem = $filesystem;
95+
$this->api = $api;
96+
$this->vcl = $vcl;
97+
98+
parent::__construct($context);
99+
}
100+
101+
/**
102+
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface
103+
*/
104+
public function execute()
105+
{
106+
$result = $this->resultJson->create();
107+
108+
try {
109+
$activeVersion = $this->getRequest()->getParam('active_version');
110+
$snippet = $this->getRequest()->getParam('snippet_id');
111+
$service = $this->api->checkServiceDetails();
112+
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
113+
114+
$snippetName = explode('_', $snippet);
115+
$snippetName = explode('.', $snippetName[2]);
116+
117+
$reqName = Config::FASTLY_MAGENTO_MODULE . '_' . $snippetName[0];
118+
$checkIfSnippetExist = $this->api->hasSnippet($activeVersion, $reqName);
119+
120+
return $result->setData([
121+
'status' => $checkIfSnippetExist
122+
]);
123+
} catch (\Exception $e) {
124+
return $result->setData([
125+
'status' => false,
126+
'msg' => $e->getMessage()
127+
]);
128+
}
129+
}
130+
}

Diff for: Controller/Adminhtml/FastlyCdn/CustomSnippet/DeleteCustomSnippet.php

+42-11
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222

2323
use Magento\Framework\App\Action\Action;
2424
use Magento\Framework\App\Action\Context;
25-
use Magento\Framework\Controller\Result\RawFactory;
2625
use Magento\Framework\App\Response\Http\FileFactory;
2726
use Magento\Framework\App\Filesystem\DirectoryList;
2827
use Magento\Framework\Filesystem\Directory\WriteFactory;
2928
use Magento\Framework\Controller\Result\JsonFactory;
3029
use Magento\Framework\Filesystem;
3130
use Fastly\Cdn\Model\Config;
31+
use Fastly\Cdn\Model\Api;
32+
use Fastly\Cdn\Helper\Vcl;
3233

3334
/**
3435
* Class DeleteCustomSnippet
@@ -37,10 +38,6 @@
3738
*/
3839
class DeleteCustomSnippet extends Action
3940
{
40-
/**
41-
* @var RawFactory
42-
*/
43-
private $resultRawFactory;
4441
/**
4542
* @var FileFactory
4643
*/
@@ -61,33 +58,43 @@ class DeleteCustomSnippet extends Action
6158
* @var Filesystem
6259
*/
6360
private $filesystem;
61+
/**
62+
* @var Api
63+
*/
64+
private $api;
65+
/**
66+
* @var Vcl
67+
*/
68+
private $vcl;
6469

6570
/**
6671
* DeleteCustomSnippet constructor.
67-
*
6872
* @param Context $context
69-
* @param RawFactory $resultRawFactory
7073
* @param FileFactory $fileFactory
7174
* @param DirectoryList $directoryList
7275
* @param WriteFactory $writeFactory
7376
* @param JsonFactory $resultJsonFactory
7477
* @param Filesystem $filesystem
78+
* @param Api $api
79+
* @param Vcl $vcl
7580
*/
7681
public function __construct(
7782
Context $context,
78-
RawFactory $resultRawFactory,
7983
FileFactory $fileFactory,
8084
DirectoryList $directoryList,
8185
WriteFactory $writeFactory,
8286
JsonFactory $resultJsonFactory,
83-
Filesystem $filesystem
87+
Filesystem $filesystem,
88+
Api $api,
89+
Vcl $vcl
8490
) {
85-
$this->resultRawFactory = $resultRawFactory;
8691
$this->fileFactory = $fileFactory;
8792
$this->directoryList = $directoryList;
8893
$this->writeFactory = $writeFactory;
8994
$this->resultJson = $resultJsonFactory;
9095
$this->filesystem = $filesystem;
96+
$this->api = $api;
97+
$this->vcl = $vcl;
9198

9299
parent::__construct($context);
93100
}
@@ -102,17 +109,41 @@ public function execute()
102109
$result = $this->resultJson->create();
103110

104111
try {
112+
$activeVersion = $this->getRequest()->getParam('active_version');
105113
$snippet = $this->getRequest()->getParam('snippet_id');
114+
$activateVcl = $this->getRequest()->getParam('activate_flag');
115+
$service = $this->api->checkServiceDetails();
116+
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
117+
$currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
106118

107119
$write = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
108120
$snippetPath = $write->getRelativePath(Config::CUSTOM_SNIPPET_PATH . $snippet);
109121

122+
$snippetName = explode('_', $snippet);
123+
$snippetName = explode('.', $snippetName[2]);
124+
125+
$reqName = Config::FASTLY_MAGENTO_MODULE . '_' . $snippetName[0];
126+
$checkIfSnippetExist = $this->api->hasSnippet($activeVersion, $reqName);
127+
128+
if ($checkIfSnippetExist) {
129+
$clone = $this->api->cloneVersion($currActiveVersion);
130+
$this->api->removeSnippet($clone->number, $reqName);
131+
$this->api->validateServiceVersion($clone->number);
132+
133+
if ($activateVcl === 'true') {
134+
$this->api->activateVersion($clone->number);
135+
}
136+
137+
$comment = ['comment' => 'Magento Module deleted the ' . $reqName . ' custom snippet.'];
138+
$this->api->addComment($clone->number, $comment);
139+
}
140+
110141
if ($write->isExist($snippetPath)) {
111142
$write->delete($snippetPath);
112143
}
113144

114145
return $result->setData([
115-
'status' => true
146+
'status' => true
116147
]);
117148
} catch (\Exception $e) {
118149
return $result->setData([

Diff for: view/adminhtml/templates/system/config/dialogs.phtml

+39
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,43 @@
161161
</form>
162162
</script>
163163

164+
<!-- Delete Custom Snippet overlay template -->
165+
<script type="text/x-magento-template" id="fastly-delete-custom-snippet-template">
166+
<div class="messages">
167+
<div class="message message-notice fastly-message-notice" style="display: none">
168+
</div>
169+
<div class="message message-warning fastly-message-warning" style="display: none">
170+
<?php /* @noEscape */ echo __("You are about to clone your active version #x.")?>
171+
<?php /* @noEscape */ echo __("We'll make the changes to version #y.")?>
172+
</div>
173+
<div class="message message-error fastly-message-error" style="display: none">
174+
</div>
175+
</div>
176+
<form action="<?php /* @noEscape */ echo $block->getUrl('adminhtml/fastlyCdn_Vcl/upload'); ?>"
177+
method="POST"
178+
id="vcl-upload-form"
179+
class="form-inline"
180+
enctype="multipart/form-data">
181+
<fieldset class="admin__fieldset form-list question">
182+
<div class="admin__field field maintenance-checkbox-container">
183+
<label for="fastly_activate_vcl" class="admin__field-label">
184+
<span><?php /* @noEscape */ echo __('Activate after the change')?></span>
185+
</label>
186+
<div class="admin__field-control">
187+
<div class="admin__field-option">
188+
<input class="admin__control-checkbox"
189+
type="checkbox"
190+
name="fastly_delete_custom_snippet_activate"
191+
id="fastly_delete_custom_snippet_activate"
192+
checked/>
193+
<label class="admin__field-label" for="fastly_delete_custom_snippet_active"></label>
194+
</div>
195+
</div>
196+
</div>
197+
</fieldset>
198+
</form>
199+
</script>
200+
164201
<!-- WAF Bypass toggle overlay template -->
165202
<script type="text/x-magento-template" id="fastly-waf-bypass-template">
166203
<div class="messages">
@@ -1557,6 +1594,7 @@ $createCustomSnippetUrl = $block->getUrl('adminhtml/fastlyCdn_CustomSnippe
15571594
$deleteCustomSnippet = $block->getUrl('adminhtml/fastlyCdn_CustomSnippet/deleteCustomSnippet');
15581595
$getCustomSnippetsUrl = $block->getUrl('adminhtml/fastlyCdn_CustomSnippet/getCustomSnippets');
15591596
$getCustomSnippet = $block->getUrl('adminhtml/fastlyCdn_CustomSnippet/getCustomSnippet');
1597+
$checkCustomSnippet = $block->getUrl('adminhtml/fastlyCdn_CustomSnippet/checkCustomSnippet');
15601598
// Domains
15611599
$getDomainsUrl = $block->getUrl('adminhtml/fastlyCdn_Domains/getDomains');
15621600
$pushDomainsUrl = $block->getUrl('adminhtml/fastlyCdn_Domains/pushDomains');
@@ -1642,6 +1680,7 @@ $isFastlyEnabled = $block->getConfig()->isFastlyEnabled();
16421680
"deleteAcl": "<?php echo $block->escapeJsQuote($deleteAcl);?>",
16431681
"deleteCustomSnippet": "<?php echo $block->escapeJsQuote($deleteCustomSnippet);?>",
16441682
"getCustomSnippet": "<?php echo $block->escapeJsQuote($getCustomSnippet);?>",
1683+
"checkCustomSnippet": "<?php echo $block->escapeJsQuote($checkCustomSnippet);?>",
16451684
"getAcls": "<?php echo $block->escapeJsQuote($listAcls);?>",
16461685
"getAclItems": "<?php echo $block->escapeJsQuote($aclItems);?>",
16471686
"createAclItem": "<?php echo $block->escapeJsQuote($createAclItem);?>",

0 commit comments

Comments
 (0)