Completed
Push — master ( 1fe708...6360f1 )
by Arnaud
16s queued 12s
created

ORMDataProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 36
c 1
b 0
f 0
dl 0
loc 86
ccs 38
cts 38
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 2
A create() 0 3 1
A getRepository() 0 10 2
A getAdmin() 0 3 1
A getCollection() 0 37 4
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Bridge\Doctrine;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\EntityRepository;
9
use Doctrine\ORM\QueryBuilder;
10
use Doctrine\Persistence\ObjectRepository;
11
use LAG\AdminBundle\Admin\AdminInterface;
12
use LAG\AdminBundle\Admin\Helper\AdminHelperInterface;
13
use LAG\AdminBundle\Bridge\Doctrine\DataSource\ORMDataSource;
14
use LAG\AdminBundle\DataProvider\DataProviderInterface;
15
use LAG\AdminBundle\DataProvider\DataSourceInterface;
16
use LAG\AdminBundle\Exception\Exception;
17
18
class ORMDataProvider implements DataProviderInterface
19
{
20
    private EntityManagerInterface $entityManager;
21
    private AdminHelperInterface $adminHelper;
22
23 15
    public function __construct(
24
        EntityManagerInterface $entityManager,
25
        AdminHelperInterface $adminHelper
26
    ) {
27 15
        $this->entityManager = $entityManager;
28 15
        $this->adminHelper = $adminHelper;
29 15
    }
30
31 12
    public function getCollection(
32
        string $class,
33
        array $criteria = [],
34
        array $orderBy = [],
35
        int $limit = 1,
36
        int $offset = 25
37
    ): DataSourceInterface {
38 12
        $admin = $this->getAdmin();
39 12
        $adminConfiguration = $admin->getConfiguration();
40 12
        $actionConfiguration = $admin->getAction()->getConfiguration();
41 12
        $repository = $this->getRepository($adminConfiguration->getEntityClass());
42
43
        // Allow to change the default method in configuration
44 9
        $method = $actionConfiguration->getRepositoryMethod();
45 9
        $isPaginated = ('pagerfanta' === $actionConfiguration->getPager());
46
47
        // Fetch pagination parameters
48 9
        $pageParameter = $actionConfiguration->getPageParameter();
49 9
        $page = (int) $admin->getRequest()->get($pageParameter, 1);
50 9
        $maxPerPage = $actionConfiguration->getMaxPerPage();
51
52
        // The repository could return an object, an array, a collection, a pager or a query builder. The results
53
        // handler will act according to result type
54 9
        if ($method) {
55 6
            if (!method_exists($repository, $method)) {
56 3
                throw new Exception(sprintf('The method "%s" does not exists for the class "%s"', $method, \get_class($repository)));
57
            }
58 3
            $data = $repository->$method($criteria, $orderBy, $limit, $offset);
59
60 3
            if (!$data instanceof QueryBuilder) {
61 3
                throw new Exception(sprintf('The method "%s" of the repository "%s" should return a instance of "%s"', $method, \get_class($repository), QueryBuilder::class));
62
            }
63
        } else {
64 3
            $data = $repository->createQueryBuilder('entity');
65
        }
66
67 6
        return new ORMDataSource($data, $isPaginated, $page, $maxPerPage);
68
    }
69
70 2
    public function get(string $class, $identifier): object
71
    {
72
        $item = $this
73 2
            ->getRepository($class)
74 2
            ->find($identifier)
75
        ;
76
77 2
        if ($item === null) {
78 1
            throw new Exception(sprintf('Item of class "%s" with identifier "%s" not found.', $class, $identifier));
79
        }
80
81 1
        return $item;
82
    }
83
84 1
    public function create(string $class): object
85
    {
86 1
        return new $class();
87
    }
88
89 14
    private function getRepository(string $class): ObjectRepository
90
    {
91 14
        $repository = $this->entityManager->getRepository($class);
92
93 14
        if (!$repository instanceof EntityRepository) {
94 3
            $admin = $this->adminHelper->getAdmin();
95 3
            throw new Exception(sprintf('The repository of admin "%s" should be an instance of "%s" to use the default method createQueryBuilder()', $admin->getName(), EntityRepository::class));
96
        }
97
98 11
        return $repository;
99
    }
100
101 12
    private function getAdmin(): AdminInterface
102
    {
103 12
        return $this->adminHelper->getAdmin();
104
    }
105
}
106