Passed
Push — master ( a105d0...2cbf75 )
by Marko
02:48
created

AnnotationAdmin::getParentAssociationMapping()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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