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()); + } +} diff --git a/composer.json b/composer.json index 92cb2eb..cca7955 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "pagos", "magento2" ], - "version": "2.5.3", + "version": "2.5.4", "license": "MIT", "authors": [ { diff --git a/etc/module.xml b/etc/module.xml index 1879b31..c295180 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -5,7 +5,7 @@ */ --> - +