/** * {@inheritdoc} */ public function duplicate(RequestConfiguration $requestConfiguration, FactoryInterface $factory, ResourceInterface $originalResource) { if (null === ($method = $requestConfiguration->getFactoryMethod())) { $method = 'duplicate'; } if (!method_exists($factory, $method)) { throw new BadMethodCallException('Method "' . $method . '" not found in factory class'); } $callable = [$factory, $method]; $arguments = array_merge([$originalResource], $requestConfiguration->getFactoryArguments()); return call_user_func_array($callable, $arguments); }
/** * Moves resource to the top or bottom of a pagination page. * * @param RequestConfiguration $requestConfiguration * @param MetadataInterface $metadataInterface * @param $resource * @param EntityRepository $repository * @param int $page * @param bool $top */ public function moveToPage(RequestConfiguration $requestConfiguration, MetadataInterface $metadataInterface, $resource, $repository, $page, $top) { $property = $requestConfiguration->getSortablePosition(); $strategy = $requestConfiguration->getSortingStrategy(); $paginate = $requestConfiguration->getPaginationMaxPerPage(); $accessor = PropertyAccess::createPropertyAccessor(); $sorting = $strategy == self::STRATEGY_DESC_LAST || $strategy == self::STRATEGY_DESC_FIRST ? 'desc' : 'asc'; $targetPosition = ($page - 1) * $paginate + ($top ? 0 : $paginate - 1); $target = $repository->createQueryBuilder('r')->orderBy('r.' . $property, $sorting)->setFirstResult($targetPosition)->setMaxResults(1)->getQuery()->getOneOrNullResult(); if (!$target) { // The target position is after the last element, get last element instead $target = $repository->createQueryBuilder('r')->addOrderBy('r.' . $property, $sorting)->addOrderBy('r.id', 'DESC')->setMaxResults(1)->getQuery()->getOneOrNullResult(); } $resourceValue = $accessor->getValue($resource, $property); $targetValue = $accessor->getValue($target, $property); if ($resourceValue === null || $targetValue === null || $resourceValue == $targetValue) { // Errors in value consistency: recalculate all position values for this entity $this->recalculateSortingProperty($property, $repository); $targetValue = $accessor->getValue($target, $property); } // Execute movement $this->moveToPosition($resource, $targetValue, $property, $strategy, $repository); }