ORMDataProvider::provide()   B
last analyzed

Complexity

Conditions 11
Paths 17

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 11
eloc 39
c 2
b 0
f 2
nc 17
nop 3
dl 0
loc 65
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\State;
6
7
use Doctrine\Bundle\DoctrineBundle\Registry;
8
use Doctrine\ORM\EntityRepository;
9
use LAG\AdminBundle\Bridge\Doctrine\ORM\Exception\EntityManagerNotFoundException;
10
use LAG\AdminBundle\Bridge\Doctrine\ORM\QueryBuilder\QueryBuilderHelper;
11
use LAG\AdminBundle\Metadata\CollectionOperationInterface;
12
use LAG\AdminBundle\Metadata\Create;
13
use LAG\AdminBundle\Metadata\OperationInterface;
14
use LAG\AdminBundle\State\DataProviderInterface;
15
use Pagerfanta\Doctrine\ORM\QueryAdapter;
16
use Pagerfanta\Pagerfanta;
17
18
class ORMDataProvider implements DataProviderInterface
19
{
20
    public function __construct(
21
        private Registry $registry,
22
    ) {
23
    }
24
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());
0 ignored issues
show
Bug introduced by
It seems like $operation->getResource()->getDataClass() can also be of type null; however, parameter $class of Doctrine\Persistence\Abs...y::getManagerForClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $manager = $this->registry->getManagerForClass(/** @scrutinizer ignore-type */ $operation->getResource()->getDataClass());
Loading history...
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';
0 ignored issues
show
Bug introduced by
The method getResourceName() does not exist on LAG\AdminBundle\Metadata\OperationInterface. Did you maybe mean getResource()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $rootAlias = $operation->/** @scrutinizer ignore-call */ getResourceName().'_entity';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
        ;
91
    }
92
}
93