diff --git a/Cron/InconsistenciesInBlockEmail.php b/Cron/InconsistenciesInBlockEmail.php index ce51355a2f5fc607b303757d7088aa9f1824c50b..031425128249b83fcf518d3b0d21e86d2731bbb7 100644 --- a/Cron/InconsistenciesInBlockEmail.php +++ b/Cron/InconsistenciesInBlockEmail.php @@ -19,7 +19,8 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\Cron; -use Firegento\ContentProvisioning\Model\Console\BlockListCommand; +use Firegento\ContentProvisioning\Model\Query\GetChangedBlockIdentifiers; +use Firegento\ContentProvisioning\Model\Query\GetFilesContentInformation; use Firegento\ContentProvisioning\Model\RenderChangedBlockList; use Magento\Framework\Mail\Message; use Psr\Log\LoggerInterface; @@ -39,11 +40,6 @@ class InconsistenciesInBlockEmail */ private LoggerInterface $logger; - /** - * @var BlockListCommand - */ - private BlockListCommand $blockList; - /** * @var TransportInterfaceFactory */ @@ -64,28 +60,41 @@ class InconsistenciesInBlockEmail */ private RenderChangedBlockList $renderChangedBlockList; + /** + * @var GetChangedBlockIdentifiers + */ + private GetChangedBlockIdentifiers $getChangedBlockIdentifier; + + /** + * @var GetFilesContentInformation + */ + private GetFilesContentInformation $getFilesContentInformation; + /** * @param LoggerInterface $logger - * @param BlockListCommand $blockList * @param TransportInterfaceFactory $mailTransportFactory * @param Message $message * @param ScopeConfigInterface $scopeConfig * @param RenderChangedBlockList $renderChangedBlockList + * @param GetFilesContentInformation $getFilesContentInformation + * @param GetChangedBlockIdentifiers $getChangedBlockIdentifier */ public function __construct( LoggerInterface $logger, - BlockListCommand $blockList, TransportInterfaceFactory $mailTransportFactory, Message $message, ScopeConfigInterface $scopeConfig, - RenderChangedBlockList $renderChangedBlockList + RenderChangedBlockList $renderChangedBlockList, + GetFilesContentInformation $getFilesContentInformation, + GetChangedBlockIdentifiers $getChangedBlockIdentifier ) { $this->logger = $logger; - $this->blockList = $blockList; $this->mailTransportFactory = $mailTransportFactory; $this->message = $message; $this->scopeConfig = $scopeConfig; $this->renderChangedBlockList = $renderChangedBlockList; + $this->getFilesContentInformation = $getFilesContentInformation; + $this->getChangedBlockIdentifier = $getChangedBlockIdentifier; } /** @@ -96,15 +105,30 @@ class InconsistenciesInBlockEmail */ public function execute(): void { - $changedBlocks = $this->blockList->getChangedEntries(); + $files = $this->getFilesContentInformation->execute(); + $changedBlocks = $this->getChangedBlockIdentifier->execute($files); if ($changedBlocks) { - $emailMessage = $this->renderChangedBlockList->getValuesForEmail($changedBlocks); + $emailMessage = $this->getValuesForEmail($changedBlocks); $this->sendEmail($emailMessage); } else { $this->logger->info("No changed blocks found."); } } + /** + * Transform array values into a string to be displayed in the email + * @param array $entries + * @return string + */ + public function getValuesForEmail(array $entries): string + { + $entriesList = []; + foreach ($entries as $entry) { + $entriesList[] = "key: " . $entry['key'] . ", identifier: " . $entry['identifier']; + } + return implode(PHP_EOL, $entriesList); + } + /** * @param string $message * @return void diff --git a/Model/Query/GetChangedBlockIdentifiers.php b/Model/Query/GetChangedBlockIdentifiers.php index 15d01666f7217a5aa9f6fc10bbb4f15e67b5cefc..da8e46314b477affc60387f4763b927a7cbaac62 100644 --- a/Model/Query/GetChangedBlockIdentifiers.php +++ b/Model/Query/GetChangedBlockIdentifiers.php @@ -18,7 +18,9 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\Model\Query; -use Magento\Framework\Exception\LocalizedException; +use Psr\Log\LoggerInterface; +use Exception; +use Firegento\ContentProvisioning\Model\DTO\FileDataDto; class GetChangedBlockIdentifiers { @@ -35,19 +37,23 @@ class GetChangedBlockIdentifiers /** * @param GetBlockEntryByKey $getBlockEntryByKey * @param GetCmsBlockByTitle $getCmsBlockByTitle + * @param LoggerInterface $logger */ public function __construct( GetBlockEntryByKey $getBlockEntryByKey, - GetCmsBlockByTitle $getCmsBlockByTitle + GetCmsBlockByTitle $getCmsBlockByTitle, + LoggerInterface $logger ) { $this->getBlockEntryByKey = $getBlockEntryByKey; $this->getCmsBlockByTitle = $getCmsBlockByTitle; + $this->logger = $logger; + } /** * Return array with changed blocks identifier * - * @param array $filesPath + * @param FileDataDto[] $filesPath * @return array|string */ public function execute(array $filesPath) @@ -56,12 +62,12 @@ class GetChangedBlockIdentifiers foreach ($filesPath as $file) { try { // Get Block content from code - $codeFileContent = $file['content']; + $codeFileContent = $file->getContent(); // Get Block content from database - $dbBlock = $this->getCmsBlockByTitle->execute($file['title']); + $dbBlock = $this->getCmsBlockByTitle->execute($file->getTitle()); $dbBlockContent = $dbBlock['content']; - } catch (LocalizedException $e) { - return 'ERROR: ' . $e->getMessage(); + } catch (Exception $e) { + return $this->logger->error($e->getMessage()); } if ($codeFileContent !== $dbBlockContent) { diff --git a/Model/Query/GetDbBlockIds.php b/Model/Query/GetDbBlockIds.php index af4e5ee99fc97d20867745bf904d6608d82e7569..99854a8f353d87ccfaadb1bde0112217263a641a 100644 --- a/Model/Query/GetDbBlockIds.php +++ b/Model/Query/GetDbBlockIds.php @@ -18,7 +18,9 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\Model\Query; -use Magento\Framework\Exception\LocalizedException; + +use Exception; +use Psr\Log\LoggerInterface; class GetDbBlockIds { @@ -35,13 +37,16 @@ class GetDbBlockIds /** * @param GetBlockEntryByKey $getBlockEntryByKey * @param GetBlocksByBlockEntry $getBlocksByBlockEntry + * @param LoggerInterface $logger */ public function __construct( GetBlockEntryByKey $getBlockEntryByKey, - GetBlocksByBlockEntry $getBlocksByBlockEntry + GetBlocksByBlockEntry $getBlocksByBlockEntry, + LoggerInterface $logger ) { $this->getBlockEntryByKey = $getBlockEntryByKey; $this->getBlocksByBlockEntry = $getBlocksByBlockEntry; + $this->logger = $logger; } /** @@ -59,8 +64,8 @@ class GetDbBlockIds } return implode(', ', $ids) . ')'; - } catch (LocalizedException $noSuchEntityException) { - return 'ERROR: ' . $noSuchEntityException->getMessage(); + } catch (Exception $e) { + return $this->logger->error($e->getMessage()); } } } diff --git a/Model/Query/GetFilesContentInformation.php b/Model/Query/GetFilesContentInformation.php index acd815b25dd0338ab2ecdf1e8d7be5e7f612e7e1..c29955b9c9c29f760d8a9c7ea8f1a4495399ecc1 100644 --- a/Model/Query/GetFilesContentInformation.php +++ b/Model/Query/GetFilesContentInformation.php @@ -20,7 +20,7 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\Model\Query; use Firegento\ContentProvisioning\Model\Config\Data; -use Magento\Framework\Exception\LocalizedException; +use Firegento\ContentProvisioning\Model\DTO\FileDataDtoFactory; class GetFilesContentInformation { @@ -31,31 +31,37 @@ class GetFilesContentInformation /** * @param Data $moduleConfig + * @param FileDataDtoFactory $fileDataDtoFactory */ public function __construct( - Data $moduleConfig + Data $moduleConfig, + FileDataDtoFactory $fileDataDtoFactory ) { $this->moduleConfig = $moduleConfig; + $this->fileDataDtoFactory = $fileDataDtoFactory; } /** * @return array * @SuppressWarnings(PHPMD.UnusedLocalVariable) - * @throws \Exception */ public function execute(): array { $results = []; - foreach ($this->moduleConfig->getList()['blocks'] as $fileData) { - $results[] = [ - 'identifier' => $fileData['identifier'], - 'title' => $fileData['title'], - 'key' => $fileData['key'], - 'content' => $fileData['content'], - 'stores' => implode(", ", $fileData['stores']), - 'is_active' => $fileData['is_active'], - 'is_maintained' => $fileData['is_maintained'] - ]; + $blockList = $this->moduleConfig->getList()['blocks']; + if (isset($blockList)) { + foreach ($blockList as $fileData) { + $changedBlockDto = $this->fileDataDtoFactory->create(); + $changedBlockDto->setIdentifier((string)$fileData['identifier']); + $changedBlockDto->setTitle((string)$fileData['title']); + $changedBlockDto->setKey((string)$fileData['key']); + $changedBlockDto->setContent((string)$fileData['content']); + $changedBlockDto->setIsActive((bool)$fileData['is_active']); + $changedBlockDto->setStore(implode(", ", $fileData['stores'])); + $changedBlockDto->setIsMaintained((bool)$fileData['is_maintained']); + + $results[] = $changedBlockDto; + } } return $results; } diff --git a/Model/RenderChangedBlockList.php b/Model/RenderChangedBlockList.php index e797ddac0ace755d7eb22f7d9edc8e9c99228208..f36b9fa573bd4cb5453f20a93797ff064ba46420 100644 --- a/Model/RenderChangedBlockList.php +++ b/Model/RenderChangedBlockList.php @@ -19,7 +19,6 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\Model; use Firegento\ContentProvisioning\ViewModel\ChangedBlockDataProvider; -use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -50,18 +49,4 @@ class RenderChangedBlockList { $this->changedBlockDataProvider->execute($input, $output, $entries); } - - /** - * Transform array values into a string to be displayed in the email - * @param array $entries - * @return string - */ - public function getValuesForEmail(array $entries): string - { - $entriesList = []; - foreach ($entries as $entry) { - $entriesList[] = "key: " . $entry['key'] . ", identifier: " . $entry['identifier']; - } - return implode(PHP_EOL, $entriesList); - } } diff --git a/Model/Repository/CmsBlockRepository.php b/Model/Repository/CmsBlockRepository.php index 8e94b5f0947ac3796a3d0f8c71cd8731f352a1f2..c1e8b99a4e2f09789b2a3e5893f7a33c53b1d82b 100644 --- a/Model/Repository/CmsBlockRepository.php +++ b/Model/Repository/CmsBlockRepository.php @@ -48,8 +48,11 @@ class CmsBlockRepository implements CmsBlockRepositoryInterface public function getByTitle(string $title): array { $connection = $this->resource->getConnection(); - $select = $connection->select()->from(self::CMS_BLOCK_TABLE_NAME)->where('title = :', $title); + $select = $connection->select() + ->from('cms_block') + ->where('title = :title'); - return $connection->fetchRow($select); + $bind = ['title' => $title]; + return $connection->fetchRow($select, $bind); } } diff --git a/ViewModel/ChangedBlockDataProvider.php b/ViewModel/ChangedBlockDataProvider.php index 697f50294a21fc2b6291a28d2c0f2653c7bf8f27..ca8864b09ce3f71b44d5722e66785ea32463be5d 100644 --- a/ViewModel/ChangedBlockDataProvider.php +++ b/ViewModel/ChangedBlockDataProvider.php @@ -19,10 +19,12 @@ declare(strict_types=1); namespace Firegento\ContentProvisioning\ViewModel; use Firegento\ContentProvisioning\Model\Query\GetDbBlockIds; -use Magento\Framework\View\Element\Block\ArgumentInterface; use Symfony\Component\Console\Helper\Table; +use Firegento\ContentProvisioning\Model\DTO\FileDataDto; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class ChangedBlockDataProvider implements ArgumentInterface +class ChangedBlockDataProvider { /** * @var GetDbBlockIds @@ -37,9 +39,9 @@ class ChangedBlockDataProvider implements ArgumentInterface /** * Delivery table data - * @param InputInterface $input - * @param OutputInterface $output - * @param array $entries + * @param FileDataDto[] $entries + * @param InputInterface + * @param OutputInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(InputInterface $input, OutputInterface $output, array $entries) @@ -48,18 +50,17 @@ class ChangedBlockDataProvider implements ArgumentInterface $table->setHeaders(['Key', 'Identifier', 'Stores', 'Maintained', 'Active', 'Title', 'in DB (IDs)']); foreach ($entries as $entry) { - $table->addRow( - [ - (string)$entry['key'], - (string)$entry['identifier'], - (string)(!empty($entry['stores'])) ? (is_array($entry['stores']) ? - implode(', ', $entry['stores']) : $entry['stores']) : "null", - (string)$entry['is_maintained'] ? 'yes' : 'no', - (string)$entry['is_active'] ? 'yes' : 'no', - (string)$entry['title'], - $this->getDbBlockIds->execute($entry['key']), - ] - ); + $rowData = [ + $entry->getKey(), + $entry->getIdentifier(), + $entry->getStores(), + $entry->getIsMaintained(), + $entry->getIsActive(), + $entry->getTitle(), + $this->getDbBlockIds->execute($entry->getKey()) + ]; + + $table->addRow($rowData); } $table->render($output);