1+ <?php
2+ namespace Experius \WysiwygDownloads \Setup \Patch \Data ;
3+
4+ use Magento \Framework \Setup \ModuleDataSetupInterface ;
5+ use Magento \Framework \Setup \Patch \DataPatchInterface ;
6+ use Magento \Framework \Setup \Patch \PatchRevertableInterface ;
7+ use Magento \Framework \App \Config \ScopeConfigInterface ;
8+ use Magento \Framework \App \Config \Storage \WriterInterface ;
9+ use Magento \MediaStorage \Model \File \Validator \NotProtectedExtension ;
10+
11+ class ProtectedExtensions implements DataPatchInterface, PatchRevertableInterface
12+ {
13+ /**
14+ * @var ModuleDataSetupInterface
15+ */
16+ private $ moduleDataSetup ;
17+
18+ /**
19+ * @var ScopeConfigInterface
20+ */
21+ private $ scopeConfig ;
22+
23+ /**
24+ * @var WriterInterface
25+ */
26+ private $ configWriter ;
27+
28+ /**
29+ * @var \Magento\Store\Model\StoreManagerInterface
30+ */
31+ private $ storeManager ;
32+
33+ /**
34+ * @param ModuleDataSetupInterface $moduleDataSetup
35+ */
36+ public function __construct (
37+ ModuleDataSetupInterface $ moduleDataSetup ,
38+ ScopeConfigInterface $ scopeConfig ,
39+ WriterInterface $ configWriter ,
40+ \Magento \Store \Model \StoreManagerInterface $ storeManager
41+ ) {
42+ $ this ->moduleDataSetup = $ moduleDataSetup ;
43+ $ this ->scopeConfig = $ scopeConfig ;
44+ $ this ->configWriter = $ configWriter ;
45+ $ this ->storeManager = $ storeManager ;
46+ }
47+
48+ /**
49+ * @inheritdoc
50+ */
51+ public function apply ()
52+ {
53+ $ this ->moduleDataSetup ->getConnection ()->startSetup ();
54+ $ protectedExtensions = $ this ->getCurrentValue ();
55+ $ protectedExtensions = array_diff ($ protectedExtensions , ['svg ' , 'svgz ' ]);
56+ $ protectedExtensions = implode (', ' , $ protectedExtensions );
57+ $ this ->configWriter ->save (
58+ NotProtectedExtension::XML_PATH_PROTECTED_FILE_EXTENSIONS ,
59+ $ protectedExtensions ,
60+ 'default ' ,
61+ 'default '
62+ );
63+ $ this ->moduleDataSetup ->getConnection ()->endSetup ();
64+ }
65+
66+ /**
67+ * @inheritdoc
68+ */
69+ public static function getDependencies ()
70+ {
71+ return [];
72+ }
73+
74+ public function revert ()
75+ {
76+ $ this ->moduleDataSetup ->getConnection ()->startSetup ();
77+ $ protectedExtensions = $ this ->getCurrentValue ();
78+ $ protectedExtensions = array_merge ($ protectedExtensions , ['svg ' , 'svgz ' ]);
79+ $ this ->configWriter ->save (
80+ NotProtectedExtension::XML_PATH_PROTECTED_FILE_EXTENSIONS ,
81+ implode (', ' , $ protectedExtensions ),
82+ 'default ' ,
83+ 'default '
84+ );
85+ $ this ->moduleDataSetup ->getConnection ()->endSetup ();
86+ }
87+
88+ public function getCurrentValue ()
89+ {
90+ $ configValue = $ this ->scopeConfig ->getValue (
91+ NotProtectedExtension::XML_PATH_PROTECTED_FILE_EXTENSIONS ,
92+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
93+ );
94+ if (is_string ($ configValue )) {
95+ return explode (', ' , $ configValue );
96+ }
97+ return $ configValue ?? [];
98+ }
99+
100+ /**
101+ * @inheritdoc
102+ */
103+ public function getAliases ()
104+ {
105+ return [];
106+ }
107+ }
0 commit comments