diff --git a/Api/CmsBlockRepositoryInterface.php b/Api/CmsBlockRepositoryInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..8e989349f2012d303874e56a8ab363f3225f86e6 --- /dev/null +++ b/Api/CmsBlockRepositoryInterface.php @@ -0,0 +1,14 @@ +<?php +declare(strict_types=1); +namespace Firegento\ContentProvisioning\Api; + +interface CmsBlockRepositoryInterface +{ + /** + * Get CMS Block data by its identifier. + * + * @param string $identifier + * @return array + */ + public function getByIdentifier(string $identifier): array; +} \ No newline at end of file diff --git a/Model/Query/GetCmsBlockByIdentifier.php b/Model/Query/GetCmsBlockByIdentifier.php new file mode 100644 index 0000000000000000000000000000000000000000..e5cba173a0fb645881bd6c49195ea1a633e92c7d --- /dev/null +++ b/Model/Query/GetCmsBlockByIdentifier.php @@ -0,0 +1,28 @@ +<?php +declare(strict_types=1); +namespace Firegento\ContentProvisioning\Model\Query; + +use Magento\Framework\App\State; +use Firegento\ContentProvisioning\Api\CmsBlockRepositoryInterface; + +class GetCmsBlockByIdentifier +{ + private $cmsBlockRepository; + + public function __construct( + CmsBlockRepositoryInterface $cmsBlockRepository + ) { + $this->cmsBlockRepository = $cmsBlockRepository; + } + + protected function execute(string $key) + { + $blockData = $this->cmsBlockRepository->getByIdentifier($key); + + if (empty($blockData)) { + throw new NotFoundException(__('Block with key %1 not found.', $key)); + } + + return $blockData; + } +} \ No newline at end of file diff --git a/Model/Repository/CmsBlockRepository.php b/Model/Repository/CmsBlockRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..fcd249d8863d55195bdba210ac551cb7615c0d57 --- /dev/null +++ b/Model/Repository/CmsBlockRepository.php @@ -0,0 +1,43 @@ +<?php +declare(strict_types=1); +namespace Firegento\ContentProvisioning\Model\Repository; + +use Firegento\ContentProvisioning\Api\CmsBlockRepositoryInterface; +use Magento\Framework\App\ResourceConnection; + +class CmsBlockRepository implements CmsBlockRepositoryInterface +{ + /** + * @var ResourceConnection + */ + private $resource; + + /** + * CmsBlockRepository constructor. + * + * @param ResourceConnection $resource + */ + public function __construct( + ResourceConnection $resource + ) { + $this->resource = $resource; + } + + /** + * Get CMS Block data by its identifier. + * + * @param string $identifier + * @return array + */ + public function getByIdentifier(string $identifier): array + { + $connection = $this->resource->getConnection(); + $tableName = $this->resource->getTableName('cms_block'); // Get table with prefix + + $sql = "SELECT * FROM $tableName WHERE identifier = :identifier"; + $bind = ['identifier' => $identifier]; + + $result = $connection->fetchRow($sql, $bind); // Fetch a single row + return $result ? $result : []; + } +} \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index cea4de44c362a28b4123c606938200affe312fe4..d3e79e8442a20dd93d9ea76d5ab7c0ff79d35a4d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -148,4 +148,10 @@ </argument> </arguments> </type> + <type name="Firegento\ContentProvisioning\Api\CmsBlockRepositoryInterface"> + <arguments> + <argument name="resource" xsi:type="object">Magento\Framework\App\ResourceConnection</argument> + </arguments> + </type> + <preference for="Firegento\ContentProvisioning\Api\CmsBlockRepositoryInterface" type="Firegento\ContentProvisioning\Model\Repository\CmsBlockRepository" /> </config>