Completed
Push — master ( d62d29...27939e )
by Marko
02:28
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\Route;
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
        foreach ($this->get(RouteReader::class)->getRoutes($this->getReflectionClass()) as $route) {
60
            $this->handleRoute($collection, $route);
61
        }
62
    }
63
64
    private function handleRoute(RouteCollection $collection, Route $route): void
65
    {
66
        if ($route->shouldAddRoute()) {
67
            $collection->add($route->name, $this->replaceIdParameterInRoutePath($route->path));
68
            return;
69
        }
70
71
        $collection->remove($route->name);
72
    }
73
74
    private function replaceIdParameterInRoutePath(string $path): string
75
    {
76
        return str_replace(Route::ID_PARAMETER, $this->getRouterIdParameter(), $path);
77
    }
78
79
    public function configureActionButtons($action, $object = null): array
80
    {
81
        return $this->get(ActionButtonReader::class)
82
            ->getActions(
83
                $this->getReflectionClass(),
84
                parent::configureActionButtons($action, $object)
85
            );
86
    }
87
88
    public function getDashboardActions(): array
89
    {
90
        return $this->get(DashboardActionReader::class)
91
            ->getActions(
92
                $this->getReflectionClass(),
93
                parent::getDashboardActions()
94
            );
95
    }
96
97
    public function getExportFields(): array
98
    {
99
        return $this->get(ExportReader::class)
100
            ->getFields($this->getReflectionClass()) ?: parent::getExportFields();
101
    }
102
103
    public function getExportFormats(): array
104
    {
105
        return $this->get(ExportReader::class)
106
            ->getFormats($this->getReflectionClass()) ?: parent::getExportFormats();
107
    }
108
109
    public function buildDatagrid()
110
    {
111
        if (!$this->datagridValuesLoaded) {
112
            $this->datagridValues = $this->get(DatagridValuesReader::class)
113
                ->getDatagridValues($this->getReflectionClass()) ?: $this->datagridValues;
114
115
            $this->datagridValuesLoaded = true;
116
        }
117
118
        parent::buildDatagrid();
119
    }
120
121
    public function getParentAssociationMapping()
122
    {
123
        if (!$this->parentAssociationMappingLoaded) {
124
            $this->parentAssociationMapping = $this->get(ParentAssociationMappingReader::class)
125
                ->getParent($this->getReflectionClass()) ?: $this->parentAssociationMapping;
126
127
            $this->parentAssociationMappingLoaded = true;
128
        }
129
130
        return $this->parentAssociationMapping;
131
    }
132
133
    private function get(string $service)
134
    {
135
        return $this->getConfigurationPool()->getContainer()->get($service);
136
    }
137
138
    private function getReflectionClass(): \ReflectionClass
139
    {
140
        return new \ReflectionClass($this->getClass());
141
    }
142
}
143