11
11
use Magento \Framework \App \Action \Context ;
12
12
use Magento \Framework \App \Request \Http ;
13
13
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 ;
14
18
15
19
/**
16
20
* Class Create
@@ -23,30 +27,38 @@ class ToggleModules extends Action
23
27
* @var ManifestFactory
24
28
*/
25
29
private $ manifestFactory ;
26
-
27
30
/**
28
31
* @var Manifest
29
32
*/
30
33
private $ manifest ;
31
-
32
34
/**
33
35
* @var Modly
34
36
*/
35
37
private $ modly ;
36
-
37
38
/**
38
39
* @var ManifestResource
39
40
*/
40
41
private $ manifestResource ;
41
-
42
42
/**
43
43
* @var JsonFactory
44
44
*/
45
45
private $ resultJson ;
46
-
46
+ /**
47
+ * @var Http
48
+ */
47
49
private $ request ;
48
-
50
+ /**
51
+ * @var CollectionFactory
52
+ */
49
53
private $ collectionFactory ;
54
+ /**
55
+ * @var Api
56
+ */
57
+ private $ api ;
58
+
59
+ private $ vcl ;
60
+
61
+ private $ enabledModules = [];
50
62
51
63
public function __construct (
52
64
Context $ context ,
@@ -56,7 +68,9 @@ public function __construct(
56
68
Modly $ modly ,
57
69
JsonFactory $ resultJsonFactory ,
58
70
Http $ request ,
59
- CollectionFactory $ collectionFactory
71
+ CollectionFactory $ collectionFactory ,
72
+ Api $ api ,
73
+ Vcl $ vcl
60
74
) {
61
75
$ this ->manifestFactory = $ manifestFactory ;
62
76
$ this ->manifestResource = $ manifestResource ;
@@ -65,6 +79,8 @@ public function __construct(
65
79
$ this ->resultJson = $ resultJsonFactory ;
66
80
$ this ->request = $ request ;
67
81
$ this ->collectionFactory = $ collectionFactory ;
82
+ $ this ->api = $ api ;
83
+ $ this ->vcl = $ vcl ;
68
84
parent ::__construct ($ context );
69
85
}
70
86
@@ -85,12 +101,22 @@ public function execute()
85
101
$ manifest ->setManifestId ($ moduleId );
86
102
$ manifest ->setManifestStatus (1 );
87
103
} else {
104
+ if ($ module ['manifest_status ' ] == 1 ) {
105
+ $ this ->enabledModules [$ moduleId ] = $ module ['manifest_vcl ' ];
106
+ }
88
107
$ manifest ->setManifestId ($ moduleId );
89
108
$ manifest ->setManifestStatus (0 );
90
109
}
91
110
$ this ->saveManifest ($ manifest );
92
111
}
93
112
113
+ if ($ this ->enabledModules ) {
114
+ $ removeStatus = $ this ->removeManifests ($ this ->enabledModules );
115
+ if ($ removeStatus != false ) {
116
+ throw new LocalizedException ($ removeStatus );
117
+ }
118
+ }
119
+
94
120
return $ result ->setData ([
95
121
'status ' => true
96
122
]);
@@ -110,4 +136,44 @@ private function saveManifest($manifest)
110
136
{
111
137
$ this ->manifestResource ->save ($ manifest );
112
138
}
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
+ }
113
179
}
0 commit comments