Skip to content
Snippets Groups Projects
Commit cd1a6dff authored by Eduarda Lentz Rodrigues da Silva's avatar Eduarda Lentz Rodrigues da Silva
Browse files

ZERO-162 Updated classes with DTO and logger

parent 5380432b
No related branches found
No related tags found
1 merge request!46ZERO-162-changes
Pipeline #92505 failed
......@@ -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
......
......@@ -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) {
......
......@@ -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());
}
}
}
......@@ -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;
}
......
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment