Files
2026-09-15 10:35:56 +08:00

91 lines
2.3 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 Application extends Template implements IdentityInterface
{
private const XML_PATH = 'esun_homepage/application/';
/**
* @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);
}
public function getSubtitle(): string
{
return trim((string) $this->getConfig('subtitle'));
}
public function getExploreAllUrl(): string
{
return trim((string) $this->getConfig('explore_all_url'));
}
/**
* @return array<int, array<string, int|string>>
*/
public function getItems(): array
{
if ($this->items === null) {
$this->items = $this->imageProvider->getImagesByCategoryId($this->getCategoryId());
}
return $this->items;
}
public function getCacheKeyInfo(): array
{
return [
'ESUN_HOMEPAGE_APPLICATION',
(int) $this->_storeManager->getStore()->getId(),
$this->getCategoryId(),
sha1($this->getSubtitle()),
sha1($this->getExploreAllUrl()),
];
}
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->getConfig('category_id');
}
private function getConfig(string $field): mixed
{
return $this->_scopeConfig->getValue(
self::XML_PATH . $field,
ScopeInterface::SCOPE_STORE
);
}
}