init
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user