|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\ProductPriceDataExporter\Model\Query; |
| 10 | + |
| 11 | +use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface; |
| 12 | +use Magento\Framework\App\ResourceConnection; |
| 13 | +use Magento\Framework\Stdlib\DateTime; |
| 14 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Get _current_ date in website timezone in format [website_id => website_date, ...] |
| 18 | + */ |
| 19 | +class DateWebsiteProvider |
| 20 | +{ |
| 21 | + protected TimezoneInterface $localeDate; |
| 22 | + private ResourceConnection $resourceConnection; |
| 23 | + private CommerceDataExportLoggerInterface $logger; |
| 24 | + private DateTime $dateTime; |
| 25 | + private array $data = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param ResourceConnection $resourceConnection |
| 29 | + * @param DateTime $dateTime |
| 30 | + * @param TimezoneInterface $localeDate |
| 31 | + * @param CommerceDataExportLoggerInterface $logger |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + ResourceConnection $resourceConnection, |
| 35 | + DateTime $dateTime, |
| 36 | + TimezoneInterface $localeDate, |
| 37 | + CommerceDataExportLoggerInterface $logger |
| 38 | + ) { |
| 39 | + $this->resourceConnection = $resourceConnection; |
| 40 | + $this->logger = $logger; |
| 41 | + $this->dateTime = $dateTime; |
| 42 | + $this->localeDate = $localeDate; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function getWebsitesDate(): array |
| 49 | + { |
| 50 | + if (!empty($this->data)) { |
| 51 | + return $this->data; |
| 52 | + } |
| 53 | + $connection = $this->resourceConnection->getConnection(); |
| 54 | + $select = $connection->select()->from( |
| 55 | + ['cw' => $this->resourceConnection->getTableName('store_website')], |
| 56 | + ['website_id'] |
| 57 | + )->join( |
| 58 | + ['csg' => $this->resourceConnection->getTableName('store_group')], |
| 59 | + 'cw.default_group_id = csg.group_id', |
| 60 | + ['store_id' => 'default_store_id'] |
| 61 | + )->where( |
| 62 | + 'cw.website_id != 0' |
| 63 | + ); |
| 64 | + |
| 65 | + foreach ($connection->fetchAll($select) as $item) { |
| 66 | + try { |
| 67 | + $timestamp = $this->localeDate->scopeTimeStamp($item['store_id']); |
| 68 | + } catch (\Throwable $e) { |
| 69 | + // use current timestamp |
| 70 | + $timestamp = true; |
| 71 | + $this->logger->warning("can't obtain website datetime", ['exception' => $e]); |
| 72 | + } |
| 73 | + |
| 74 | + $this->data[$item['website_id']] = $this->dateTime->formatDate($timestamp, false); |
| 75 | + } |
| 76 | + return $this->data; |
| 77 | + } |
| 78 | +} |
0 commit comments