44namespace Lof \Webp2 \Config ;
55
66use Magento \Framework \App \Config \ScopeConfigInterface ;
7+ use Magento \Framework \Exception \NoSuchEntityException ;
78use Magento \Framework \View \Element \Block \ArgumentInterface ;
9+ use Magento \Store \Model \ScopeInterface ;
10+ use Magento \Store \Model \StoreManagerInterface ;
11+ use Lof \Webp2 \Exception \InvalidConvertorException ;
812
913class Config implements ArgumentInterface
1014{
@@ -13,31 +17,39 @@ class Config implements ArgumentInterface
1317 */
1418 private $ scopeConfig ;
1519
20+ /**
21+ * @var StoreManagerInterface
22+ */
23+ private $ storeManager ;
24+
1625 /**
1726 * Config constructor.
1827 *
1928 * @param ScopeConfigInterface $scopeConfig
29+ * @param StoreManagerInterface $storeManager
2030 */
2131 public function __construct (
22- ScopeConfigInterface $ scopeConfig
32+ ScopeConfigInterface $ scopeConfig ,
33+ StoreManagerInterface $ storeManager
2334 ) {
2435 $ this ->scopeConfig = $ scopeConfig ;
36+ $ this ->storeManager = $ storeManager ;
2537 }
2638
2739 /**
2840 * @return bool
2941 */
3042 public function enabled (): bool
3143 {
32- return (bool )$ this ->scopeConfig -> getValue ('lof_webp2/settings/enabled ' );
44+ return (bool )$ this ->getValue ('lof_webp2/settings/enabled ' );
3345 }
3446
3547 /**
3648 * @return int
3749 */
3850 public function getQualityLevel (): int
3951 {
40- $ qualityLevel = (int )$ this ->scopeConfig -> getValue ('lof_webp2/settings/quality_level ' );
52+ $ qualityLevel = (int )$ this ->getValue ('lof_webp2/settings/quality_level ' );
4153 if ($ qualityLevel > 100 ) {
4254 return 100 ;
4355 }
@@ -51,9 +63,77 @@ public function getQualityLevel(): int
5163
5264 /**
5365 * @return string[]
66+ * @throws InvalidConvertorException
5467 */
5568 public function getConvertors (): array
5669 {
57- return ['cwebp ' , 'gd ' , 'imagick ' , 'wpc ' , 'ewww ' ];
70+ $ allConvertors = ['cwebp ' , 'gd ' , 'imagick ' , 'wpc ' , 'ewww ' ];
71+ $ storedConvertors = $ this ->getValue ('lof_webp2/settings/convertors ' );
72+ $ storedConvertors = $ this ->stringToArray ((string )$ storedConvertors );
73+ if (empty ($ storedConvertors )) {
74+ return $ allConvertors ;
75+ }
76+
77+ foreach ($ storedConvertors as $ storedConvertor ) {
78+ if (!in_array ($ storedConvertor , $ allConvertors )) {
79+ throw new InvalidConvertorException ('Invalid convertor: " ' . $ storedConvertor . '" ' );
80+ }
81+ }
82+
83+ return $ storedConvertors ;
84+ }
85+
86+ /**
87+ * @return string
88+ * @throws InvalidConvertorException
89+ */
90+ public function getEncoding (): string
91+ {
92+ $ allEncoding = ['lossy ' , 'lossless ' , 'auto ' ];
93+ $ storedEncoding = (string )$ this ->getValue ('lof_webp2/settings/encoding ' );
94+ if (empty ($ storedEncoding )) {
95+ return 'lossy ' ;
96+ }
97+
98+ if (!in_array ($ storedEncoding , $ allEncoding )) {
99+ throw new InvalidConvertorException ('Invalid encoding: " ' . $ storedEncoding . '" ' );
100+ }
101+
102+ return $ storedEncoding ;
103+ }
104+
105+ /**
106+ * @param string $path
107+ * @return mixed
108+ */
109+ private function getValue (string $ path )
110+ {
111+ try {
112+ return $ this ->scopeConfig ->getValue (
113+ $ path ,
114+ ScopeInterface::SCOPE_STORE ,
115+ $ this ->storeManager ->getStore ()
116+ );
117+ } catch (NoSuchEntityException $ e ) {
118+ return null ;
119+ }
120+ }
121+
122+ /**
123+ * @param string $string
124+ * @return array
125+ */
126+ private function stringToArray (string $ string ): array
127+ {
128+ $ array = [];
129+ $ strings = explode (', ' , $ string );
130+ foreach ($ strings as $ string ) {
131+ $ string = trim ($ string );
132+ if ($ string ) {
133+ $ array [] = $ string ;
134+ }
135+ }
136+
137+ return $ array ;
58138 }
59139}
0 commit comments