Completed
Push — 1.x ( 8bcf66...54c417 )
by Marko
03:54
created

AnnotationAdmin::configureRoutes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Admin;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\AddRoute;
8
use Sonata\AdminBundle\Admin\AbstractAdmin;
9
use Sonata\AdminBundle\Datagrid\DatagridMapper;
10
use Sonata\AdminBundle\Datagrid\ListMapper;
11
use Sonata\AdminBundle\Form\FormMapper;
12
use Sonata\AdminBundle\Route\RouteCollection;
13
use Sonata\AdminBundle\Show\ShowMapper;
14
15
/**
16
 * @author Marko Kunic <[email protected]>
17
 */
18
class AnnotationAdmin extends AbstractAdmin
19
{
20
    private $parentAssociationMappingLoaded = false;
21
    private $datagridValuesLoaded = false;
22
23
    protected function configureFormFields(FormMapper $formMapper): void
24
    {
25
        if ($this->request->get($this->getIdParameter())) {
26
            $this->get('sonata.annotation.reader.form')
27
                ->configureEditFields($this->getReflectionClass(), $formMapper);
28
            return;
29
        }
30
31
        $this->get('sonata.annotation.reader.form')
32
            ->configureCreateFields($this->getReflectionClass(), $formMapper);
33
    }
34
35
    protected function configureListFields(ListMapper $listMapper): void
36
    {
37
        $this->get('sonata.annotation.reader.list')
38
            ->configureFields($this->getReflectionClass(), $listMapper);
39
    }
40
41
    protected function configureShowFields(ShowMapper $showMapper): void
42
    {
43
        $this->get('sonata.annotation.reader.show')
44
            ->configureFields($this->getReflectionClass(), $showMapper);
45
    }
46
47
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
48
    {
49
        $this->get('sonata.annotation.reader.datagrid')
50
            ->configureFields($this->getReflectionClass(), $datagridMapper);
51
    }
52
53
    protected function configureRoutes(RouteCollection $collection): void
54
    {
55
        [$addRoutes, $removeRoutes] = $this->get('sonata.annotation.reader.route')
56
            ->getRoutes($this->getReflectionClass());
57
58
        foreach ($addRoutes as $route) {
59
            $collection->add(
60
                $route->name,
61
                $route->path ? $this->replaceIdParameterInRoutePath($route->path) : $route->name
62
            );
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('sonata.annotation.reader.action_button')
78
            ->getActions(
79
                $this->getReflectionClass(),
80
                parent::configureActionButtons($action, $object)
81
            );
82
    }
83
84
    public function getDashboardActions(): array
85
    {
86
        return $this->get('sonata.annotation.reader.dashboard_action')
87
            ->getActions(
88
                $this->getReflectionClass(),
89
                parent::getDashboardActions()
90
            );
91
    }
92
93
    public function getExportFields(): array
94
    {
95
        return $this->get('sonata.annotation.reader.export')
96
            ->getFields($this->getReflectionClass()) ?: parent::getExportFields();
97
    }
98
99
    public function getExportFormats(): array
100
    {
101
        return $this->get('sonata.annotation.reader.export')
102
            ->getFormats($this->getReflectionClass()) ?: parent::getExportFormats();
103
    }
104
105
    public function buildDatagrid()
106
    {
107
        if (!$this->datagridValuesLoaded) {
108
            $this->datagridValues = $this->get('sonata.annotation.reader.datagrid_values')
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('sonata.annotation.reader.parent_association_mapping')
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