AdminResource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 12
dl 0
loc 22
rs 10

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 LAG\AdminBundle\Metadata;
6
7
use Attribute;
8
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\ORMDataProcessor;
9
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\ORMDataProvider;
10
use LAG\AdminBundle\Exception\Operation\OperationMissingException;
11
12
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
13
class AdminResource
14
{
15
    private OperationInterface $currentOperation;
16
17
    public function __construct(
18
        private ?string $name = null,
19
        private ?string $dataClass = null,
20
        private ?string $title = null,
21
        private ?string $group = null,
22
        private ?string $icon = null,
23
        /** @var OperationInterface[] $operations */
24
        private array $operations = [
25
            new Index(),
26
            new Create(),
27
            new Update(),
28
            new Delete(),
29
            new Show(),
30
        ],
31
        private string $processor = ORMDataProcessor::class,
32
        private string $provider = ORMDataProvider::class,
33
        /** @var string[] $identifiers */
34
        private array $identifiers = ['id'],
35
        private string $routePattern = 'lag_admin.{resource}.{operation}',
36
        private ?string $routePrefix = '/{resourceName}',
37
        private ?string $translationPattern = null,
38
    ) {
39
    }
40
41
    public function getName(): ?string
42
    {
43
        return $this->name;
44
    }
45
46
    public function withName(?string $name): self
47
    {
48
        $self = clone $this;
49
        $self->name = $name;
50
51
        return $self;
52
    }
53
54
    public function getDataClass(): ?string
55
    {
56
        return $this->dataClass;
57
    }
58
59
    public function withDataClass(?string $dataClass): self
60
    {
61
        $self = clone $this;
62
        $self->dataClass = $dataClass;
63
64
        return $self;
65
    }
66
67
    public function getTitle(): ?string
68
    {
69
        return $this->title;
70
    }
71
72
    public function withTitle(string $title): self
73
    {
74
        $self = clone $this;
75
        $self->title = $title;
76
77
        return $self;
78
    }
79
80
    public function getGroup(): ?string
81
    {
82
        return $this->group;
83
    }
84
85
    public function withGroup(string $group): self
86
    {
87
        $self = clone $this;
88
        $self->group = $group;
89
90
        return $self;
91
    }
92
93
    public function getIcon(): ?string
94
    {
95
        return $this->icon;
96
    }
97
98
    public function withIcon(string $icon): self
99
    {
100
        $self = clone $this;
101
        $self->icon = $icon;
102
103
        return $self;
104
    }
105
106
    /**
107
     * @return OperationInterface[]
108
     */
109
    public function getOperations(): array
110
    {
111
        return $this->operations;
112
    }
113
114
    public function hasOperation(string $operationName): bool
115
    {
116
        foreach ($this->operations as $operation) {
117
            if ($operation->getName() === $operationName) {
118
                return true;
119
            }
120
        }
121
122
        return false;
123
    }
124
125
    public function getOperation(string $operationName): OperationInterface
126
    {
127
        foreach ($this->operations as $operation) {
128
            if ($operation->getName() === $operationName) {
129
                return $operation;
130
            }
131
        }
132
133
        throw new OperationMissingException(sprintf('The operation with name "%s" does not exists in the resource "%s"', $operationName, $this->getName()));
134
    }
135
136
    public function withOperations(array $operations): self
137
    {
138
        $self = clone $this;
139
        $self->operations = $operations;
140
141
        return $self;
142
    }
143
144
    public function getProcessor(): string
145
    {
146
        return $this->processor;
147
    }
148
149
    public function withProcessor(string $processor): self
150
    {
151
        $self = clone $this;
152
        $self->processor = $processor;
153
154
        return $self;
155
    }
156
157
    public function getProvider(): string
158
    {
159
        return $this->provider;
160
    }
161
162
    public function withProvider(string $provider): self
163
    {
164
        $self = clone $this;
165
        $self->provider = $provider;
166
167
        return $self;
168
    }
169
170
    public function getRoutePattern(): string
171
    {
172
        return $this->routePattern;
173
    }
174
175
    public function withRoutePattern(string $routePattern): self
176
    {
177
        $self = clone $this;
178
        $self->routePattern = $routePattern;
179
180
        return $self;
181
    }
182
183
    public function getIdentifiers(): array
184
    {
185
        return $this->identifiers;
186
    }
187
188
    public function withIdentifiers(array $identifiers): self
189
    {
190
        $self = clone $this;
191
        $self->identifiers = $identifiers;
192
193
        return $self;
194
    }
195
196
    public function getCurrentOperation(): OperationInterface
197
    {
198
        return $this->currentOperation;
199
    }
200
201
    public function withCurrentOperation(OperationInterface $currentOperation): self
202
    {
203
        $self = clone $this;
204
        $self->currentOperation = $currentOperation;
205
206
        return $self;
207
    }
208
209
    public function getRoutePrefix(): ?string
210
    {
211
        return $this->routePrefix;
212
    }
213
214
    public function withRoutePrefix(?string $prefix): self
215
    {
216
        $self = clone $this;
217
        $self->routePrefix = $prefix;
218
219
        return $self;
220
    }
221
222
    public function getTranslationPattern(): ?string
223
    {
224
        return $this->translationPattern;
225
    }
226
227
    public function withTranslationPattern(?string $translationPattern): self
228
    {
229
        $self = clone $this;
230
        $self->translationPattern = $translationPattern;
231
232
        return $self;
233
    }
234
}
235