Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor: Use DesignInterface for Hyvä Theme Detection in ProductGalleryHelper #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 38 additions & 57 deletions Helper/ProductGalleryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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',
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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] = [];
}
Expand Down Expand Up @@ -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
*/
Expand Down