Skip to content

Commit 5c3ad1f

Browse files
authored
Merge pull request #530 from favicode/fix/503
Fix/503
2 parents 4d38585 + ddef39d commit 5c3ad1f

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

Diff for: Controller/Adminhtml/FastlyCdn/Vcl/Upload.php

+38-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838
use Magento\Framework\App\Cache\TypeListInterface;
3939

4040
/**
41-
* Class Upload
41+
* Class for VCL Upload
4242
*
43-
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl
4443
*/
4544
class Upload extends Action
4645
{
@@ -132,7 +131,6 @@ public function __construct(
132131
parent::__construct($context);
133132
$this->coreConfig = $coreConfig;
134133
$this->typeList = $typeList;
135-
136134
}
137135

138136
/**
@@ -155,6 +153,7 @@ public function execute()
155153
$customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH);
156154
$customSnippets = $this->config->getCustomSnippets($customSnippetPath);
157155

156+
$allowedSnippets = [];
158157
foreach ($snippets as $key => $value) {
159158
$priority = 50;
160159
if ($key == 'hash') {
@@ -167,6 +166,7 @@ public function execute()
167166
'priority' => $priority,
168167
'content' => $value
169168
];
169+
$allowedSnippets[] = $snippetData['name'];
170170
$this->api->uploadSnippet($clone->number, $snippetData);
171171
}
172172

@@ -183,9 +183,12 @@ public function execute()
183183
'content' => $value,
184184
'dynamic' => '0'
185185
];
186+
$allowedSnippets[] = $customSnippetData['name'];
186187
$this->api->uploadSnippet($clone->number, $customSnippetData);
187188
}
188189

190+
$this->syncSnippets($allowedSnippets, $clone->number);
191+
189192
$this->createGzipHeader($clone);
190193

191194
$condition = [
@@ -273,6 +276,8 @@ private function validateCustomSnippet($customSnippet)
273276
}
274277

275278
/**
279+
* Setup Dictionary
280+
*
276281
* @param $cloneNumber
277282
* @param $currActiveVersion
278283
* @return bool|mixed
@@ -291,6 +296,8 @@ private function setupDictionary($cloneNumber, $currActiveVersion)
291296
}
292297

293298
/**
299+
* Setup Acl
300+
*
294301
* @param $cloneNumber
295302
* @param $currActiveVersion
296303
* @return bool|mixed
@@ -309,10 +316,12 @@ private function setupAcl($cloneNumber, $currActiveVersion)
309316
}
310317

311318
/**
319+
* Create Gzip Header
320+
*
312321
* @param $clone
313322
* @throws LocalizedException
314323
*/
315-
private function createGzipHeader($clone)
324+
private function createGzipHeader($clone): void
316325
{
317326
$condition = [
318327
'name' => Config::FASTLY_MAGENTO_MODULE . '_gzip_safety',
@@ -334,4 +343,29 @@ private function createGzipHeader($clone)
334343

335344
$this->api->createHeader($clone->number, $headerData);
336345
}
346+
347+
/**
348+
* Remove disabled snippets from current vcl file
349+
*
350+
* @param array $allowedSnippets
351+
* @param int $version
352+
* @throws LocalizedException
353+
*/
354+
private function syncSnippets(array $allowedSnippets, int $version): void
355+
{
356+
$snippets = $this->api->getSnippets($version);
357+
358+
$currentActiveSnippets = [];
359+
foreach ($snippets as $item) {
360+
$currentActiveSnippets[] = $item->name;
361+
}
362+
$snippetsForDelete = array_diff($currentActiveSnippets, $allowedSnippets);
363+
364+
foreach ($snippetsForDelete as $snippetName) {
365+
//remove only snippet name which starts with magento prefix
366+
if (strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') === 0) {
367+
$this->api->removeSnippet($version, $snippetName);
368+
}
369+
}
370+
}
337371
}

Diff for: Model/Api.php

+15
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,21 @@ public function activateVersion($version)
500500
return $result;
501501
}
502502

503+
/**
504+
* Get all VCL snippets
505+
*
506+
* @param $version
507+
* @return bool|mixed
508+
* @throws LocalizedException
509+
*/
510+
public function getSnippets($version)
511+
{
512+
$url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/snippet';
513+
$result = $this->_fetch($url, 'GET');
514+
515+
return $result;
516+
}
517+
503518
/**
504519
* Creating and updating regular VCL snippets
505520
*

0 commit comments

Comments
 (0)