Passed
Push — master ( 26f9dc...b66765 )
by Marko
02:57
created

Admin::handleRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Admin;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\AddRoute;
8
use KunicMarko\SonataAnnotationBundle\Reader\ActionButtonReader;
9
use KunicMarko\SonataAnnotationBundle\Reader\DashboardActionReader;
10
use KunicMarko\SonataAnnotationBundle\Reader\DatagridReader;
11
use KunicMarko\SonataAnnotationBundle\Reader\DatagridValuesReader;
12
use KunicMarko\SonataAnnotationBundle\Reader\ExportReader;
13
use KunicMarko\SonataAnnotationBundle\Reader\FormReader;
14
use KunicMarko\SonataAnnotationBundle\Reader\ListReader;
15
use KunicMarko\SonataAnnotationBundle\Reader\ParentAssociationMappingReader;
16
use KunicMarko\SonataAnnotationBundle\Reader\RouteReader;
17
use KunicMarko\SonataAnnotationBundle\Reader\ShowReader;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Datagrid\DatagridMapper;
20
use Sonata\AdminBundle\Datagrid\ListMapper;
21
use Sonata\AdminBundle\Form\FormMapper;
22
use Sonata\AdminBundle\Route\RouteCollection;
23
use Sonata\AdminBundle\Show\ShowMapper;
24
25
/**
26
 * @author Marko Kunic <[email protected]>
27
 */
28
class Admin extends AbstractAdmin
29
{
30
    private $parentAssociationMappingLoaded = false;
31
    private $datagridValuesLoaded = false;
32
33
    protected function configureFormFields(FormMapper $formMapper): void
34
    {
35
        $this->get(FormReader::class)
36
            ->configureFields($this->getReflectionClass(), $formMapper);
37
    }
38
39
    protected function configureListFields(ListMapper $listMapper): void
40
    {
41
        $this->get(ListReader::class)
42
            ->configureFields($this->getReflectionClass(), $listMapper);
43
    }
44
45
    protected function configureShowFields(ShowMapper $showMapper): void
46
    {
47
        $this->get(ShowReader::class)
48
            ->configureFields($this->getReflectionClass(), $showMapper);
49
    }
50
51
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
52
    {
53
        $this->get(DatagridReader::class)
54
            ->configureFields($this->getReflectionClass(), $datagridMapper);
55
    }
56
57
    protected function configureRoutes(RouteCollection $collection): void
58
    {
59
        [$addRoutes, $removeRoutes] = $this->get(RouteReader::class)->getRoutes($this->getReflectionClass());
60
61
        foreach ($addRoutes as $route) {
62
            $collection->add($route->name, $this->replaceIdParameterInRoutePath($route->path));
63
        }
64
65
        foreach ($removeRoutes as $route) {
66
            $collection->remove($route->name);
67
        }
68
    }
69
70
    private function replaceIdParameterInRoutePath(string $path): string
71
    {
72
        return str_replace(AddRoute::ID_PARAMETER, $this->getRouterIdParameter(), $path);
73
    }
74
75
    public function configureActionButtons($action, $object = null): array
76
    {
77
        return $this->get(ActionButtonReader::class)
78
            ->getActions(
79
                $this->getReflectionClass(),
80
                parent::configureActionButtons($action, $object)
81
            );
82
    }
83
84
    public function getDashboardActions(): array
85
    {
86
        return $this->get(DashboardActionReader::class)
87
            ->getActions(
88
                $this->getReflectionClass(),
89
                parent::getDashboardActions()
90
            );
91
    }
92
93
    public function getExportFields(): array
94
    {
95
        return $this->get(ExportReader::class)
96
            ->getFields($this->getReflectionClass()) ?: parent::getExportFields();
97
    }
98
99
    public function getExportFormats(): array
100
    {
101
        return $this->get(ExportReader::class)
102
            ->getFormats($this->getReflectionClass()) ?: parent::getExportFormats();
103
    }
104
105
    public function buildDatagrid()
106
    {
107
        if (!$this->datagridValuesLoaded) {
108
            $this->datagridValues = $this->get(DatagridValuesReader::class)
109
                ->getDatagridValues($this->getReflectionClass()) ?: $this->datagridValues;
110
111
            $this->datagridValuesLoaded = true;
112
        }
113
114
        parent::buildDatagrid();
115
    }
116
117
    public function getParentAssociationMapping()
118
    {
119
        if (!$this->parentAssociationMappingLoaded) {
120
            $this->parentAssociationMapping = $this->get(ParentAssociationMappingReader::class)
121
                ->getParent($this->getReflectionClass()) ?: $this->parentAssociationMapping;
122
123
            $this->parentAssociationMappingLoaded = true;
124
        }
125
126
        return $this->parentAssociationMapping;
127
    }
128
129
    private function get(string $service)
130
    {
131
        return $this->getConfigurationPool()->getContainer()->get($service);
132
    }
133
134
    private function getReflectionClass(): \ReflectionClass
135
    {
136
        return new \ReflectionClass($this->getClass());
137
    }
138
}
139