From cd1a6dffd2bac8b7645033ace01d28d09ef76938 Mon Sep 17 00:00:00 2001
From: Eduarda Lentz Rodrigues da Silva
 <e.lentzrodriguesdasilva@techdivision.com>
Date: Wed, 22 Jan 2025 16:52:36 +0100
Subject: [PATCH] ZERO-162 Updated classes with DTO and logger

---
 Cron/InconsistenciesInBlockEmail.php       | 48 ++++++++++++++++------
 Model/Query/GetChangedBlockIdentifiers.php | 20 +++++----
 Model/Query/GetDbBlockIds.php              | 13 ++++--
 Model/Query/GetFilesContentInformation.php | 32 +++++++++------
 Model/RenderChangedBlockList.php           | 15 -------
 Model/Repository/CmsBlockRepository.php    |  7 +++-
 ViewModel/ChangedBlockDataProvider.php     | 35 ++++++++--------
 7 files changed, 100 insertions(+), 70 deletions(-)

diff --git a/Cron/InconsistenciesInBlockEmail.php b/Cron/InconsistenciesInBlockEmail.php
index ce51355..0314251 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 15d0166..da8e463 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 af4e5ee..99854a8 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 acd815b..c29955b 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 e797dda..f36b9fa 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 8e94b5f..c1e8b99 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 697f502..ca8864b 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);
-- 
GitLab