diff --git a/Api/Log/RepositoryInterface.php b/Api/Log/RepositoryInterface.php
index 3876ffd..163c362 100755
--- a/Api/Log/RepositoryInterface.php
+++ b/Api/Log/RepositoryInterface.php
@@ -13,21 +13,54 @@
interface RepositoryInterface
{
+ /**
+ * Limit stream size to 100 lines
+ */
+ public const STREAM_DEFAULT_LIMIT = 100;
+
+ /**
+ * Log file path pattern
+ */
+ public const LOG_FILE = '%s/log/messagebird-%s.log';
+
/**
* Add record to error log
*
* @param string $type
* @param mixed $data
- *
*/
- public function addErrorLog(string $type, $data);
+ public function addErrorLog(string $type, $data): void;
/**
* Add record to debug log
*
* @param string $type
* @param mixed $data
+ */
+ public function addDebugLog(string $type, $data): void;
+
+ /**
+ * Add record to log
+ *
+ * @param string $type
+ * @param mixed $data
+ */
+ public function addLog(string $type, $data): void;
+
+ /**
+ * Returns path of logfile
+ *
+ * @param string $type
+ * @return string
+ */
+ public function getLogFilePath(string $type): ?string;
+
+ /**
+ * Return log entries as sorted array
*
+ * @param string $path
+ * @param int|null $limit
+ * @return array|null
*/
- public function addDebugLog(string $type, $data);
+ public function getLogEntriesAsArray(string $path, ?int $limit = null): ?array;
}
diff --git a/Block/Adminhtml/MessageBird/Header.php b/Block/Adminhtml/Design/Header.php
similarity index 91%
rename from Block/Adminhtml/MessageBird/Header.php
rename to Block/Adminhtml/Design/Header.php
index 996d457..31877ac 100755
--- a/Block/Adminhtml/MessageBird/Header.php
+++ b/Block/Adminhtml/Design/Header.php
@@ -5,7 +5,7 @@
*/
declare(strict_types=1);
-namespace Magmodules\MessageBird\Block\Adminhtml\MessageBird;
+namespace Magmodules\MessageBird\Block\Adminhtml\Design;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
diff --git a/Block/Adminhtml/MessageBird/Heading.php b/Block/Adminhtml/Design/Heading.php
similarity index 70%
rename from Block/Adminhtml/MessageBird/Heading.php
rename to Block/Adminhtml/Design/Heading.php
index 3cd9970..3f9d8b6 100755
--- a/Block/Adminhtml/MessageBird/Heading.php
+++ b/Block/Adminhtml/Design/Heading.php
@@ -5,7 +5,7 @@
*/
declare(strict_types=1);
-namespace Magmodules\MessageBird\Block\Adminhtml\MessageBird;
+namespace Magmodules\MessageBird\Block\Adminhtml\Design;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
@@ -26,8 +26,8 @@ public function render(AbstractElement $element): string
$html = '
';
$html .= ' ';
$html .= ' ';
- $html .= ' ' . $element->getData('label') . '
';
- $html .= ' ';
+ $html .= ' ' . $element->getData('label') . '
';
+ $html .= ' ';
$html .= ' ';
$html .= ' ';
$html .= ' ';
diff --git a/Block/Adminhtml/System/Config/Button/Credentials.php b/Block/Adminhtml/System/Config/Button/Credentials.php
index e93ad59..5dd2ec8 100755
--- a/Block/Adminhtml/System/Config/Button/Credentials.php
+++ b/Block/Adminhtml/System/Config/Button/Credentials.php
@@ -83,19 +83,19 @@ public function getApiCheckUrl()
}
/**
- * @return mixed
+ * @return string
*/
- public function getButtonHtml()
+ public function getButtonHtml(): string
{
- $buttonData = ['id' => 'messagebird-button_credentials', 'label' => __('Check Credentials')];
try {
- $button = $this->getLayout()->createBlock(
- Button::class
- )->setData($buttonData);
- return $button->toHtml();
+ return $this->getLayout()
+ ->createBlock(Button::class)
+ ->setData([
+ 'id' => 'mm-ui-button_credentials',
+ 'label' => __('Check Credentials')
+ ])->toHtml();
} catch (Exception $e) {
- $this->logger->addErrorLog('LocalizedException', $e->getMessage());
- return false;
+ return '';
}
}
}
diff --git a/Block/Adminhtml/System/Config/Button/DebugCheck.php b/Block/Adminhtml/System/Config/Button/DebugCheck.php
deleted file mode 100755
index 0d808f9..0000000
--- a/Block/Adminhtml/System/Config/Button/DebugCheck.php
+++ /dev/null
@@ -1,94 +0,0 @@
-request = $context->getRequest();
- parent::__construct($context, $data);
- }
-
- /**
- * @param AbstractElement $element
- *
- * @return string
- */
- public function render(AbstractElement $element)
- {
- $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
- return parent::render($element);
- }
-
- /**
- * @param AbstractElement $element
- *
- * @return string
- */
- public function _getElementHtml(AbstractElement $element)
- {
- return $this->_toHtml();
- }
-
- /**
- * @return string
- */
- public function getDebugCheckUrl()
- {
- return $this->getUrl('messagebird/log/debug');
- }
-
- /**
- * @return mixed
- */
- public function getButtonHtml()
- {
- $buttonData = [
- 'id' => 'magmodules-messagebird-button_debug',
- 'label' => __('Check last 100 debug log records')
- ];
- try {
- $button = $this->getLayout()->createBlock(
- Button::class
- )->setData($buttonData);
- return $button->toHtml();
- } catch (Exception $e) {
- return false;
- }
- }
-}
diff --git a/Block/Adminhtml/System/Config/Button/ErrorCheck.php b/Block/Adminhtml/System/Config/Button/ErrorCheck.php
deleted file mode 100755
index 95b2a62..0000000
--- a/Block/Adminhtml/System/Config/Button/ErrorCheck.php
+++ /dev/null
@@ -1,95 +0,0 @@
-request = $context->getRequest();
- parent::__construct($context, $data);
- }
-
- /**
- * @param AbstractElement $element
- *
- * @return string
- */
- public function render(AbstractElement $element)
- {
- $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
- return parent::render($element);
- }
-
- /**
- * @param AbstractElement $element
- *
- * @return string
- */
- public function _getElementHtml(AbstractElement $element)
- {
- return $this->_toHtml();
- }
-
- /**
- * @return string
- */
- public function getErrorCheckUrl()
- {
- return $this->getUrl('messagebird/log/error');
- }
-
- /**
- * @return mixed
- */
- public function getButtonHtml()
- {
- $buttonData = [
- 'id' => 'magmodules-messagebird-button_error',
- 'label' => __('Check last 100 error log records')
- ];
- try {
- $button = $this->getLayout()->createBlock(
- Button::class
- )->setData($buttonData);
- return $button->toHtml();
- } catch (Exception $e) {
- return false;
- }
- }
-}
diff --git a/Block/Adminhtml/System/Config/Button/Log.php b/Block/Adminhtml/System/Config/Button/Log.php
new file mode 100644
index 0000000..34f055f
--- /dev/null
+++ b/Block/Adminhtml/System/Config/Button/Log.php
@@ -0,0 +1,74 @@
+unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
+ return parent::render($element);
+ }
+
+ /**
+ * @param AbstractElement $element
+ *
+ * @return string
+ */
+ public function _getElementHtml(AbstractElement $element): string
+ {
+ return $this->_toHtml();
+ }
+
+ /**
+ * @param string $type
+ * @return string
+ */
+ public function getDownloadUrl(string $type): string
+ {
+ return $this->getUrl('messagebird/log/stream', ['type' => $type]);
+ }
+
+ /**
+ * @param string $type
+ * @return string
+ */
+ public function getButtonHtml(string $type): string
+ {
+ try {
+ return $this->getLayout()
+ ->createBlock(Button::class)
+ ->setData([
+ 'id' => 'mm-ui-button_' . $type,
+ 'label' => __('Show last %1 %2 log records', LogRepository::STREAM_DEFAULT_LIMIT, $type)
+ ])->toHtml();
+ } catch (Exception $e) {
+ return '';
+ }
+ }
+}
diff --git a/Block/Adminhtml/System/Config/Button/VersionCheck.php b/Block/Adminhtml/System/Config/Button/VersionCheck.php
index b5fa02b..b7f6896 100755
--- a/Block/Adminhtml/System/Config/Button/VersionCheck.php
+++ b/Block/Adminhtml/System/Config/Button/VersionCheck.php
@@ -53,7 +53,7 @@ public function __construct(
/**
* @return string
*/
- public function getVersion()
+ public function getVersion(): string
{
return $this->configRepository->getExtensionVersion();
}
@@ -63,7 +63,7 @@ public function getVersion()
*
* @return string
*/
- public function render(AbstractElement $element)
+ public function render(AbstractElement $element): string
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
@@ -74,7 +74,7 @@ public function render(AbstractElement $element)
*
* @return string
*/
- public function _getElementHtml(AbstractElement $element)
+ public function _getElementHtml(AbstractElement $element): string
{
return $this->_toHtml();
}
@@ -82,32 +82,33 @@ public function _getElementHtml(AbstractElement $element)
/**
* @return string
*/
- public function getVersionCheckUrl()
+ public function getVersionCheckUrl(): string
{
- return $this->getUrl('messagebird/versioncheck/index');
+ return $this->getUrl('messagebird/versionCheck/index');
}
/**
* @return string
*/
- public function getChangeLogUrl()
+ public function getChangeLogUrl(): string
{
- return $this->getUrl('messagebird/versioncheck/changelog');
+ return $this->getUrl('messagebird/versionCheck/changelog');
}
/**
- * @return mixed
+ * @return string
*/
- public function getButtonHtml()
+ public function getButtonHtml(): string
{
- $buttonData = ['id' => 'magmodules-messagebird-button_version', 'label' => __('Check for latest versions')];
try {
- $button = $this->getLayout()->createBlock(
- Button::class
- )->setData($buttonData);
- return $button->toHtml();
+ return $this->getLayout()
+ ->createBlock(Button::class)
+ ->setData([
+ 'id' => 'mm-ui-button_version',
+ 'label' => __('Check for latest versions')
+ ])->toHtml();
} catch (Exception $e) {
- return false;
+ return '';
}
}
}
diff --git a/Block/ProductAlert/Price.php b/Block/ProductAlert/Price.php
index 30a5f1d..7d699a9 100755
--- a/Block/ProductAlert/Price.php
+++ b/Block/ProductAlert/Price.php
@@ -91,9 +91,10 @@ public function setTemplate($template)
$template = '';
} elseif (!$this->session->isLoggedIn() || !$this->configRepository->isEnabledForPriceChanges($storeId)) {
$template = 'Magento_ProductAlert::product/view.phtml';
- $this->setSignupUrl($this->_helper->getSaveUrl('price'));
+ $this->setData('signup_url', $this->_helper->getSaveUrl('price'));
} else {
- $this->setSignupUrl(
+ $this->setData(
+ 'signup_url',
$this->getUrl(
'messagebird/addalert/price',
[
diff --git a/Block/ProductAlert/Stock.php b/Block/ProductAlert/Stock.php
index de842ff..bd0b32a 100755
--- a/Block/ProductAlert/Stock.php
+++ b/Block/ProductAlert/Stock.php
@@ -88,9 +88,10 @@ public function setTemplate($template)
$template = '';
} elseif (!$this->session->isLoggedIn() || !$this->configRepository->isEnabledForOutOfStock($storeId)) {
$template = 'Magento_ProductAlert::product/view.phtml';
- $this->setSignupUrl($this->_helper->getSaveUrl('stock'));
+ $this->setData('signup_url', $this->_helper->getSaveUrl('stock'));
} else {
- $this->setSignupUrl(
+ $this->setData(
+ 'signup_url',
$this->getUrl(
'messagebird/addalert/stock',
[
diff --git a/Controller/Adminhtml/Log/Debug.php b/Controller/Adminhtml/Log/Debug.php
deleted file mode 100755
index 48b018a..0000000
--- a/Controller/Adminhtml/Log/Debug.php
+++ /dev/null
@@ -1,138 +0,0 @@
-resultJsonFactory = $resultJsonFactory;
- $this->dir = $dir;
- $this->file = $file;
- $this->serializerJson = $serializerJson;
- parent::__construct($context);
- }
-
- /**
- * @return ResponseInterface|Json|ResultInterface
- * @throws FileSystemException
- */
- public function execute()
- {
- $resultJson = $this->resultJsonFactory->create();
- if ($this->isLogExists(self::DEBUG_LOG_FILE)) {
- $result = ['result' => $this->prepareLogText(self::DEBUG_LOG_FILE)];
- } else {
- $result = __('Log is empty');
- }
- return $resultJson->setData($result);
- }
-
- /**
- * Check is log file exists
- *
- * @param string $file
- *
- * @return bool
- */
- private function isLogExists(string $file): bool
- {
- try {
- $logFile = sprintf($file, $this->dir->getPath('var'));
- return $this->file->isExists($logFile);
- } catch (Exception $e) {
- return false;
- }
- }
-
- /**
- * Prepare encoded log text
- *
- * @param string $file
- *
- * @return array
- * @throws FileSystemException
- */
- private function prepareLogText(string $file): array
- {
- $logFile = sprintf($file, $this->dir->getPath('var'));
- $fileContent = explode(PHP_EOL, $this->file->fileGetContents($logFile));
- if (count($fileContent) > 100) {
- $fileContent = array_slice($fileContent, -100, 100, true);
- }
- $result = [];
- foreach ($fileContent as $line) {
- $data = explode('] ', $line);
- $date = ltrim(array_shift($data), '[');
- $data = implode('] ', $data);
- $data = explode(': ', $data);
- array_shift($data);
- $result[] = [
- 'date' => $date,
- 'msg' => implode(': ', $data)
- ];
- }
- return $result;
- }
-}
diff --git a/Controller/Adminhtml/Log/Error.php b/Controller/Adminhtml/Log/Error.php
deleted file mode 100755
index 2867f97..0000000
--- a/Controller/Adminhtml/Log/Error.php
+++ /dev/null
@@ -1,134 +0,0 @@
-resultJsonFactory = $resultJsonFactory;
- $this->dir = $dir;
- $this->file = $file;
- $this->serializerJson = $serializerJson;
- parent::__construct($context);
- }
-
- /**
- * @return ResponseInterface|Json|ResultInterface
- * @throws FileSystemException
- */
- public function execute()
- {
- $resultJson = $this->resultJsonFactory->create();
- if ($this->isLogExists(self::ERROR_LOG_FILE)) {
- $result = ['result' => $this->prepareLogText(self::ERROR_LOG_FILE)];
- } else {
- $result = __('Log is empty');
- }
- return $resultJson->setData($result);
- }
-
- /**
- * Check is log file exists
- *
- * @param string $file
- *
- * @return bool
- */
- private function isLogExists(string $file): bool
- {
- try {
- $logFile = sprintf($file, $this->dir->getPath('var'));
- return $this->file->isExists($logFile);
- } catch (Exception $e) {
- return false;
- }
- }
-
- /**
- * Prepare encoded log text
- *
- * @param string $file
- *
- * @return array
- * @throws FileSystemException
- */
- private function prepareLogText(string $file): array
- {
- $logFile = sprintf($file, $this->dir->getPath('var'));
- $fileContent = explode(PHP_EOL, $this->file->fileGetContents($logFile));
- if (count($fileContent) > 100) {
- $fileContent = array_slice($fileContent, -100, 100, true);
- }
- $result = [];
- foreach ($fileContent as $line) {
- $data = explode('] ', $line);
- $date = ltrim(array_shift($data), '[');
- $data = implode('] ', $data);
- $data = explode(': ', $data);
- array_shift($data);
- $result[] = [
- 'date' => $date,
- 'msg' => implode(': ', $data)
- ];
- }
- return $result;
- }
-}
diff --git a/Controller/Adminhtml/Log/Stream.php b/Controller/Adminhtml/Log/Stream.php
new file mode 100644
index 0000000..364bb8e
--- /dev/null
+++ b/Controller/Adminhtml/Log/Stream.php
@@ -0,0 +1,74 @@
+resultJsonFactory = $resultJsonFactory;
+ $this->request = $context->getRequest();
+ $this->logRepository = $logRepository;
+ parent::__construct($context);
+ }
+
+ /**
+ * @return Json
+ */
+ public function execute(): Json
+ {
+ $resultJson = $this->resultJsonFactory->create();
+ $type = $this->request->getParam('type') == 'error' ? 'error' : 'debug';
+ $logFilePath = $this->logRepository->getLogFilePath($type);
+
+ if ($logFilePath && $result = $this->logRepository->getLogEntriesAsArray($logFilePath)) {
+ $result = ['result' => $result];
+ } else {
+ $result = __('Log is empty');
+ }
+
+ return $resultJson->setData($result);
+ }
+}
diff --git a/Controller/Adminhtml/VersionCheck/Changelog.php b/Controller/Adminhtml/VersionCheck/Changelog.php
index c53e0a7..c2b9438 100755
--- a/Controller/Adminhtml/VersionCheck/Changelog.php
+++ b/Controller/Adminhtml/VersionCheck/Changelog.php
@@ -8,10 +8,8 @@
namespace Magmodules\MessageBird\Controller\Adminhtml\VersionCheck;
use Magento\Backend\App\Action;
-use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
-use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
@@ -30,11 +28,6 @@ class Changelog extends Action
*/
private $resultJsonFactory;
- /**
- * @var ConfigRepository
- */
- private $configRepository;
-
/**
* @var JsonSerializer
*/
@@ -50,41 +43,31 @@ class Changelog extends Action
*
* @param Action\Context $context
* @param JsonFactory $resultJsonFactory
- * @param ConfigRepository $configRepository
* @param JsonSerializer $json
* @param File $file
*/
public function __construct(
Action\Context $context,
JsonFactory $resultJsonFactory,
- ConfigRepository $configRepository,
JsonSerializer $json,
File $file
) {
$this->resultJsonFactory = $resultJsonFactory;
- $this->configRepository = $configRepository;
$this->json = $json;
$this->file = $file;
parent::__construct($context);
}
/**
- * @return ResponseInterface|Json|ResultInterface
+ * @return Json
* @throws FileSystemException
*/
- public function execute()
+ public function execute(): Json
{
$resultJson = $this->resultJsonFactory->create();
$result = $this->getVersions();
- $current = $latest = preg_replace('/^v/', '', $this->configRepository->getExtensionVersion());
$data = $this->json->unserialize($result);
- $logs = [];
- foreach ($data as $version => $log) {
- if (version_compare((string)$current, (string)$version) == -1) {
- $logs[$version] = $log;
- }
- }
- return $resultJson->setData($logs);
+ return $resultJson->setData($data);
}
/**
@@ -94,7 +77,7 @@ public function execute()
private function getVersions(): string
{
return $this->file->fileGetContents(
- sprintf('http://version.magmodules.eu/%s.json', ConfigRepository::EXTENSION_CODE)
+ sprintf('https://version.magmodules.eu/%s.json', ConfigRepository::EXTENSION_CODE)
);
}
}
diff --git a/Controller/Adminhtml/VersionCheck/Index.php b/Controller/Adminhtml/VersionCheck/Index.php
index 27153be..e23ba93 100755
--- a/Controller/Adminhtml/VersionCheck/Index.php
+++ b/Controller/Adminhtml/VersionCheck/Index.php
@@ -12,7 +12,6 @@
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
-use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magmodules\MessageBird\Api\Config\RepositoryInterface as ConfigRepository;
@@ -69,21 +68,31 @@ public function __construct(
}
/**
- * @return ResponseInterface|Json|ResultInterface
+ * @return Json
*/
- public function execute()
+ public function execute(): Json
{
$resultJson = $this->resultJsonFactory->create();
$result = $this->getVersions();
$current = $latest = preg_replace('/^v/', '', $this->configRepository->getExtensionVersion());
+ $changeLog = [];
if ($result) {
$data = $this->json->unserialize($result);
$versions = array_keys($data);
$latest = preg_replace('/^v/', '', reset($versions));
+ foreach ($data as $version => $changes) {
+ if (version_compare(preg_replace('/^v/', '', $version), $current) == 0) {
+ break;
+ }
+ $changeLog[] = [
+ $version => $changes['changelog']
+ ];
+ }
}
$data = [
- 'current_verion' => 'v' . $current,
- 'last_version' => 'v' . $latest
+ 'current_version' => 'v' . $current,
+ 'last_version' => 'v' . $latest,
+ 'changelog' => $changeLog,
];
return $resultJson->setData(['result' => $data]);
}
@@ -95,7 +104,7 @@ private function getVersions(): string
{
try {
return $this->file->fileGetContents(
- sprintf('http://version.magmodules.eu/%s.json', ConfigRepository::EXTENSION_CODE)
+ sprintf('https://version.magmodules.eu/%s.json', ConfigRepository::EXTENSION_CODE)
);
} catch (\Exception $exception) {
return '';
diff --git a/Logger/DebugLogger.php b/Logger/DebugLogger.php
index f8f220f..38c00c2 100755
--- a/Logger/DebugLogger.php
+++ b/Logger/DebugLogger.php
@@ -46,7 +46,7 @@ public function __construct(
* @param mixed $data
*
*/
- public function addLog(string $type, $data)
+ public function addLog(string $type, $data): void
{
if (is_array($data) || is_object($data)) {
$this->addRecord(static::INFO, $type . ': ' . $this->json->serialize($data));
diff --git a/Logger/ErrorLogger.php b/Logger/ErrorLogger.php
index d86abb5..770fbbe 100755
--- a/Logger/ErrorLogger.php
+++ b/Logger/ErrorLogger.php
@@ -46,7 +46,7 @@ public function __construct(
* @param mixed $data
*
*/
- public function addLog($type, $data)
+ public function addLog($type, $data): void
{
if (is_array($data) || is_object($data)) {
$this->addRecord(static::ERROR, $type . ': ' . $this->json->serialize($data));
diff --git a/Logger/Handler/Debug.php b/Logger/Handler/Debug.php
index cb3f634..3036557 100755
--- a/Logger/Handler/Debug.php
+++ b/Logger/Handler/Debug.php
@@ -24,5 +24,5 @@ class Debug extends Base
/**
* @var string
*/
- protected $fileName = '/var/log/messagebird/debug.log';
+ protected $fileName = '/var/log/messagebird-debug.log';
}
diff --git a/Logger/Handler/Error.php b/Logger/Handler/Error.php
index d764cfd..bce4e52 100755
--- a/Logger/Handler/Error.php
+++ b/Logger/Handler/Error.php
@@ -24,5 +24,5 @@ class Error extends Base
/**
* @var string
*/
- protected $fileName = '/var/log/messagebird/error.log';
+ protected $fileName = '/var/log/messagebird-error.log';
}
diff --git a/Model/Config/Repository.php b/Model/Config/Repository.php
index 958f126..7b62a0f 100755
--- a/Model/Config/Repository.php
+++ b/Model/Config/Repository.php
@@ -14,7 +14,6 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use Magmodules\MessageBird\Api\Config\RepositoryInterface;
-use Magmodules\MessageBird\Api\Log\RepositoryInterface as LogRepository;
/**
* MessageBird Connect config repository class
@@ -32,11 +31,6 @@ class Repository implements RepositoryInterface
*/
private $storeManager;
- /**
- * @var LogRepository
- */
- private $logRepository;
-
/**
* @var ProductMetadataInterface
*/
@@ -52,19 +46,16 @@ class Repository implements RepositoryInterface
*
* @param ScopeConfig $scopeConfig
* @param StoreManager $storeManager
- * @param LogRepository $logRepository
* @param ProductMetadataInterface $metadata
*/
public function __construct(
ScopeConfig $scopeConfig,
StoreManager $storeManager,
- LogRepository $logRepository,
ProductMetadataInterface $metadata,
EncryptorInterface $encryptor
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
- $this->logRepository = $logRepository;
$this->metadata = $metadata;
$this->encryptor = $encryptor;
}
@@ -102,7 +93,6 @@ public function getStore(): StoreInterface
try {
return $this->storeManager->getStore();
} catch (Exception $e) {
- $this->logRepository->addErrorLog('Store error', $e->getMessage());
if ($store = $this->storeManager->getDefaultStoreView()) {
return $store;
}
diff --git a/Model/Log/Repository.php b/Model/Log/Repository.php
index 04d3312..309a21d 100755
--- a/Model/Log/Repository.php
+++ b/Model/Log/Repository.php
@@ -7,10 +7,14 @@
namespace Magmodules\MessageBird\Model\Log;
+use Magento\Framework\Filesystem\DirectoryList;
+use Magento\Framework\Filesystem\Driver\File;
+use Magento\Framework\Stdlib\DateTime\DateTime;
+use Magmodules\MessageBird\Api\Log\RepositoryInterface as LogRepository;
+use Magmodules\MessageBird\Api\Log\RepositoryInterface as LogRepositoryInterface;
use Magmodules\MessageBird\Logger\DebugLogger;
use Magmodules\MessageBird\Logger\ErrorLogger;
-use Magmodules\MessageBird\Api\Log\RepositoryInterface
- as LogRepositoryInterface;
+use Magmodules\MessageBird\Api\Config\RepositoryInterface as Config;
/**
* Logs repository class
@@ -22,30 +26,57 @@ class Repository implements LogRepositoryInterface
* @var DebugLogger
*/
private $debugLogger;
-
/**
* @var ErrorLogger
*/
private $errorLogger;
+ /**
+ * @var DirectoryList
+ */
+ private $dir;
+ /**
+ * @var DateTime
+ */
+ private $dateTime;
+ /**
+ * @var File
+ */
+ private $file;
+ /**
+ * @var Config
+ */
+ private $config;
/**
* Repository constructor.
*
* @param DebugLogger $debugLogger
* @param ErrorLogger $errorLogger
+ * @param DirectoryList $dir
+ * @param File $file
+ * @param DateTime $dateTime
+ * @param Config $config
*/
public function __construct(
DebugLogger $debugLogger,
- ErrorLogger $errorLogger
+ ErrorLogger $errorLogger,
+ DirectoryList $dir,
+ File $file,
+ DateTime $dateTime,
+ Config $config
) {
$this->debugLogger = $debugLogger;
$this->errorLogger = $errorLogger;
+ $this->dir = $dir;
+ $this->file = $file;
+ $this->dateTime = $dateTime;
+ $this->config = $config;
}
/**
* @inheritDoc
*/
- public function addErrorLog(string $type, $data)
+ public function addErrorLog(string $type, $data): void
{
$this->errorLogger->addLog($type, $data);
}
@@ -53,8 +84,83 @@ public function addErrorLog(string $type, $data)
/**
* @inheritDoc
*/
- public function addDebugLog(string $type, $data)
+ public function addDebugLog(string $type, $data): void
{
$this->debugLogger->addLog($type, $data);
}
+
+ /**
+ * @inheritDoc
+ */
+ public function addLog(string $type, $data): void
+ {
+ if ($this->config->isDebugMode()) {
+ $this->addDebugLog($type, $data);
+ } else {
+ $this->addErrorLog($type, $data);
+ }
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getLogFilePath(string $type): ?string
+ {
+ try {
+ return sprintf(LogRepositoryInterface::LOG_FILE, $this->dir->getPath('var'), $type);
+ } catch (\Exception $exception) {
+ return null;
+ }
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getLogEntriesAsArray(string $path, ?int $limit = null): ?array
+ {
+ try {
+ if (!$this->file->isExists($path)) {
+ return null;
+ }
+
+ $stream = $this->file->fileOpen($path, 'r');
+ $this->file->fileSeek($stream, 0, SEEK_END);
+ $pos = $this->file->fileTell($stream);
+ $numberOfLines = LogRepository::STREAM_DEFAULT_LIMIT;
+ while ($pos >= 0 && $numberOfLines > 0) {
+ $this->file->fileSeek($stream, $pos);
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
+ $char = fgetc($stream);
+ if ($char === "\n") {
+ $numberOfLines--;
+ }
+ $pos--;
+ }
+
+ $result = [];
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
+ while (!feof($stream) && $numberOfLines < LogRepository::STREAM_DEFAULT_LIMIT) {
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
+ if ($line = fgets($stream)) {
+ $data = explode('] ', $line);
+ $date = ltrim(array_shift($data), '[');
+ $data = implode('] ', $data);
+ $data = explode(': ', $data);
+ unset($data[0]);
+ $type = $data[1] ?? '--';
+ array_shift($data);
+
+ $result[] = [
+ 'date' => $this->dateTime->date('Y-m-d H:i:s', $date) . ' - ' . $type,
+ 'msg' => implode(': ', $data)
+ ];
+ }
+ }
+
+ $this->file->fileClose($stream);
+ return array_reverse($result);
+ } catch (\Exception $exception) {
+ return null;
+ }
+ }
}
diff --git a/composer.json b/composer.json
index 797e9f8..6394b51 100755
--- a/composer.json
+++ b/composer.json
@@ -2,11 +2,11 @@
"name": "magmodules/magento2-messagebird",
"description": "MessageBird integration for Magento 2",
"type": "magento2-module",
- "version": "1.0.1",
+ "version": "1.1.0",
"require": {
"magento/framework": ">=100.1.0",
- "messagebird/php-rest-api": "^1.20",
- "giggsey/libphonenumber-for-php": "^8.12"
+ "messagebird/php-rest-api": ">=1.20",
+ "giggsey/libphonenumber-for-php": ">=8.12"
},
"license": [
"OSL-3.0",
diff --git a/etc/adminhtml/system/branding.xml b/etc/adminhtml/system/branding.xml
index 6566eb0..0c8ce0b 100755
--- a/etc/adminhtml/system/branding.xml
+++ b/etc/adminhtml/system/branding.xml
@@ -9,6 +9,6 @@
Branding
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Header
+ Magmodules\MessageBird\Block\Adminhtml\Design\Header
diff --git a/etc/adminhtml/system/communication.xml b/etc/adminhtml/system/communication.xml
index 5b7bce0..a603f21 100755
--- a/etc/adminhtml/system/communication.xml
+++ b/etc/adminhtml/system/communication.xml
@@ -15,7 +15,7 @@
Communication Triggers
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Heading
+ Magmodules\MessageBird\Block\Adminhtml\Design\Heading
@@ -126,7 +126,7 @@
Payment Reminder Messages
In collaboration with Mollie payments, we have made it possible to notify customers about unfinished payments using an e-mail. With the MessageBird integration, we can share this payment link directly with them through a text message so the customers can directly revive their order.
Please note that; - This feature is only available when the Mollie Payments plugin is installed and enabled; - The message is only sent if the customer didn't finish their order in a certain time period.]]>
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Heading
+ Magmodules\MessageBird\Block\Adminhtml\Design\Heading
Logging
Errors are always logged and written into the error.log file.]]>
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Heading
+ Magmodules\MessageBird\Block\Adminhtml\Design\Heading
@@ -23,15 +23,9 @@
Magento\Config\Model\Config\Source\Yesno
magmodules_messagebird/debug/debug
-
+
- Magmodules\MessageBird\Block\Adminhtml\System\Config\Button\DebugCheck
-
-
-
- Magmodules\MessageBird\Block\Adminhtml\System\Config\Button\ErrorCheck
+ Magmodules\MessageBird\Block\Adminhtml\System\Config\Button\Log
diff --git a/etc/adminhtml/system/general.xml b/etc/adminhtml/system/general.xml
index 290cf97..c19896f 100755
--- a/etc/adminhtml/system/general.xml
+++ b/etc/adminhtml/system/general.xml
@@ -26,7 +26,7 @@
Credentials
this link. Please note that
sending messages with Test Mode disabled uses your available credits.]]>
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Heading
+ Magmodules\MessageBird\Block\Adminhtml\Design\Heading
diff --git a/etc/adminhtml/system/restrictions.xml b/etc/adminhtml/system/restrictions.xml
index a6a8dd3..f7590ef 100755
--- a/etc/adminhtml/system/restrictions.xml
+++ b/etc/adminhtml/system/restrictions.xml
@@ -15,7 +15,7 @@
Triggerpoints
- Magmodules\MessageBird\Block\Adminhtml\MessageBird\Heading
+ Magmodules\MessageBird\Block\Adminhtml\Design\Heading
diff --git a/etc/config.xml b/etc/config.xml
index 6bbf957..a2e5c28 100755
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -8,7 +8,7 @@
- v1.0.1
+ v1.1.0
We successfully received your order with ID #{{order_id}} and awaiting your payment.
diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js
deleted file mode 100755
index a9ab93d..0000000
--- a/view/adminhtml/requirejs-config.js
+++ /dev/null
@@ -1,6 +0,0 @@
-var config = {
- deps: [
- 'Magmodules_MessageBird/js/button-functions',
- 'Magmodules_MessageBird/js/show-more',
- ]
-};
diff --git a/view/adminhtml/templates/system/config/button/credentials.phtml b/view/adminhtml/templates/system/config/button/credentials.phtml
index 597580b..639fdcd 100755
--- a/view/adminhtml/templates/system/config/button/credentials.phtml
+++ b/view/adminhtml/templates/system/config/button/credentials.phtml
@@ -11,38 +11,46 @@ use Magmodules\MessageBird\Block\Adminhtml\System\Config\Button\Credentials;
* @var Credentials $block
*/
?>
+
+
+
+ = $block->getButtonHtml() ?>
+
+ = $block->escapeHtml(__('Connecting...')); ?>
+
+
+
+
+
+
-= $block->getButtonHtml() ?>
-
- Connecting...
-
-
diff --git a/view/adminhtml/templates/system/config/button/debug.phtml b/view/adminhtml/templates/system/config/button/debug.phtml
deleted file mode 100755
index 837e537..0000000
--- a/view/adminhtml/templates/system/config/button/debug.phtml
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-= $block->getButtonHtml() ?>
-
diff --git a/view/adminhtml/templates/system/config/button/error.phtml b/view/adminhtml/templates/system/config/button/error.phtml
deleted file mode 100755
index 9e1f367..0000000
--- a/view/adminhtml/templates/system/config/button/error.phtml
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
-= $block->getButtonHtml() ?>
diff --git a/view/adminhtml/templates/system/config/button/log.phtml b/view/adminhtml/templates/system/config/button/log.phtml
new file mode 100644
index 0000000..253f3c4
--- /dev/null
+++ b/view/adminhtml/templates/system/config/button/log.phtml
@@ -0,0 +1,42 @@
+
+
+