Files
ltsecurityinc/app/code/Lts/HomePage/Setup/Patch/Data/AddResourcesBlockToHomePage.php
T

73 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Lts\HomePage\Setup\Patch\Data;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class AddResourcesBlockToHomePage implements DataPatchInterface
{
private const PAGE_IDENTIFIER = 'athlete2-home-greece';
private const CATEGORY_BLOCK = '{{block class="Magento\\Cms\\Block\\Block" block_id="lts-home-shop-by-category"}}';
private const RESOURCES_BLOCK = '{{block class="Magento\\Cms\\Block\\Block" block_id="lts-home-resources"}}';
public function __construct(
private readonly PageFactory $pageFactory
) {
}
public function apply(): self
{
$page = $this->pageFactory->create()->getCollection()
->addFieldToFilter('identifier', self::PAGE_IDENTIFIER)
->addStoreFilter(0, false)
->setPageSize(1)
->getFirstItem();
if (!$page->getId()) {
return $this;
}
$content = (string) $page->getContent();
if (str_contains($content, self::RESOURCES_BLOCK)) {
return $this;
}
if (str_contains($content, self::CATEGORY_BLOCK)) {
$content = str_replace(
self::CATEGORY_BLOCK,
self::CATEGORY_BLOCK . PHP_EOL . ' ' . self::RESOURCES_BLOCK,
$content
);
} else {
$feedbackPosition = strpos($content, '<a class="lts-feedback"');
if ($feedbackPosition === false) {
$content .= PHP_EOL . self::RESOURCES_BLOCK;
} else {
$content = substr($content, 0, $feedbackPosition)
. self::RESOURCES_BLOCK
. PHP_EOL
. ' '
. substr($content, $feedbackPosition);
}
}
$page->setContent($content)->save();
return $this;
}
public static function getDependencies(): array
{
return [CreateShopByCategoryBlock::class];
}
public function getAliases(): array
{
return [];
}
}