Files
esun3d/app/code/Esun/Homepage/Block/InnovativeMaterials.php
2026-09-15 10:35:56 +08:00

79 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Homepage\Block;
use Esun\ImageManager\Model\Category;
use Esun\ImageManager\Model\Image;
use Esun\ImageManager\Model\ImageProvider;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\ScopeInterface;
class InnovativeMaterials extends Template implements IdentityInterface
{
private const XML_PATH_CATEGORY_ID = 'esun_homepage/innovative_materials/category_id';
/**
* @var array<int, array<string, int|string>>|null
*/
private ?array $items = null;
public function __construct(
Context $context,
private readonly ImageProvider $imageProvider,
array $data = []
) {
parent::__construct($context, $data);
}
/**
* @return array<int, array<string, int|string>>
*/
public function getItems(): array
{
if ($this->items === null) {
$this->items = array_values(array_filter(
$this->imageProvider->getImagesByCategoryId($this->getCategoryId()),
static fn (array $item): bool => trim((string) $item['thumbnail_url']) !== ''
));
}
return $this->items;
}
public function getCacheKeyInfo(): array
{
return [
'ESUN_HOMEPAGE_INNOVATIVE_MATERIALS',
(int) $this->_storeManager->getStore()->getId(),
$this->getCategoryId(),
];
}
public function getIdentities(): array
{
$identities = [];
$categoryId = $this->getCategoryId();
if ($categoryId > 0) {
$identities[] = Category::CACHE_TAG . '_' . $categoryId;
}
foreach ($this->getItems() as $item) {
$identities[] = Image::CACHE_TAG . '_' . (int) $item['id'];
}
return array_values(array_unique($identities));
}
private function getCategoryId(): int
{
return (int) $this->_scopeConfig->getValue(
self::XML_PATH_CATEGORY_ID,
ScopeInterface::SCOPE_STORE
);
}
}