From fdd1af4542d4c789cc72ac67d7de5faa8bc19fa1 Mon Sep 17 00:00:00 2001 From: Marija Date: Tue, 22 Oct 2024 13:41:30 +0200 Subject: [PATCH] Add migration to save send report for existing users CS-5957 --- Setup/Patch/Data/Version254.php | 127 ++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Setup/Patch/Data/Version254.php diff --git a/Setup/Patch/Data/Version254.php b/Setup/Patch/Data/Version254.php new file mode 100644 index 0000000..98f4082 --- /dev/null +++ b/Setup/Patch/Data/Version254.php @@ -0,0 +1,127 @@ +getStatisticalEntitiesForAllStores(); + + foreach ($statisticalDataArray as $statisticalData) { + $storeId = $statisticalData->getStoreId(); + + if ( + $this->getSendReportForGivenStore($storeId) || + !$statisticalData->getStatisticalData()->isSendStatisticalData() + ) { + return $this; + } + + $this->saveSendReport($storeId); + } + + return $this; + } + + /** + * @return StatisticalDataEntity[] + * + * @throws RepositoryNotRegisteredException + */ + private function getStatisticalEntitiesForAllStores(): array + { + $statisticalDataRepository = RepositoryRegistry::getRepository(StatisticalDataEntity::getClassName()); + + return $statisticalDataRepository->select(); + } + + /** + * @param string $storeId + * + * @return SendReportEntity|null + * + * @throws QueryFilterInvalidParamException + * @throws RepositoryNotRegisteredException + */ + private function getSendReportForGivenStore(string $storeId): ?SendReportEntity + { + $queryFilter = new QueryFilter(); + $queryFilter->where('context', Operators::EQUALS, $storeId); + + /** @var SendReportEntity|null $sendReport $sendReport */ + $sendReport = $this->getSendReportRepository()->selectOne($queryFilter); + + return $sendReport; + } + + /** + * @throws RepositoryNotRegisteredException + */ + private function saveSendReport(string $storeId): void + { + $sendReport = new SendReport(strtotime(self::SCHEDULE_TIME_NEXT_DAY)); + + if ((new DateTime())->getTimestamp() <= strtotime(self::SCHEDULE_TIME)) { + $sendReport = new SendReport(strtotime(self::SCHEDULE_TIME)); + } + + $sendReportEntity = new SendReportEntity(); + $sendReportEntity->setContext($storeId); + $sendReportEntity->setSendReportTime($sendReport->getSendReportTime()); + $sendReportEntity->setSendReport($sendReport); + $this->getSendReportRepository()->save($sendReportEntity); + } + + /** + * @return RepositoryInterface + * + * @throws RepositoryNotRegisteredException + */ + private function getSendReportRepository(): RepositoryInterface + { + return RepositoryRegistry::getRepository(SendReportEntity::getClassName()); + } +}