Skip to content

Commit 8aa0274

Browse files
cammonromrahman3177
authored andcommitted
MAGE-866: commit cherry-picked from 864
1 parent 5ad5942 commit 8aa0274

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

Helper/AnalyticsHelper.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@
66
use Algolia\AlgoliaSearch\Config\AnalyticsConfig;
77
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
88
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
9+
use Magento\Framework\Locale\ResolverInterface;
910

1011
class AnalyticsHelper
1112
{
1213
public const ANALYTICS_SEARCH_PATH = '/2/searches';
1314
public const ANALYTICS_HITS_PATH = '/2/hits';
1415
public const ANALYTICS_FILTER_PATH = '/2/filters';
1516
public const ANALYTICS_CLICKS_PATH = '/2/clicks';
16-
17-
/** @var AlgoliaHelper */
18-
private $algoliaHelper;
19-
20-
/** @var ConfigHelper */
21-
private $configHelper;
22-
23-
/** @var IndexEntityDataProvider */
24-
private $entityHelper;
25-
26-
/** @var Logger */
27-
private $logger;
17+
public const DATE_FORMAT_PICKER = 'dd MMM yyyy';
18+
public const DATE_FORMAT_API = 'Y-m-d';
2819

2920
private $searches;
3021
private $users;
@@ -58,25 +49,17 @@ class AnalyticsHelper
5849
protected $region;
5950

6051
/**
61-
* @param AlgoliaHelper $algoliaHelper
6252
* @param ConfigHelper $configHelper
6353
* @param IndexEntityDataProvider $entityHelper
6454
* @param Logger $logger
65-
* @param string $region
55+
* @param ResolverInterface $localeResolver
6656
*/
6757
public function __construct(
68-
AlgoliaHelper $algoliaHelper,
69-
ConfigHelper $configHelper,
70-
IndexEntityDataProvider $entityHelper,
71-
Logger $logger,
72-
string $region = 'us'
58+
private ConfigHelper $configHelper,
59+
private IndexEntityDataProvider $entityHelper,
60+
private Logger $logger,
61+
private ResolverInterface $localeResolver
7362
) {
74-
$this->algoliaHelper = $algoliaHelper;
75-
$this->configHelper = $configHelper;
76-
77-
$this->entityHelper = $entityHelper;
78-
79-
$this->logger = $logger;
8063
$this->region = $this->configHelper->getAnalyticsRegion();
8164
}
8265

@@ -371,4 +354,17 @@ public function getErrors()
371354
{
372355
return $this->errors;
373356
}
357+
358+
/**
359+
* @param string $timezone
360+
* @return \IntlDateFormatter
361+
*/
362+
public function getAnalyticsDatePickerFormatter(string $timezone): \IntlDateFormatter
363+
{
364+
$locale = $this->localeResolver->getLocale();
365+
$dateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, $timezone);
366+
$dateFormatter->setPattern(self::DATE_FORMAT_PICKER);
367+
return $dateFormatter;
368+
}
369+
374370
}

ViewModel/Adminhtml/Analytics/Overview.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
3030
private $analyticsParams = [];
3131

3232
/**
33-
* Index constructor.
34-
*
3533
* @param BackendView $backendView
3634
* @param AnalyticsHelper $analyticsHelper
3735
* @param IndexEntityDataProvider $indexEntityDataProvider
3836
*/
3937
public function __construct(
40-
BackendView $backendView,
41-
AnalyticsHelper $analyticsHelper,
42-
IndexEntityDataProvider $indexEntityDataProvider
43-
) {
44-
$this->backendView = $backendView;
45-
$this->analyticsHelper = $analyticsHelper;
46-
$this->indexEntityDataProvider = $indexEntityDataProvider;
47-
}
38+
protected BackendView $backendView,
39+
protected AnalyticsHelper $analyticsHelper,
40+
protected IndexEntityDataProvider $indexEntityDataProvider,
41+
protected ResolverInterface $localeResolver
42+
) { }
4843

4944
/**
5045
* @return BackendView
@@ -86,13 +81,11 @@ public function getAnalyticsParams($additional = [])
8681
if (empty($this->analyticsParams)) {
8782
$params = ['index' => $this->getIndexName()];
8883
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
89-
$dateTime = $this->getBackendView()->getDateTime();
90-
$timeZone = $this->getTimeZone();
91-
if (isset($formData['from']) && $formData['from'] !== '') {
92-
$params['startDate'] = $dateTime->date($formData['from'], $timeZone, true, false)->format('Y-m-d');
84+
if (!empty($formData['from'])) {
85+
$params['startDate'] = $this->formatFormSubmittedDate($formData['from']);
9386
}
94-
if (isset($formData['to']) && $formData['to'] !== '') {
95-
$params['endDate'] = $dateTime->date($formData['to'], $timeZone, true, false)->format('Y-m-d');
87+
if (!empty($formData['to'])) {
88+
$params['endDate'] = $this->formatFormSubmittedDate($formData['to']);
9689
}
9790
}
9891

@@ -102,6 +95,39 @@ public function getAnalyticsParams($additional = [])
10295
return array_merge($this->analyticsParams, $additional);
10396
}
10497

98+
/**
99+
* @param string $dateString
100+
* @return string
101+
* @throws NoSuchEntityException
102+
*/
103+
protected function formatFormSubmittedDate(string $dateString): string
104+
{
105+
$timezone = $this->getTimeZone();
106+
$dateTime = $this->parseFormSubmittedDate($dateString, $timezone);
107+
return $dateTime->format(AnalyticsHelper::DATE_FORMAT_API);
108+
}
109+
110+
/**
111+
* @param string|null $dateString
112+
* @param string|null $timezone
113+
* @return \DateTime
114+
* @throws NoSuchEntityException
115+
*/
116+
protected function parseFormSubmittedDate(string $dateString = null, string $timezone = null): \DateTime
117+
{
118+
if (empty($timezone)) {
119+
$timezone = $this->getTimeZone();
120+
}
121+
122+
if (empty($dateString)) {
123+
return new \DateTime('now', new \DateTimeZone($timezone));
124+
}
125+
126+
$dateFormatter = $this->analyticsHelper->getAnalyticsDatePickerFormatter($timezone);
127+
$parsedDate = $dateFormatter->parse($dateString);
128+
return (new \DateTime('now', new \DateTimeZone($timezone)))->setTimestamp($parsedDate);
129+
}
130+
105131
public function getTotalCountOfSearches()
106132
{
107133
return $this->analyticsHelper->getTotalCountOfSearches($this->getAnalyticsParams());
@@ -309,11 +335,12 @@ public function getNoResultSearches()
309335
public function checkIsValidDateRange()
310336
{
311337
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
312-
if (isset($formData['from']) && !empty($formData['from'])) {
313-
$dateTime = $this->getBackendView()->getDateTime();
314-
$timeZone = $this->getTimeZone();
315-
$startDate = $dateTime->date($formData['from'], $timeZone, true, false);
316-
$diff = date_diff($startDate, $dateTime->date(null, $timeZone, true, null));
338+
if (!empty($formData['from'])) {
339+
$timezone = $this->getTimeZone();
340+
341+
$startDate = $this->parseFormSubmittedDate($formData['from'], $timezone);
342+
$now = $this->parseFormSubmittedDate(null, $timezone);
343+
$diff = date_diff($startDate, $now);
317344

318345
if ($diff->days > $this->getAnalyticRetentionDays()) {
319346
return false;

0 commit comments

Comments
 (0)