Data.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Reports data helper
  8. */
  9. namespace Magento\Reports\Helper;
  10. use Magento\Framework\Data\Collection;
  11. use Magento\Framework\Stdlib\DateTime;
  12. /**
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  17. {
  18. const REPORT_PERIOD_TYPE_DAY = 'day';
  19. const REPORT_PERIOD_TYPE_MONTH = 'month';
  20. const REPORT_PERIOD_TYPE_YEAR = 'year';
  21. /**
  22. * Item factory
  23. *
  24. * @var \Magento\Reports\Model\ItemFactory
  25. */
  26. protected $_itemFactory;
  27. /**
  28. * Constructor
  29. *
  30. * @param \Magento\Framework\App\Helper\Context $context
  31. * @param \Magento\Reports\Model\ItemFactory $itemFactory
  32. */
  33. public function __construct(
  34. \Magento\Framework\App\Helper\Context $context,
  35. \Magento\Reports\Model\ItemFactory $itemFactory
  36. ) {
  37. parent::__construct($context);
  38. $this->_itemFactory = $itemFactory;
  39. }
  40. /**
  41. * Retrieve array of intervals
  42. *
  43. * @param string $from
  44. * @param string $to
  45. * @param string $period
  46. * @return array
  47. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  48. */
  49. public function getIntervals($from, $to, $period = self::REPORT_PERIOD_TYPE_DAY)
  50. {
  51. $intervals = [];
  52. if (!$from && !$to) {
  53. return $intervals;
  54. }
  55. $dateStart = new \DateTime($from);
  56. $dateEnd = new \DateTime($to);
  57. while ($dateStart->diff($dateEnd)->invert == 0) {
  58. switch ($period) {
  59. case self::REPORT_PERIOD_TYPE_DAY:
  60. $intervals[] = $dateStart->format('Y-m-d');
  61. $dateStart->add(new \DateInterval('P1D'));
  62. break;
  63. case self::REPORT_PERIOD_TYPE_MONTH:
  64. $intervals[] = $dateStart->format('Y-m');
  65. $dateStart->add(new \DateInterval('P1M'));
  66. break;
  67. case self::REPORT_PERIOD_TYPE_YEAR:
  68. $intervals[] = $dateStart->format('Y');
  69. $dateStart->add(new \DateInterval('P1Y'));
  70. break;
  71. }
  72. }
  73. return $intervals;
  74. }
  75. /**
  76. * Add items to interval collection
  77. *
  78. * @param Collection $collection
  79. * @param string $from
  80. * @param string $to
  81. * @param string $periodType
  82. * @return void
  83. */
  84. public function prepareIntervalsCollection($collection, $from, $to, $periodType = self::REPORT_PERIOD_TYPE_DAY)
  85. {
  86. $intervals = $this->getIntervals($from, $to, $periodType);
  87. foreach ($intervals as $interval) {
  88. $item = $this->_itemFactory->create();
  89. $item->setPeriod($interval);
  90. $item->setIsEmpty();
  91. $collection->addItem($item);
  92. }
  93. }
  94. }