From 11ed78cb486edb46aa36a0fb9c0c62c96a2e6c87 Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 01:53:56 +0300 Subject: [PATCH 01/14] Make order expot efficient --- src/Controller/ExportDataController.php | 12 ++- .../ORM/Hydrator/HydratorInterface.php | 16 ++++ src/Exporter/ORM/Hydrator/OrderHydrator.php | 88 +++++++++++++++++++ src/Exporter/Plugin/OrderResourcePlugin.php | 19 +++- src/Exporter/Plugin/ResourcePlugin.php | 2 +- src/Resources/config/services.yml | 7 ++ 6 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 src/Exporter/ORM/Hydrator/HydratorInterface.php create mode 100644 src/Exporter/ORM/Hydrator/OrderHydrator.php diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 9e37c514..2269e400 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -6,6 +6,7 @@ use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ExporterRegistry; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ResourceExporterInterface; +use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface; @@ -101,9 +102,14 @@ private function findRepository(string $resource): RepositoryInterface */ private function getResourceIds($resources): array { - return array_map(function (ResourceInterface $resource) { - return $resource->getId(); - }, $this->getResources($resources)); + if ($resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { + $query = $resources->getData()->getAdapter()->getQuery()->setMaxResults(null); + return array_column($query->getArrayResult(), 'id'); + } else { + return array_map(function (ResourceInterface $resource) { + return $resource->getId(); + }, $this->getResources($resources)); + } } /** diff --git a/src/Exporter/ORM/Hydrator/HydratorInterface.php b/src/Exporter/ORM/Hydrator/HydratorInterface.php new file mode 100644 index 00000000..ad634b9b --- /dev/null +++ b/src/Exporter/ORM/Hydrator/HydratorInterface.php @@ -0,0 +1,16 @@ +repository = $repository; + } + + /** + * @param int[]|string[] $idsToExport + * @return ResourceInterface[] + */ + public function getHydratedResources(array $idsToExport): array + { + /** @var ResourceInterface[] $items */ + if ($this->repository instanceof \Doctrine\ORM\EntityRepository) { + $query = $this->findOrdersQb($idsToExport)->getQuery(); + $items = $this->enableEagerLoading($query)->getResult(); + $this->hydrateOrderItemsQb($idsToExport)->getQuery()->getResult(); // This result can be discarded + } else { + $items = $this->repository->findBy(['id' => $idsToExport]); + } + + return $items; + } + + /** + * @param int[]|string[] $idsToExport + * @return QueryBuilder + */ + protected function findOrdersQb(array $idsToExport): QueryBuilder + { + return $this->repository->createQueryBuilder('o') + ->andWhere('o.id IN (:exportIds)') + ->setParameter('exportIds', $idsToExport) + ; + } + + /** + * @param int[]|string[] $idsToExport + * @return QueryBuilder + */ + protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder + { + // Partial hydration to make sure order items don't get lazy-loaded + return $this->repository->createQueryBuilder('o') + ->select('PARTIAL o.{id}, items') + ->leftJoin('o.items', 'items') + ->andWhere('o.id IN (:exportIds)') + ->setParameter('exportIds', $idsToExport) + ; + } + + protected function enableEagerLoading(Query $query): Query + { + return $query + ->setFetchMode( + $this->repository->getClassName(), + 'customer', + ClassMetadata::FETCH_EAGER + ) + ->setFetchMode( + $this->repository->getClassName(), + 'shippingAddress', + ClassMetadata::FETCH_EAGER + ) + ->setFetchMode( + $this->repository->getClassName(), + 'billingAddress', + ClassMetadata::FETCH_EAGER + ) + ; + } +} diff --git a/src/Exporter/Plugin/OrderResourcePlugin.php b/src/Exporter/Plugin/OrderResourcePlugin.php index c30c968f..54d0a5a6 100644 --- a/src/Exporter/Plugin/OrderResourcePlugin.php +++ b/src/Exporter/Plugin/OrderResourcePlugin.php @@ -5,11 +5,13 @@ namespace FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin; use Doctrine\ORM\EntityManagerInterface; +use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ORM\Hydrator\HydratorInterface; use FriendsOfSylius\SyliusImportExportPlugin\Service\AddressConcatenationInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\OrderItemInterface; use Sylius\Component\Product\Model\ProductInterface; use Sylius\Component\Product\Model\ProductVariantInterface; +use Sylius\Component\Resource\Model\ResourceInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; @@ -20,15 +22,22 @@ class OrderResourcePlugin extends ResourcePlugin */ private $addressConcatenation; + /** + * @var HydratorInterface + */ + private $orderHydrator; + public function __construct( RepositoryInterface $repository, PropertyAccessorInterface $propertyAccessor, EntityManagerInterface $entityManager, - AddressConcatenationInterface $addressConcatenation + AddressConcatenationInterface $addressConcatenation, + HydratorInterface $orderHydrator ) { parent::__construct($repository, $propertyAccessor, $entityManager); $this->addressConcatenation = $addressConcatenation; + $this->orderHydrator = $orderHydrator; } /** @@ -131,4 +140,12 @@ private function addOrderItemData(array $items, OrderInterface $resource): void $this->addDataForResource($resource, 'Product_list', $str); } + + protected function findResources(array $idsToExport): array + { + /** @var ResourceInterface[] $items */ + $items = $this->orderHydrator->getHydratedResources($idsToExport); + + return $items; + } } diff --git a/src/Exporter/Plugin/ResourcePlugin.php b/src/Exporter/Plugin/ResourcePlugin.php index 0792176c..f832d464 100644 --- a/src/Exporter/Plugin/ResourcePlugin.php +++ b/src/Exporter/Plugin/ResourcePlugin.php @@ -138,7 +138,7 @@ private function addDataForId(ResourceInterface $resource): void * * @return ResourceInterface[] */ - private function findResources(array $idsToExport): array + protected function findResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ $items = $this->repository->findBy(['id' => $idsToExport]); diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index cd3126fb..96e19bf1 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -100,6 +100,7 @@ services: - "@property_accessor" - "@doctrine.orm.entity_manager" - "@sylius.service.address_concatenation" + - "@sylius.exporter.orm.hydrator.orders" sylius.exporter.plugin.resource.customers: class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\ResourcePlugin @@ -108,6 +109,12 @@ services: - "@property_accessor" - "@doctrine.orm.entity_manager" + # ORM hydrators to improve performance + sylius.exporter.orm.hydrator.orders: + class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\ORM\Hydrator\OrderHydrator + arguments: + - "@sylius.repository.order" + # PluginPools for Exporters. Can contain multiple Plugins sylius.exporter.pluginpool.countries: class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\PluginPool From b4a72857b379ee3063b28d02dc8b2a1a63ef81b0 Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 01:55:25 +0300 Subject: [PATCH 02/14] Fix missing newline --- src/Exporter/ORM/Hydrator/HydratorInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exporter/ORM/Hydrator/HydratorInterface.php b/src/Exporter/ORM/Hydrator/HydratorInterface.php index ad634b9b..76fca612 100644 --- a/src/Exporter/ORM/Hydrator/HydratorInterface.php +++ b/src/Exporter/ORM/Hydrator/HydratorInterface.php @@ -13,4 +13,4 @@ interface HydratorInterface * @return ResourceInterface[] */ public function getHydratedResources(array $idsToExport): array; -} \ No newline at end of file +} From 8058e260ea256667b038c59a78e182cb6ca94cd5 Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 02:13:17 +0300 Subject: [PATCH 03/14] Call getData only if instance of ResourceGridView --- src/Controller/ExportDataController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 2269e400..8ac50041 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -102,7 +102,8 @@ private function findRepository(string $resource): RepositoryInterface */ private function getResourceIds($resources): array { - if ($resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { + if ($resources instanceof ResourceGridView && + $resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { $query = $resources->getData()->getAdapter()->getQuery()->setMaxResults(null); return array_column($query->getArrayResult(), 'id'); } else { From bb067db1745e4e952c65f4ac2f42f2e447b3542c Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 02:17:10 +0300 Subject: [PATCH 04/14] Update tests --- spec/Exporter/Plugin/OrderResourcePluginSpec.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/Exporter/Plugin/OrderResourcePluginSpec.php b/spec/Exporter/Plugin/OrderResourcePluginSpec.php index 06e125eb..955f58b8 100644 --- a/spec/Exporter/Plugin/OrderResourcePluginSpec.php +++ b/spec/Exporter/Plugin/OrderResourcePluginSpec.php @@ -7,6 +7,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; +use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ORM\Hydrator\HydratorInterface; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\OrderResourcePlugin; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\ResourcePlugin; use FriendsOfSylius\SyliusImportExportPlugin\Service\AddressConcatenationInterface; @@ -29,9 +30,10 @@ function let( RepositoryInterface $repository, PropertyAccessorInterface $propertyAccessor, EntityManagerInterface $entityManager, - AddressConcatenationInterface $addressConcatenation + AddressConcatenationInterface $addressConcatenation, + HydratorInterface $hydrator ) { - $this->beConstructedWith($repository, $propertyAccessor, $entityManager, $addressConcatenation); + $this->beConstructedWith($repository, $propertyAccessor, $entityManager, $addressConcatenation, $hydrator); } function it_is_initializable() From 75e1d8f2de5c130d2147bbebf845715821354564 Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 02:19:22 +0300 Subject: [PATCH 05/14] Fix CS --- src/Controller/ExportDataController.php | 8 ++++---- src/Exporter/ORM/Hydrator/HydratorInterface.php | 1 + src/Exporter/ORM/Hydrator/OrderHydrator.php | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 8ac50041..8fd132d1 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -106,11 +106,11 @@ private function getResourceIds($resources): array $resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { $query = $resources->getData()->getAdapter()->getQuery()->setMaxResults(null); return array_column($query->getArrayResult(), 'id'); - } else { - return array_map(function (ResourceInterface $resource) { - return $resource->getId(); - }, $this->getResources($resources)); } + + return array_map(function (ResourceInterface $resource) { + return $resource->getId(); + }, $this->getResources($resources)); } /** diff --git a/src/Exporter/ORM/Hydrator/HydratorInterface.php b/src/Exporter/ORM/Hydrator/HydratorInterface.php index 76fca612..5d4bfea8 100644 --- a/src/Exporter/ORM/Hydrator/HydratorInterface.php +++ b/src/Exporter/ORM/Hydrator/HydratorInterface.php @@ -10,6 +10,7 @@ interface HydratorInterface { /** * @param int[]|string[] $idsToExport + * * @return ResourceInterface[] */ public function getHydratedResources(array $idsToExport): array; diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index ed6b7883..1341dab3 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -22,6 +22,7 @@ public function __construct( /** * @param int[]|string[] $idsToExport + * * @return ResourceInterface[] */ public function getHydratedResources(array $idsToExport): array @@ -40,7 +41,6 @@ public function getHydratedResources(array $idsToExport): array /** * @param int[]|string[] $idsToExport - * @return QueryBuilder */ protected function findOrdersQb(array $idsToExport): QueryBuilder { @@ -52,7 +52,6 @@ protected function findOrdersQb(array $idsToExport): QueryBuilder /** * @param int[]|string[] $idsToExport - * @return QueryBuilder */ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { From ad83380eee9e575c059e77ce73be7067b73898f7 Mon Sep 17 00:00:00 2001 From: dnna Date: Thu, 13 Sep 2018 02:30:20 +0300 Subject: [PATCH 06/14] More test and CS updates --- spec/Exporter/Plugin/OrderResourcePluginSpec.php | 10 ++++------ src/Controller/ExportDataController.php | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/Exporter/Plugin/OrderResourcePluginSpec.php b/spec/Exporter/Plugin/OrderResourcePluginSpec.php index 955f58b8..a2e52da0 100644 --- a/spec/Exporter/Plugin/OrderResourcePluginSpec.php +++ b/spec/Exporter/Plugin/OrderResourcePluginSpec.php @@ -47,19 +47,17 @@ function it_implements_the_resource_plugin_interface() } function it_should_add_customer_data( - RepositoryInterface $repository, EntityManagerInterface $entityManager, OrderInterface $resource, PropertyAccessorInterface $propertyAccessor, ClassMetaData $classMetadata, - AddressConcatenationInterface $addressConcatenation + AddressConcatenationInterface $addressConcatenation, + HydratorInterface $hydrator ) { $idsToExport = [1]; - $repository->findBy( - [ - 'id' => $idsToExport, - ] + $hydrator->getHydratedResources( + $idsToExport )->willReturn( [ $resource, diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 8fd132d1..3ba1f3b2 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -105,6 +105,7 @@ private function getResourceIds($resources): array if ($resources instanceof ResourceGridView && $resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { $query = $resources->getData()->getAdapter()->getQuery()->setMaxResults(null); + return array_column($query->getArrayResult(), 'id'); } From 35a6ed3fb4c8493525dd4c5c700a8444d488646b Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 22:11:31 +0300 Subject: [PATCH 07/14] Changes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 23 ++++++++++++--------- src/Exporter/Plugin/OrderResourcePlugin.php | 2 +- src/Exporter/Plugin/ResourcePlugin.php | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 1341dab3..629821d9 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -12,6 +12,9 @@ class OrderHydrator implements HydratorInterface { + /** + * @var RepositoryInterface + */ protected $repository; public function __construct( @@ -21,28 +24,27 @@ public function __construct( } /** - * @param int[]|string[] $idsToExport - * - * @return ResourceInterface[] + * @inheritdoc */ public function getHydratedResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ - if ($this->repository instanceof \Doctrine\ORM\EntityRepository) { - $query = $this->findOrdersQb($idsToExport)->getQuery(); - $items = $this->enableEagerLoading($query)->getResult(); - $this->hydrateOrderItemsQb($idsToExport)->getQuery()->getResult(); // This result can be discarded - } else { - $items = $this->repository->findBy(['id' => $idsToExport]); + if (!$this->repository instanceof \Doctrine\ORM\EntityRepository) { + return $this->repository->findBy(['id' => $idsToExport]); } + $query = $this->findOrdersQb($idsToExport)->getQuery(); + $items = $this->enableEagerLoading($query)->getResult(); + $this->hydrateOrderItemsQb($idsToExport)->getQuery()->getResult(); // This result can be discarded + return $items; } /** * @param int[]|string[] $idsToExport + * @return QueryBuilder */ - protected function findOrdersQb(array $idsToExport): QueryBuilder + private function findOrdersQb(array $idsToExport): QueryBuilder { return $this->repository->createQueryBuilder('o') ->andWhere('o.id IN (:exportIds)') @@ -52,6 +54,7 @@ protected function findOrdersQb(array $idsToExport): QueryBuilder /** * @param int[]|string[] $idsToExport + * @return QueryBuilder */ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { diff --git a/src/Exporter/Plugin/OrderResourcePlugin.php b/src/Exporter/Plugin/OrderResourcePlugin.php index 54d0a5a6..40b9a5df 100644 --- a/src/Exporter/Plugin/OrderResourcePlugin.php +++ b/src/Exporter/Plugin/OrderResourcePlugin.php @@ -141,7 +141,7 @@ private function addOrderItemData(array $items, OrderInterface $resource): void $this->addDataForResource($resource, 'Product_list', $str); } - protected function findResources(array $idsToExport): array + private function findResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ $items = $this->orderHydrator->getHydratedResources($idsToExport); diff --git a/src/Exporter/Plugin/ResourcePlugin.php b/src/Exporter/Plugin/ResourcePlugin.php index f832d464..0792176c 100644 --- a/src/Exporter/Plugin/ResourcePlugin.php +++ b/src/Exporter/Plugin/ResourcePlugin.php @@ -138,7 +138,7 @@ private function addDataForId(ResourceInterface $resource): void * * @return ResourceInterface[] */ - protected function findResources(array $idsToExport): array + private function findResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ $items = $this->repository->findBy(['id' => $idsToExport]); From 1293a8db7a2b3906be4d16f7d1120877bc5da8ee Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 22:24:56 +0300 Subject: [PATCH 08/14] More fixes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 4 +--- src/Exporter/Plugin/OrderResourcePlugin.php | 2 +- src/Exporter/Plugin/ResourcePlugin.php | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 629821d9..5d24bbb9 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -24,7 +24,7 @@ public function __construct( } /** - * @inheritdoc + * {@inheritdoc} */ public function getHydratedResources(array $idsToExport): array { @@ -42,7 +42,6 @@ public function getHydratedResources(array $idsToExport): array /** * @param int[]|string[] $idsToExport - * @return QueryBuilder */ private function findOrdersQb(array $idsToExport): QueryBuilder { @@ -54,7 +53,6 @@ private function findOrdersQb(array $idsToExport): QueryBuilder /** * @param int[]|string[] $idsToExport - * @return QueryBuilder */ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { diff --git a/src/Exporter/Plugin/OrderResourcePlugin.php b/src/Exporter/Plugin/OrderResourcePlugin.php index 40b9a5df..54d0a5a6 100644 --- a/src/Exporter/Plugin/OrderResourcePlugin.php +++ b/src/Exporter/Plugin/OrderResourcePlugin.php @@ -141,7 +141,7 @@ private function addOrderItemData(array $items, OrderInterface $resource): void $this->addDataForResource($resource, 'Product_list', $str); } - private function findResources(array $idsToExport): array + protected function findResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ $items = $this->orderHydrator->getHydratedResources($idsToExport); diff --git a/src/Exporter/Plugin/ResourcePlugin.php b/src/Exporter/Plugin/ResourcePlugin.php index 0792176c..f832d464 100644 --- a/src/Exporter/Plugin/ResourcePlugin.php +++ b/src/Exporter/Plugin/ResourcePlugin.php @@ -138,7 +138,7 @@ private function addDataForId(ResourceInterface $resource): void * * @return ResourceInterface[] */ - private function findResources(array $idsToExport): array + protected function findResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ $items = $this->repository->findBy(['id' => $idsToExport]); From 90d926d32735cc3b362cbebf3820bfce603792ee Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 22:36:38 +0300 Subject: [PATCH 09/14] Merge latest version and more fixes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 5d24bbb9..9710785c 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -30,7 +30,8 @@ public function getHydratedResources(array $idsToExport): array { /** @var ResourceInterface[] $items */ if (!$this->repository instanceof \Doctrine\ORM\EntityRepository) { - return $this->repository->findBy(['id' => $idsToExport]); + $items = $this->repository->findBy(['id' => $idsToExport]); + return $items; } $query = $this->findOrdersQb($idsToExport)->getQuery(); From 1fd83bcd53d46cec67c3ce02df1a4f4528977105 Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 23:05:03 +0300 Subject: [PATCH 10/14] Fixes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 9710785c..97302557 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -28,8 +28,8 @@ public function __construct( */ public function getHydratedResources(array $idsToExport): array { - /** @var ResourceInterface[] $items */ if (!$this->repository instanceof \Doctrine\ORM\EntityRepository) { + /** @var ResourceInterface[] $items */ $items = $this->repository->findBy(['id' => $idsToExport]); return $items; } @@ -46,7 +46,11 @@ public function getHydratedResources(array $idsToExport): array */ private function findOrdersQb(array $idsToExport): QueryBuilder { - return $this->repository->createQueryBuilder('o') + /** + * @var \Doctrine\ORM\EntityRepository $repository + */ + $repository = $this->repository; + return $repository->createQueryBuilder('o') ->andWhere('o.id IN (:exportIds)') ->setParameter('exportIds', $idsToExport) ; @@ -57,8 +61,12 @@ private function findOrdersQb(array $idsToExport): QueryBuilder */ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { + /** + * @var \Doctrine\ORM\EntityRepository $repository + */ + $repository = $this->repository; // Partial hydration to make sure order items don't get lazy-loaded - return $this->repository->createQueryBuilder('o') + return $repository->createQueryBuilder('o') ->select('PARTIAL o.{id}, items') ->leftJoin('o.items', 'items') ->andWhere('o.id IN (:exportIds)') From ec01c99ec632c9be01aa178de60d23e5709f66ec Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 23:11:19 +0300 Subject: [PATCH 11/14] More phpstan fixes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 97302557..8cd59514 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -31,6 +31,7 @@ public function getHydratedResources(array $idsToExport): array if (!$this->repository instanceof \Doctrine\ORM\EntityRepository) { /** @var ResourceInterface[] $items */ $items = $this->repository->findBy(['id' => $idsToExport]); + return $items; } @@ -47,7 +48,7 @@ public function getHydratedResources(array $idsToExport): array private function findOrdersQb(array $idsToExport): QueryBuilder { /** - * @var \Doctrine\ORM\EntityRepository $repository + * @var \Doctrine\ORM\EntityRepository */ $repository = $this->repository; return $repository->createQueryBuilder('o') @@ -62,7 +63,7 @@ private function findOrdersQb(array $idsToExport): QueryBuilder protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { /** - * @var \Doctrine\ORM\EntityRepository $repository + * @var \Doctrine\ORM\EntityRepository */ $repository = $this->repository; // Partial hydration to make sure order items don't get lazy-loaded From d3ea766d796524fa799cb184687aad583aee0c61 Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 7 Oct 2018 23:17:22 +0300 Subject: [PATCH 12/14] More phpstan fixes --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index 8cd59514..dd6710f1 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -51,6 +51,7 @@ private function findOrdersQb(array $idsToExport): QueryBuilder * @var \Doctrine\ORM\EntityRepository */ $repository = $this->repository; + return $repository->createQueryBuilder('o') ->andWhere('o.id IN (:exportIds)') ->setParameter('exportIds', $idsToExport) @@ -66,6 +67,7 @@ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder * @var \Doctrine\ORM\EntityRepository */ $repository = $this->repository; + // Partial hydration to make sure order items don't get lazy-loaded return $repository->createQueryBuilder('o') ->select('PARTIAL o.{id}, items') From 94fdfe3266d4fe63866f53fa5d409c25e633f905 Mon Sep 17 00:00:00 2001 From: dnna Date: Sat, 20 Oct 2018 19:04:47 +0300 Subject: [PATCH 13/14] Make OrderHydrator final --- src/Controller/ExportDataController.php | 4 ++-- src/Exporter/ORM/Hydrator/OrderHydrator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 3ba1f3b2..6ff68f53 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -102,8 +102,8 @@ private function findRepository(string $resource): RepositoryInterface */ private function getResourceIds($resources): array { - if ($resources instanceof ResourceGridView && - $resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { + if ($resources instanceof ResourceGridView + && $resources->getData()->getAdapter() instanceof DoctrineORMAdapter) { $query = $resources->getData()->getAdapter()->getQuery()->setMaxResults(null); return array_column($query->getArrayResult(), 'id'); diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index dd6710f1..a496fc06 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -10,7 +10,7 @@ use Sylius\Component\Resource\Model\ResourceInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; -class OrderHydrator implements HydratorInterface +final class OrderHydrator implements HydratorInterface { /** * @var RepositoryInterface From 6742a85281920b376976af47494f24514dab4283 Mon Sep 17 00:00:00 2001 From: dnna Date: Sat, 20 Oct 2018 19:11:29 +0300 Subject: [PATCH 14/14] Make functions private --- src/Exporter/ORM/Hydrator/OrderHydrator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Exporter/ORM/Hydrator/OrderHydrator.php b/src/Exporter/ORM/Hydrator/OrderHydrator.php index a496fc06..bca0bda2 100644 --- a/src/Exporter/ORM/Hydrator/OrderHydrator.php +++ b/src/Exporter/ORM/Hydrator/OrderHydrator.php @@ -15,7 +15,7 @@ final class OrderHydrator implements HydratorInterface /** * @var RepositoryInterface */ - protected $repository; + private $repository; public function __construct( RepositoryInterface $repository @@ -61,7 +61,7 @@ private function findOrdersQb(array $idsToExport): QueryBuilder /** * @param int[]|string[] $idsToExport */ - protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder + private function hydrateOrderItemsQb(array $idsToExport): QueryBuilder { /** * @var \Doctrine\ORM\EntityRepository @@ -77,7 +77,7 @@ protected function hydrateOrderItemsQb(array $idsToExport): QueryBuilder ; } - protected function enableEagerLoading(Query $query): Query + private function enableEagerLoading(Query $query): Query { return $query ->setFetchMode(