Skip to content

Commit 8abff0d

Browse files
authored
Merge pull request #250 from Inchoo/feature/#215-snippets-removal
Feature/#215 snippets removal
2 parents 3fe2f6e + a38a8a1 commit 8abff0d

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

Diff for: Controller/Adminhtml/FastlyCdn/Manifest/ToggleModules.php

+73-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
use Magento\Framework\App\Action\Context;
1212
use Magento\Framework\App\Request\Http;
1313
use Fastly\Cdn\Model\ResourceModel\Manifest\CollectionFactory;
14+
use Fastly\Cdn\Model\Api;
15+
use Fastly\Cdn\Helper\Vcl;
16+
use Fastly\Cdn\Model\Config;
17+
use Magento\Framework\Exception\LocalizedException;
1418

1519
/**
1620
* Class Create
@@ -23,30 +27,38 @@ class ToggleModules extends Action
2327
* @var ManifestFactory
2428
*/
2529
private $manifestFactory;
26-
2730
/**
2831
* @var Manifest
2932
*/
3033
private $manifest;
31-
3234
/**
3335
* @var Modly
3436
*/
3537
private $modly;
36-
3738
/**
3839
* @var ManifestResource
3940
*/
4041
private $manifestResource;
41-
4242
/**
4343
* @var JsonFactory
4444
*/
4545
private $resultJson;
46-
46+
/**
47+
* @var Http
48+
*/
4749
private $request;
48-
50+
/**
51+
* @var CollectionFactory
52+
*/
4953
private $collectionFactory;
54+
/**
55+
* @var Api
56+
*/
57+
private $api;
58+
59+
private $vcl;
60+
61+
private $enabledModules = [];
5062

5163
public function __construct(
5264
Context $context,
@@ -56,7 +68,9 @@ public function __construct(
5668
Modly $modly,
5769
JsonFactory $resultJsonFactory,
5870
Http $request,
59-
CollectionFactory $collectionFactory
71+
CollectionFactory $collectionFactory,
72+
Api $api,
73+
Vcl $vcl
6074
) {
6175
$this->manifestFactory = $manifestFactory;
6276
$this->manifestResource = $manifestResource;
@@ -65,6 +79,8 @@ public function __construct(
6579
$this->resultJson = $resultJsonFactory;
6680
$this->request = $request;
6781
$this->collectionFactory = $collectionFactory;
82+
$this->api = $api;
83+
$this->vcl = $vcl;
6884
parent::__construct($context);
6985
}
7086

@@ -85,12 +101,22 @@ public function execute()
85101
$manifest->setManifestId($moduleId);
86102
$manifest->setManifestStatus(1);
87103
} else {
104+
if ($module['manifest_status'] == 1) {
105+
$this->enabledModules[$moduleId] = $module['manifest_vcl'];
106+
}
88107
$manifest->setManifestId($moduleId);
89108
$manifest->setManifestStatus(0);
90109
}
91110
$this->saveManifest($manifest);
92111
}
93112

113+
if ($this->enabledModules) {
114+
$removeStatus = $this->removeManifests($this->enabledModules);
115+
if ($removeStatus != false) {
116+
throw new LocalizedException($removeStatus);
117+
}
118+
}
119+
94120
return $result->setData([
95121
'status' => true
96122
]);
@@ -110,4 +136,44 @@ private function saveManifest($manifest)
110136
{
111137
$this->manifestResource->save($manifest);
112138
}
139+
140+
/**
141+
* @param $enabledModules
142+
* @return bool|\Exception
143+
*/
144+
private function removeManifests($enabledModules)
145+
{
146+
try {
147+
$service = $this->api->checkServiceDetails();
148+
$activeVersion = $this->vcl->getCurrentVersion($service->versions);
149+
$existingSnippets = [];
150+
151+
foreach ($enabledModules as $key => $value) {
152+
$moduleVcl = json_decode($value);
153+
foreach ($moduleVcl as $vcl) {
154+
$type = $vcl->type;
155+
$reqName = Config::FASTLY_MODLY_MODULE . '_' . $key . '_' . $type;
156+
$checkIfSnippetExist = $this->api->hasSnippet($activeVersion, $reqName);
157+
if ($checkIfSnippetExist) {
158+
$existingSnippets[] = $reqName;
159+
}
160+
}
161+
}
162+
163+
if ($existingSnippets) {
164+
$clone = $this->api->cloneVersion($activeVersion);
165+
foreach ($existingSnippets as $snippet) {
166+
$this->api->removeSnippet($clone->number, $snippet);
167+
}
168+
$this->api->validateServiceVersion($clone->number);
169+
$this->api->activateVersion($clone->number);
170+
171+
$comment = ['comment' => 'Magento Module deleted Fastly Edge Module snippets.'];
172+
$this->api->addComment($clone->number, $comment);
173+
}
174+
return false;
175+
} catch (\Exception $e) {
176+
return $e;
177+
}
178+
}
113179
}

0 commit comments

Comments
 (0)