Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

ORMDataProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 97.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 122
ccs 44
cts 45
cp 0.9778
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get() 0 13 2
A getCollection() 0 32 3
A getRepository() 0 3 1
A save() 0 4 1
A create() 0 5 1
A delete() 0 7 2
1
<?php
2
3
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\Persistence\ObjectRepository;
8
use LAG\AdminBundle\Admin\AdminInterface;
9
use LAG\AdminBundle\Bridge\Doctrine\ORM\Event\ORMFilterEvent;
10
use LAG\AdminBundle\Bridge\Doctrine\ORM\Results\ResultsHandlerInterface;
11
use LAG\AdminBundle\DataProvider\DataProviderInterface;
12
use LAG\AdminBundle\Event\Events;
13
use LAG\AdminBundle\Exception\Exception;
14
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
15
16
class ORMDataProvider implements DataProviderInterface
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $entityManager;
22
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $eventDispatcher;
27
28
    /**
29
     * @var ResultsHandlerInterface
30
     */
31
    private $resultsHandler;
32
33
    /**
34
     * DoctrineORMDataProvider constructor.
35
     */
36 16
    public function __construct(
37
        EntityManagerInterface $entityManager,
38
        EventDispatcherInterface $eventDispatcher,
39
        ResultsHandlerInterface $handler
40
    ) {
41 16
        $this->entityManager = $entityManager;
42 16
        $this->eventDispatcher = $eventDispatcher;
43 16
        $this->resultsHandler = $handler;
44 16
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 4
    public function getCollection(AdminInterface $admin, array $filters = [])
50
    {
51 4
        $adminConfiguration = $admin->getConfiguration();
52 4
        $actionConfiguration = $admin->getAction()->getConfiguration();
53 4
        $repository = $this->getRepository($adminConfiguration->get('entity'));
54
55
        // Allow to change the default method in configuration
56 4
        $alias = strtolower($admin->getName());
57 4
        $method = $actionConfiguration->get('repository_method');
58 4
        $pagination = ('pagerfanta' === $actionConfiguration->get('pager'));
59
60
        // The repository could return an object, an array, a collection, a pager or a query builder. The results
61
        // handler will act according to result type
62 4
        if ($method) {
63 2
            $data = $repository->$method($alias);
64
        } else {
65 2
            if (!$repository instanceof EntityRepository) {
66
                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));
67
            }
68 2
            $data = $repository->createQueryBuilder($alias);
69
        }
70
71
        // Dispatch an event to allow filter alteration on the query builder
72 4
        $event = new ORMFilterEvent($data, $admin, $filters);
73 4
        $this->eventDispatcher->dispatch($event, Events::DOCTRINE_ORM_FILTER);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with LAG\AdminBundle\Event\Events::DOCTRINE_ORM_FILTER. ( Ignorable by Annotation )

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

73
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
74
                                dispatch($event, Events::DOCTRINE_ORM_FILTER);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
75
        // Fetch pagination parameters
76 4
        $pageParameter = $actionConfiguration->get('page_parameter');
77 4
        $page = (int) $admin->getRequest()->get($pageParameter, 1);
78 4
        $maxPerPage = $actionConfiguration->get('max_per_page');
79
80 4
        return $this->resultsHandler->handle($data, $pagination, $page, $maxPerPage);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 4
    public function get(AdminInterface $admin, string $identifier)
87
    {
88 4
        $class = $admin->getConfiguration()->get('entity');
89
        $item = $this
90 4
            ->getRepository($class)
91 4
            ->find($identifier)
92
        ;
93
94 4
        if (null === $item) {
95 2
            throw new Exception(sprintf('Item of class "%s" with identifier "%s" not found.', $class, $identifier));
96
        }
97
98 2
        return $item;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 2
    public function save(AdminInterface $admin): void
105
    {
106 2
        $this->entityManager->persist($admin->getEntities()->first());
107 2
        $this->entityManager->flush();
108 2
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function create(AdminInterface $admin)
114
    {
115 2
        $class = $admin->getConfiguration()->get('entity');
116
117 2
        return new $class();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 4
    public function delete(AdminInterface $admin): void
124
    {
125 4
        if ($admin->getEntities()->isEmpty()) {
126 2
            throw new Exception('The admin "'.$admin->getName().'" has no loaded entity');
127
        }
128 2
        $this->entityManager->remove($admin->getEntities()->first());
129 2
        $this->entityManager->flush();
130 2
    }
131
132
    /**
133
     * @return ObjectRepository|EntityRepository
134
     */
135 8
    private function getRepository(string $class)
136
    {
137 8
        return $this->entityManager->getRepository($class);
138
    }
139
}
140