4
4
namespace Fastly \Cdn \Plugin \GraphQl ;
5
5
6
6
use Fastly \Cdn \Model \Config ;
7
+ use Magento \Framework \App \ObjectManager ;
7
8
use Magento \Framework \App \ResponseInterface ;
8
9
use Magento \Framework \Controller \ResultInterface ;
9
- use Magento \GraphQlCache \ Model \ CacheableQuery ;
10
+ use Magento \Framework \ Module \ Manager ;
10
11
11
12
class AfterRenderResult
12
13
{
@@ -16,22 +17,22 @@ class AfterRenderResult
16
17
private $ config ;
17
18
18
19
/**
19
- * @var CacheableQuery
20
+ * @var Manager
20
21
*/
21
- private $ cacheableQuery ;
22
+ private $ moduleManager ;
22
23
23
24
/**
24
25
* AfterRenderResult constructor.
25
26
*
26
27
* @param Config $config
27
- * @param CacheableQuery $cacheableQuery
28
+ * @param Manager $moduleManager
28
29
*/
29
30
public function __construct (
30
31
Config $ config ,
31
- CacheableQuery $ cacheableQuery
32
+ Manager $ moduleManager
32
33
) {
33
34
$ this ->config = $ config ;
34
- $ this ->cacheableQuery = $ cacheableQuery ;
35
+ $ this ->moduleManager = $ moduleManager ;
35
36
}
36
37
37
38
/**
@@ -47,21 +48,29 @@ public function afterRenderResult(
47
48
ResultInterface $ result ,
48
49
ResponseInterface $ response
49
50
): ResultInterface {
50
- if ($ this ->config ->isEnabled ()
51
- && $ this ->config ->getType () === Config::FASTLY
52
- && $ this ->cacheableQuery ->isCacheable ()) {
53
- $ header = $ response ->getHeader ('cache-control ' );
54
51
55
- if ($ header ) {
56
- if ($ ttl = $ this ->config ->getStaleTtl ()) {
57
- $ header ->addDirective ('stale-while-revalidate ' , $ ttl );
58
- }
52
+ if (!$ this ->config ->isEnabled () || !($ this ->config ->getType () === Config::FASTLY )) {
53
+ return $ result ;
54
+ }
55
+
56
+ if (!$ this ->moduleManager ->isEnabled ('Magento_GraphQlCache ' ) ||
57
+ !ObjectManager::getInstance ()->get (\Magento \GraphQlCache \Model \CacheableQuery::class)->isCacheable ()
58
+ ) {
59
+ return $ result ;
60
+ }
61
+
62
+ $ header = $ response ->getHeader ('cache-control ' );
59
63
60
- if ($ ttl = $ this ->config ->getStaleErrorTtl ()) {
61
- $ header ->addDirective ('stale-if-error ' , $ ttl );
62
- }
64
+ if ($ header ) {
65
+ if ($ ttl = $ this ->config ->getStaleTtl ()) {
66
+ $ header ->addDirective ('stale-while-revalidate ' , $ ttl );
67
+ }
68
+
69
+ if ($ ttl = $ this ->config ->getStaleErrorTtl ()) {
70
+ $ header ->addDirective ('stale-if-error ' , $ ttl );
63
71
}
64
72
}
73
+
65
74
return $ result ;
66
75
}
67
76
}
0 commit comments