diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 3f0f2e02..b8d1e7bc 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -38,9 +38,8 @@ use Magento\Framework\App\Cache\TypeListInterface; /** - * Class Upload + * Class for VCL Upload * - * @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl */ class Upload extends Action { @@ -132,7 +131,6 @@ public function __construct( parent::__construct($context); $this->coreConfig = $coreConfig; $this->typeList = $typeList; - } /** @@ -155,6 +153,7 @@ public function execute() $customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH); $customSnippets = $this->config->getCustomSnippets($customSnippetPath); + $allowedSnippets = []; foreach ($snippets as $key => $value) { $priority = 50; if ($key == 'hash') { @@ -167,6 +166,7 @@ public function execute() 'priority' => $priority, 'content' => $value ]; + $allowedSnippets[] = $snippetData['name']; $this->api->uploadSnippet($clone->number, $snippetData); } @@ -183,9 +183,12 @@ public function execute() 'content' => $value, 'dynamic' => '0' ]; + $allowedSnippets[] = $customSnippetData['name']; $this->api->uploadSnippet($clone->number, $customSnippetData); } + $this->syncSnippets($allowedSnippets, $clone->number); + $this->createGzipHeader($clone); $condition = [ @@ -273,6 +276,8 @@ private function validateCustomSnippet($customSnippet) } /** + * Setup Dictionary + * * @param $cloneNumber * @param $currActiveVersion * @return bool|mixed @@ -291,6 +296,8 @@ private function setupDictionary($cloneNumber, $currActiveVersion) } /** + * Setup Acl + * * @param $cloneNumber * @param $currActiveVersion * @return bool|mixed @@ -309,10 +316,12 @@ private function setupAcl($cloneNumber, $currActiveVersion) } /** + * Create Gzip Header + * * @param $clone * @throws LocalizedException */ - private function createGzipHeader($clone) + private function createGzipHeader($clone): void { $condition = [ 'name' => Config::FASTLY_MAGENTO_MODULE . '_gzip_safety', @@ -334,4 +343,29 @@ private function createGzipHeader($clone) $this->api->createHeader($clone->number, $headerData); } + + /** + * Remove disabled snippets from current vcl file + * + * @param array $allowedSnippets + * @param int $version + * @throws LocalizedException + */ + private function syncSnippets(array $allowedSnippets, int $version): void + { + $snippets = $this->api->getSnippets($version); + + $currentActiveSnippets = []; + foreach ($snippets as $item) { + $currentActiveSnippets[] = $item->name; + } + $snippetsForDelete = array_diff($currentActiveSnippets, $allowedSnippets); + + foreach ($snippetsForDelete as $snippetName) { + //remove only snippet name which starts with magento prefix + if (strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') === 0) { + $this->api->removeSnippet($version, $snippetName); + } + } + } } diff --git a/Model/Api.php b/Model/Api.php index 853e94e2..b009e8a1 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -500,6 +500,21 @@ public function activateVersion($version) return $result; } + /** + * Get all VCL snippets + * + * @param $version + * @return bool|mixed + * @throws LocalizedException + */ + public function getSnippets($version) + { + $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/snippet'; + $result = $this->_fetch($url, 'GET'); + + return $result; + } + /** * Creating and updating regular VCL snippets *