| Conditions | 11 |
| Paths | 17 |
| Total Lines | 65 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function provide(OperationInterface $operation, array $uriVariables = [], array $context = []): mixed |
||
| 26 | { |
||
| 27 | if ($operation instanceof Create) { |
||
| 28 | $class = $operation->getResource()->getDataClass(); |
||
| 29 | |||
| 30 | return new $class(); |
||
| 31 | } |
||
| 32 | $manager = $this->registry->getManagerForClass($operation->getResource()->getDataClass()); |
||
|
|
|||
| 33 | |||
| 34 | if ($manager === null) { |
||
| 35 | throw new EntityManagerNotFoundException($operation); |
||
| 36 | } |
||
| 37 | /** @var EntityRepository $repository */ |
||
| 38 | $repository = $manager->getRepository($operation->getResource()->getDataClass()); |
||
| 39 | // Add a suffix to avoid error if the resource is named with a reserved keyword |
||
| 40 | $rootAlias = $operation->getResourceName().'_entity'; |
||
| 41 | |||
| 42 | $queryBuilder = $repository->createQueryBuilder($rootAlias); |
||
| 43 | $helper = new QueryBuilderHelper( |
||
| 44 | $queryBuilder, |
||
| 45 | $manager->getClassMetadata($operation->getResource()->getDataClass()), |
||
| 46 | ); |
||
| 47 | |||
| 48 | if ($operation instanceof CollectionOperationInterface) { |
||
| 49 | $orderBy = $operation->getOrderBy(); |
||
| 50 | |||
| 51 | if (($context['sort'] ?? false) && ($context['order'] ?? false)) { |
||
| 52 | $orderBy[$context['sort']] = $context['order']; |
||
| 53 | } |
||
| 54 | $helper->addOrderBy($orderBy); |
||
| 55 | $filters = []; |
||
| 56 | |||
| 57 | foreach ($operation->getFilters() as $filter) { |
||
| 58 | $data = $context['filters'][$filter->getName()] ?? null; |
||
| 59 | |||
| 60 | if ($data) { |
||
| 61 | $filters[] = $filter->withData($data); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | $helper->addFilters($filters); |
||
| 65 | |||
| 66 | if (!$operation->hasPagination()) { |
||
| 67 | return $helper |
||
| 68 | ->getQueryBuilder() |
||
| 69 | ->getQuery() |
||
| 70 | ->getResult() |
||
| 71 | ; |
||
| 72 | } |
||
| 73 | $pager = new Pagerfanta(new QueryAdapter($helper->getQueryBuilder(), true)); |
||
| 74 | $pager->setMaxPerPage($operation->getItemPerPage()); |
||
| 75 | $pager->setCurrentPage($context['page'] ?? 1); |
||
| 76 | |||
| 77 | return $pager; |
||
| 78 | } |
||
| 79 | $queryBuilder = $repository->createQueryBuilder('entity'); |
||
| 80 | |||
| 81 | foreach ($operation->getIdentifiers() as $identifier) { |
||
| 82 | if ($uriVariables[$identifier] ?? false) { |
||
| 83 | $queryBuilder->andWhere(sprintf('entity.%s = %s', $identifier, $uriVariables[$identifier])); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return $queryBuilder |
||
| 88 | ->getQuery() |
||
| 89 | ->getOneOrNullResult() |
||
| 90 | ; |
||
| 93 |