From 44baadb509286bc5fae514f6c1d3dde2cf3e447b Mon Sep 17 00:00:00 2001
From: Itay Raz <itay@studioraz.co.il>
Date: Sun, 16 Mar 2025 08:56:15 +0200
Subject: [PATCH] =?UTF-8?q?Refactor:=20Use=20DesignInterface=20for=20Hyv?=
 =?UTF-8?q?=C3=A4=20Theme=20Detection=20in=20ProductGalleryHelper?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Helper/ProductGalleryHelper.php | 95 +++++++++++++--------------------
 1 file changed, 38 insertions(+), 57 deletions(-)

diff --git a/Helper/ProductGalleryHelper.php b/Helper/ProductGalleryHelper.php
index 4c65591..1807f1d 100644
--- a/Helper/ProductGalleryHelper.php
+++ b/Helper/ProductGalleryHelper.php
@@ -4,13 +4,10 @@
 
 use Cloudinary\Cloudinary\Core\ConfigurationInterface;
 use Magento\Framework\App\Helper\Context;
-use Magento\Theme\Model\Theme\ThemeProvider;
-use Magento\Store\Model\StoreManagerInterface;
-use Magento\Framework\App\Config\ScopeConfigInterface;
-use Magento\Store\Model\ScopeInterface;
+use Magento\Framework\View\DesignInterface;
+
 class ProductGalleryHelper extends \Magento\Framework\App\Helper\AbstractHelper
 {
-
     /**
      * @var ConfigurationInterface
      */
@@ -22,6 +19,11 @@ class ProductGalleryHelper extends \Magento\Framework\App\Helper\AbstractHelper
      */
     protected $cloudinaryPGoptions;
 
+    /**
+     * @var DesignInterface
+     */
+    protected DesignInterface $viewDesign;
+
     protected $_casting = [
         'themeProps_primary' => 'string',
         'themeProps_onPrimary' => 'string',
@@ -48,38 +50,37 @@ class ProductGalleryHelper extends \Magento\Framework\App\Helper\AbstractHelper
     ];
 
     /**
-     * @var ScopeConfigInterface
+     * @param Context $context
+     * @param ConfigurationInterface $configuration
+     * @param DesignInterface $viewDesign
      */
-    protected $scopeConfig;
-    /**
-     * @var ThemeList
-     */
-    protected $themeProvider;
-
-    protected $storeManager;
-
-
     public function __construct(
         Context $context,
         ConfigurationInterface $configuration,
-        StoreManagerInterface $storeManager,
-        ScopeConfigInterface $scopeConfig,
-        ThemeProvider $themeProvider
+        DesignInterface $viewDesign
     ) {
         parent::__construct($context);
         $this->configuration = $configuration;
-        $this->storeManager = $storeManager;
-        $this->scopeConfig = $scopeConfig;
-        $this->themeProvider = $themeProvider;
+        $this->viewDesign = $viewDesign;
     }
 
-    protected function LogToFile($msg, $file = '/var/log/cloudinary.log')
+    /**
+     * Check if Hyvä Theme is active
+     *
+     * @return bool
+     */
+    public function isHyvaThemeEnabled(): bool
     {
-        $writer = new \Zend_Log_Writer_Stream(BP . $file);
-        $logger = new \Zend_Log();
-        $logger->addWriter($writer);
+        $theme = $this->viewDesign->getDesignTheme();
 
-        $logger->info(print_r($msg, true));
+        while ($theme) {
+            if (strpos($theme->getCode(), 'Hyva/') === 0) {
+                return true;
+            }
+            $theme = $theme->getParentTheme();
+        }
+
+        return false;
     }
 
     /**
@@ -90,16 +91,17 @@ protected function LogToFile($msg, $file = '/var/log/cloudinary.log')
      */
     public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false)
     {
-        if ((is_null($this->cloudinaryPGoptions) || $refresh) && ($ignoreDisabled || ($this->configuration->isEnabled() && $this->configuration->isEnabledProductGallery()))) {
+        if ((is_null($this->cloudinaryPGoptions) || $refresh) &&
+            ($ignoreDisabled || ($this->configuration->isEnabled() && $this->configuration->isEnabledProductGallery()))
+        ) {
             $this->cloudinaryPGoptions = $this->configuration->getProductGalleryAll();
 
-
-            if ($this->configuration->isEnabledCldVideo()){
-
+            if ($this->configuration->isEnabledCldVideo()) {
                 $transformation = [];
                 $videoSettings = $this->configuration->getAllVideoSettings();
                 $videoFreeParams = $videoSettings['video_free_params'] ?? null;
                 $videoControls = $videoSettings['controls'] ?? "none";
+
                 if ($videoFreeParams) {
                     $config = json_decode($videoFreeParams, true);
                     $config = array_shift($config);
@@ -113,15 +115,12 @@ public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false
                         'controls' => $videoControls,
                         'chapters' => false,
                         'muted' => false
-
                     ];
                     $autoplayMode = $videoSettings['autoplay'] ?? null;
                     $config['autoplayMode'] = $autoplayMode;
                     if ($autoplayMode && $autoplayMode != 'never') {
                         $config['autoplay'] = true;
-
                         $config['muted'] = true;
-
                     } else {
                         $config['autoplay'] = false;
                     }
@@ -154,15 +153,15 @@ public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false
             }
 
             foreach ($this->cloudinaryPGoptions as $key => $value) {
-                //Change casting
+                // Change casting
                 if (isset($this->_casting[$key])) {
                     \settype($value, $this->_casting[$key]);
                     $this->cloudinaryPGoptions[$key] = $value;
                 }
-                //Build options hierarchy
+                // Build options hierarchy
                 $path = explode("_", $key);
                 $_path = $path[0];
-                if (in_array($_path, ['themeProps','zoomProps','thumbnailProps','indicatorProps'])) {
+                if (in_array($_path, ['themeProps', 'zoomProps', 'thumbnailProps', 'indicatorProps'])) {
                     if (!isset($this->cloudinaryPGoptions[$_path])) {
                         $this->cloudinaryPGoptions[$_path] = [];
                     }
@@ -197,33 +196,15 @@ public function getCloudName()
         return (string)$this->configuration->getCloud();
     }
 
-
+    /**
+     * @return string
+     */
     public function getCname()
     {
         $config = $this->configuration->getCredentials();
         return ($config['cname']) ?? '';
     }
 
-    /**
-     * Check if Hyvä Theme is active
-     *
-     * @return bool
-     */
-    public function isHyvaThemeEnabled()
-    {
-
-        $themeId = $this->scopeConfig->getValue(
-            'design/theme/theme_id',
-            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
-            $this->storeManager->getStore()->getId()
-        );
-
-        $theme = $this->themeProvider->getThemeById($themeId);
-        return $theme && strpos($theme->getCode(), 'Hyva/') === 0;
-    }
-
-
-
     /**
      * @return bool
      */