Skip to content

Commit 5da031e

Browse files
committed
Remove unnecessary chart.js files, move retrieving request params to controllers, apply review requirements
1 parent d00b206 commit 5da031e

File tree

22 files changed

+246
-37204
lines changed

22 files changed

+246
-37204
lines changed

app/code/Magento/Backend/Block/Dashboard/Totals.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Backend\Block\Dashboard;
99

1010
use Magento\Backend\Block\Template\Context;
11+
use Magento\Backend\Model\Dashboard\Period;
1112
use Magento\Framework\Module\Manager;
1213
use Magento\Reports\Model\ResourceModel\Order\Collection;
1314
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
@@ -61,7 +62,7 @@ protected function _prepareLayout()
6162
) || $this->getRequest()->getParam(
6263
'group'
6364
);
64-
$period = $this->getRequest()->getParam('period', '24h');
65+
$period = $this->getRequest()->getParam('period', Period::PERIOD_24_HOURS);
6566

6667
/* @var $collection Collection */
6768
$collection = $this->_collectionFactory->create()->addCreateAtPeriodFilter(

app/code/Magento/Backend/Controller/Adminhtml/Dashboard/Chart/Amounts.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public function __construct(
5454
public function execute(): Json
5555
{
5656
$data = [
57-
'data' => $this->chart->getByPeriod($this->_request->getParam('period'), 'revenue'),
57+
'data' => $this->chart->getByPeriod(
58+
$this->_request->getParam('period'),
59+
'revenue',
60+
$this->_request->getParam('store'),
61+
$this->_request->getParam('website'),
62+
$this->_request->getParam('group')
63+
),
5864
'label' => __('Revenue')
5965
];
6066

app/code/Magento/Backend/Controller/Adminhtml/Dashboard/Chart/Orders.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public function __construct(
5454
public function execute(): Json
5555
{
5656
$data = [
57-
'data' => $this->chart->getByPeriod($this->_request->getParam('period'), 'quantity'),
57+
'data' => $this->chart->getByPeriod(
58+
$this->_request->getParam('period'),
59+
'quantity',
60+
$this->_request->getParam('store'),
61+
$this->_request->getParam('website'),
62+
$this->_request->getParam('group')
63+
),
5864
'label' => __('Quantity')
5965
];
6066

app/code/Magento/Backend/Helper/Dashboard/Data.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
*/
66
namespace Magento\Backend\Helper\Dashboard;
77

8+
use Magento\Backend\Model\Dashboard\Period;
89
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\Helper\AbstractHelper;
11+
use Magento\Framework\App\Helper\Context;
12+
use Magento\Framework\App\ObjectManager;
913
use Magento\Framework\Config\ConfigOptionsListConstants;
14+
use Magento\Framework\Data\Collection\AbstractDb;
15+
use Magento\Store\Model\StoreManagerInterface;
1016

1117
/**
1218
* Data helper for dashboard
1319
*
1420
* @api
1521
* @since 100.0.2
1622
*/
17-
class Data extends \Magento\Framework\App\Helper\AbstractHelper
23+
class Data extends AbstractHelper
1824
{
1925
/**
20-
* @var \Magento\Framework\Data\Collection\AbstractDb
26+
* @var AbstractDb
2127
*/
2228
protected $_stores;
2329

@@ -27,25 +33,33 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
2733
protected $_installDate;
2834

2935
/**
30-
* @var \Magento\Store\Model\StoreManagerInterface
36+
* @var StoreManagerInterface
3137
*/
3238
private $_storeManager;
3339

3440
/**
35-
* @param \Magento\Framework\App\Helper\Context $context
36-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
41+
* @var Period
42+
*/
43+
private $period;
44+
45+
/**
46+
* @param Context $context
47+
* @param StoreManagerInterface $storeManager
3748
* @param DeploymentConfig $deploymentConfig
49+
* @param Period|null $period
50+
* @throws \Magento\Framework\Exception\FileSystemException
51+
* @throws \Magento\Framework\Exception\RuntimeException
3852
*/
3953
public function __construct(
40-
\Magento\Framework\App\Helper\Context $context,
41-
\Magento\Store\Model\StoreManagerInterface $storeManager,
42-
DeploymentConfig $deploymentConfig
54+
Context $context,
55+
StoreManagerInterface $storeManager,
56+
DeploymentConfig $deploymentConfig,
57+
?Period $period = null
4358
) {
44-
parent::__construct(
45-
$context
46-
);
59+
parent::__construct($context);
4760
$this->_installDate = $deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE);
4861
$this->_storeManager = $storeManager;
62+
$this->period = $period ?? ObjectManager::getInstance()->get(Period::class);
4963
}
5064

5165
/**
@@ -74,17 +88,14 @@ public function countStores()
7488
/**
7589
* Prepare array with periods for dashboard graphs
7690
*
91+
* @deprecated periods were moved to it's own class
92+
* @see Period::getDatePeriods()
93+
*
7794
* @return array
7895
*/
7996
public function getDatePeriods()
8097
{
81-
return [
82-
'24h' => __('Last 24 Hours'),
83-
'7d' => __('Last 7 Days'),
84-
'1m' => __('Current Month'),
85-
'1y' => __('YTD'),
86-
'2y' => __('2YTD')
87-
];
98+
return $this->period->getDatePeriods();
8899
}
89100

90101
/**

app/code/Magento/Backend/Model/Dashboard/Chart.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77

88
namespace Magento\Backend\Model\Dashboard;
99

10-
use Magento\Backend\Helper\Dashboard\Data as DataHelper;
1110
use Magento\Backend\Helper\Dashboard\Order as OrderHelper;
1211
use Magento\Backend\Model\Dashboard\Chart\Date;
13-
use Magento\Framework\App\RequestInterface;
1412

1513
/**
1614
* Dashboard chart data retriever
1715
*/
1816
class Chart
1917
{
20-
/**
21-
* @var RequestInterface
22-
*/
23-
private $request;
24-
2518
/**
2619
* @var Date
2720
*/
@@ -33,47 +26,52 @@ class Chart
3326
private $orderHelper;
3427

3528
/**
36-
* @var DataHelper
29+
* @var Period
3730
*/
38-
private $dataHelper;
31+
private $period;
3932

4033
/**
4134
* Chart constructor.
42-
* @param RequestInterface $request
4335
* @param Date $dateRetriever
4436
* @param OrderHelper $orderHelper
45-
* @param DataHelper $dataHelper
37+
* @param Period $period
4638
*/
4739
public function __construct(
48-
RequestInterface $request,
4940
Date $dateRetriever,
5041
OrderHelper $orderHelper,
51-
DataHelper $dataHelper
42+
Period $period
5243
) {
53-
$this->request = $request;
5444
$this->dateRetriever = $dateRetriever;
5545
$this->orderHelper = $orderHelper;
56-
$this->dataHelper = $dataHelper;
46+
$this->period = $period;
5747
}
5848

5949
/**
60-
* Get chart data by period and chart type parameter
50+
* Get chart data by period and chart type parameter, with possibility to pass scope parameters
6151
*
6252
* @param string $period
6353
* @param string $chartParam
54+
* @param string|null $store
55+
* @param string|null $website
56+
* @param string|null $group
6457
*
6558
* @return array
6659
*/
67-
public function getByPeriod($period, $chartParam): array
68-
{
69-
$this->orderHelper->setParam('store', $this->request->getParam('store'));
70-
$this->orderHelper->setParam('website', $this->request->getParam('website'));
71-
$this->orderHelper->setParam('group', $this->request->getParam('group'));
60+
public function getByPeriod(
61+
string $period,
62+
string $chartParam,
63+
string $store = null,
64+
string $website = null,
65+
string $group = null
66+
): array {
67+
$this->orderHelper->setParam('store', $store);
68+
$this->orderHelper->setParam('website', $website);
69+
$this->orderHelper->setParam('group', $group);
7270

73-
$availablePeriods = array_keys($this->dataHelper->getDatePeriods());
71+
$availablePeriods = array_keys($this->period->getDatePeriods());
7472
$this->orderHelper->setParam(
7573
'period',
76-
$period && in_array($period, $availablePeriods, false) ? $period : '24h'
74+
$period && in_array($period, $availablePeriods, false) ? $period : Period::PERIOD_24_HOURS
7775
);
7876

7977
$dates = $this->dateRetriever->getByPeriod($period);

app/code/Magento/Backend/Model/Dashboard/Chart/Date.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Backend\Model\Dashboard\Chart;
99

1010
use DateTimeZone;
11+
use Magento\Backend\Model\Dashboard\Period;
1112
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1213
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
1314

@@ -17,14 +18,14 @@
1718
class Date
1819
{
1920
/**
20-
* @var TimezoneInterface
21+
* @var CollectionFactory
2122
*/
22-
private $localeDate;
23+
private $collectionFactory;
2324

2425
/**
25-
* @var CollectionFactory
26+
* @var TimezoneInterface
2627
*/
27-
private $collectionFactory;
28+
private $localeDate;
2829

2930
/**
3031
* Date constructor.
@@ -35,8 +36,8 @@ public function __construct(
3536
CollectionFactory $collectionFactory,
3637
TimezoneInterface $localeDate
3738
) {
38-
$this->localeDate = $localeDate;
3939
$this->collectionFactory = $collectionFactory;
40+
$this->localeDate = $localeDate;
4041
}
4142

4243
/**
@@ -46,7 +47,7 @@ public function __construct(
4647
*
4748
* @return array
4849
*/
49-
public function getByPeriod($period): array
50+
public function getByPeriod(string $period): array
5051
{
5152
[$dateStart, $dateEnd] = $this->collectionFactory->create()->getDateRange(
5253
$period,
@@ -60,7 +61,7 @@ public function getByPeriod($period): array
6061
$dateStart->setTimezone(new DateTimeZone($timezoneLocal));
6162
$dateEnd->setTimezone(new DateTimeZone($timezoneLocal));
6263

63-
if ($period === '24h') {
64+
if ($period === Period::PERIOD_24_HOURS) {
6465
$dateEnd->modify('-1 hour');
6566
} else {
6667
$dateEnd->setTime(23, 59, 59);
@@ -71,13 +72,13 @@ public function getByPeriod($period): array
7172

7273
while ($dateStart <= $dateEnd) {
7374
switch ($period) {
74-
case '7d':
75-
case '1m':
75+
case Period::PERIOD_7_DAYS:
76+
case Period::PERIOD_1_MONTH:
7677
$d = $dateStart->format('Y-m-d');
7778
$dateStart->modify('+1 day');
7879
break;
79-
case '1y':
80-
case '2y':
80+
case Period::PERIOD_1_YEAR:
81+
case Period::PERIOD_2_YEARS:
8182
$d = $dateStart->format('Y-m');
8283
$dateStart->modify('first day of next month');
8384
break;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Model\Dashboard;
9+
10+
/**
11+
* Dashboard period info retriever
12+
*/
13+
class Period
14+
{
15+
public const PERIOD_24_HOURS = '24h';
16+
public const PERIOD_7_DAYS = '7d';
17+
public const PERIOD_1_MONTH = '1m';
18+
public const PERIOD_1_YEAR = '1y';
19+
public const PERIOD_2_YEARS = '2y';
20+
21+
private const PERIOD_UNIT_HOUR = 'hour';
22+
private const PERIOD_UNIT_DAY = 'day';
23+
private const PERIOD_UNIT_MONTH = 'month';
24+
25+
/**
26+
* Prepare array with periods for dashboard graphs
27+
*
28+
* @return array
29+
*/
30+
public function getDatePeriods(): array
31+
{
32+
return [
33+
static::PERIOD_24_HOURS => __('Last 24 Hours'),
34+
static::PERIOD_7_DAYS => __('Last 7 Days'),
35+
static::PERIOD_1_MONTH => __('Current Month'),
36+
static::PERIOD_1_YEAR => __('YTD'),
37+
static::PERIOD_2_YEARS => __('2YTD')
38+
];
39+
}
40+
41+
/**
42+
* Prepare array with periods mapping to chart units
43+
*
44+
* @return array
45+
*/
46+
public function getPeriodChartUnits(): array
47+
{
48+
return [
49+
static::PERIOD_24_HOURS => static::PERIOD_UNIT_HOUR,
50+
static::PERIOD_7_DAYS => static::PERIOD_UNIT_DAY,
51+
static::PERIOD_1_MONTH => static::PERIOD_UNIT_DAY,
52+
static::PERIOD_1_YEAR => static::PERIOD_UNIT_MONTH,
53+
static::PERIOD_2_YEARS => static::PERIOD_UNIT_MONTH
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)