Passed
Pull Request — master (#41)
by Mathieu
03:42
created

AnnotationAdmin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 22
rs 9.9666
nc 1
nop 11
cc 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 KunicMarko\SonataAnnotationBundle\Reader\ActionButtonReader;
10
use KunicMarko\SonataAnnotationBundle\Reader\DashboardActionReader;
11
use KunicMarko\SonataAnnotationBundle\Reader\DatagridReader;
12
use KunicMarko\SonataAnnotationBundle\Reader\ExportReader;
13
use KunicMarko\SonataAnnotationBundle\Reader\FormReader;
14
use KunicMarko\SonataAnnotationBundle\Reader\ListReader;
15
use KunicMarko\SonataAnnotationBundle\Reader\RouteReader;
16
use KunicMarko\SonataAnnotationBundle\Reader\ShowReader;
17
use Sonata\AdminBundle\Admin\AbstractAdmin;
18
use Sonata\AdminBundle\Datagrid\DatagridMapper;
19
use Sonata\AdminBundle\Datagrid\ListMapper;
20
use Sonata\AdminBundle\Form\FormMapper;
21
use Sonata\AdminBundle\Route\RouteCollectionInterface;
22
use Sonata\AdminBundle\Show\ShowMapper;
23
24
/**
25
 * @author Marko Kunic <[email protected]>
26
 */
27
class AnnotationAdmin extends AbstractAdmin
28
{
29
    /**
30
     * @var FormReader
31
     */
32
    private $formReader;
33
34
    /**
35
     * @var ListReader
36
     */
37
    private $listReader;
38
39
    /**
40
     * @var ShowReader
41
     */
42
    private $showReader;
43
44
    /**
45
     * @var DatagridReader
46
     */
47
    private $datagridReader;
48
49
    /**
50
     * @var RouteReader
51
     */
52
    private $routeReader;
53
54
    /**
55
     * @var ActionButtonReader
56
     */
57
    private $actionButtonReader;
58
59
    /**
60
     * @var DashboardActionReader
61
     */
62
    private $dashboardActionReader;
63
64
    /**
65
     * @var ExportReader
66
     */
67
    private $exportReader;
68
69
    public function __construct(
70
        string $code,
71
        string $class,
72
        ?string $baseControllerName,
73
        FormReader $formReader,
74
        ListReader $listReader,
75
        ShowReader $showReader,
76
        DatagridReader $datagridReader,
77
        RouteReader $routeReader,
78
        ActionButtonReader $actionButtonReader,
79
        DashboardActionReader $dashboardActionReader,
80
        ExportReader $exportReader
81
    ) {
82
        parent::__construct($code, $class, $baseControllerName);
83
        $this->formReader = $formReader;
84
        $this->listReader = $listReader;
85
        $this->showReader = $showReader;
86
        $this->datagridReader = $datagridReader;
87
        $this->routeReader = $routeReader;
88
        $this->actionButtonReader = $actionButtonReader;
89
        $this->dashboardActionReader = $dashboardActionReader;
90
        $this->exportReader = $exportReader;
91
    }
92
93
    protected function configureFormFields(FormMapper $formMapper): void
94
    {
95
        if ($this->request->get($this->getIdParameter())) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

95
        if ($this->request->/** @scrutinizer ignore-call */ get($this->getIdParameter())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
            $this->formReader->configureEditFields($this->getReflectionClass(), $formMapper);
97
            return;
98
        }
99
100
        $this->formReader->configureCreateFields($this->getReflectionClass(), $formMapper);
101
    }
102
103
    protected function configureListFields(ListMapper $listMapper): void
104
    {
105
        $this->listReader->configureFields($this->getReflectionClass(), $listMapper);
106
    }
107
108
    protected function configureShowFields(ShowMapper $showMapper): void
109
    {
110
        $this->showReader->configureFields($this->getReflectionClass(), $showMapper);
111
    }
112
113
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
114
    {
115
        $this->datagridReader->configureFields($this->getReflectionClass(), $datagridMapper);
116
    }
117
118
    protected function configureRoutes(RouteCollectionInterface $collection): void
119
    {
120
        [$addRoutes, $removeRoutes] = $this->routeReader->getRoutes($this->getReflectionClass());
121
122
        /** @var AddRoute $route */
123
        foreach ($addRoutes as $route) {
124
            $collection->add(
125
                $route->getName(),
126
                $route->path ? $this->replaceIdParameterInRoutePath($route->path) : $route->getName()
127
            );
128
        }
129
130
        /** @var RemoveRoute $route */
131
        foreach ($removeRoutes as $route) {
132
            $collection->remove($route->getName());
133
        }
134
    }
135
136
    private function replaceIdParameterInRoutePath(string $path): string
137
    {
138
        return str_replace(AddRoute::ID_PARAMETER, $this->getRouterIdParameter(), $path);
139
    }
140
141
    public function configureActionButtons(array $buttonList, $action, ?object $object = null): array
142
    {
143
        return $this->actionButtonReader->getActions(
144
            $this->getReflectionClass(),
145
            parent::configureActionButtons($buttonList, $action, $object)
146
        );
147
    }
148
149
    public function getDashboardActions(): array
150
    {
151
        return $this->dashboardActionReader->getActions(
152
            $this->getReflectionClass(),
153
            parent::getDashboardActions()
154
        );
155
    }
156
157
    public function getExportFormats(): array
158
    {
159
        return $this->exportReader->getFormats($this->getReflectionClass()) ?: parent::getExportFormats();
160
    }
161
162
    private function getReflectionClass(): \ReflectionClass
163
    {
164
        return new \ReflectionClass($this->getClass());
165
    }
166
}
167