ConfigProvider::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Db;
5
6
use Psr\Log\LoggerInterface;
7
use SlayerBirden\DataFlowServer\Authentication\Middleware\TokenMiddleware;
8
use SlayerBirden\DataFlowServer\Db\Controller\AddConfigAction;
9
use SlayerBirden\DataFlowServer\Db\Controller\DeleteConfigAction;
10
use SlayerBirden\DataFlowServer\Db\Controller\GetConfigAction;
11
use SlayerBirden\DataFlowServer\Db\Controller\GetConfigsAction;
12
use SlayerBirden\DataFlowServer\Db\Controller\UpdateConfigAction;
13
use SlayerBirden\DataFlowServer\Db\Doctrine\Subscriber\Validation;
14
use SlayerBirden\DataFlowServer\Db\Factory\DbConfigHydratorFactory;
15
use SlayerBirden\DataFlowServer\Db\Factory\DbConfigResourceMiddlewareFactory;
16
use SlayerBirden\DataFlowServer\Db\Factory\DbConfigurationRepositoryFactory;
17
use SlayerBirden\DataFlowServer\Db\Factory\InputFilterMiddlewareFactory;
18
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
19
use SlayerBirden\DataFlowServer\Domain\Middleware\SetOwnerFilterMiddleware;
20
use SlayerBirden\DataFlowServer\Domain\Middleware\SetOwnerMiddleware;
21
use SlayerBirden\DataFlowServer\Domain\Middleware\ValidateOwnerMiddleware;
22
use SlayerBirden\DataFlowServer\Zend\InputFilter\ProxyFilterManagerFactory;
23
use Zend\Expressive\Helper\BodyParams\BodyParamsMiddleware;
24
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
25
use Zend\ServiceManager\Factory\InvokableFactory;
26
27
final class ConfigProvider
28
{
29
    public function __invoke(): array
30
    {
31
        return [
32
            ConfigAbstractFactory::class => $this->getAbstractFactoryConfig(),
33
            'dependencies' => $this->getDependenciesConfig(),
34
            'doctrine' => $this->getDoctrineConfig(),
35
            'input_filter_specs' => [
36
                'ConfigInputFilter' => $this->getConfigInputFilterSpec(),
37
            ],
38
            'routes' => $this->getRoutesConfig(),
39
        ];
40
    }
41
42
    private function getConfigInputFilterSpec(): array
43
    {
44
        return [
45
            'title' => [
46
                'required' => false,
47
                'continue_if_empty' => true,
48
                'filters' => [
49
                    [
50
                        'name' => 'stringtrim',
51
                    ],
52
                ],
53
                'validators' => [
54
                    [
55
                        'name' => 'notempty',
56
                    ],
57
                ],
58
            ],
59
            'dbname' => [
60
                'required' => false,
61
                'continue_if_empty' => true,
62
                'filters' => [
63
                    [
64
                        'name' => 'stringtrim',
65
                    ],
66
                ],
67
                'validators' => [
68
                    [
69
                        'name' => 'notempty',
70
                    ],
71
                ],
72
            ],
73
            'user' => [
74
                'required' => false,
75
                'continue_if_empty' => true,
76
                'filters' => [
77
                    [
78
                        'name' => 'stringtrim',
79
                    ],
80
                ],
81
                'validators' => [
82
                    [
83
                        'name' => 'notempty',
84
                    ],
85
                ],
86
            ],
87
            'password' => [
88
                'required' => false,
89
                'filters' => [
90
                    [
91
                        'name' => 'stringtrim',
92
                    ],
93
                ],
94
            ],
95
            'host' => [
96
                'required' => false,
97
                'continue_if_empty' => true,
98
                'filters' => [
99
                    [
100
                        'name' => 'stringtrim',
101
                    ],
102
                ],
103
                'validators' => [
104
                    [
105
                        'name' => 'notempty',
106
                    ],
107
                ],
108
            ],
109
            'driver' => [
110
                'required' => false,
111
                'continue_if_empty' => true,
112
                'filters' => [
113
                    [
114
                        'name' => 'stringtrim',
115
                    ],
116
                ],
117
                'validators' => [
118
                    [
119
                        'name' => 'inArray',
120
                        'options' => [
121
                            'haystack' => [
122
                                'mysql',
123
                                'pdo_mysql',
124
                                'pdo_sqlite',
125
                                'drizzle_pdo_mysql',
126
                                'pdo_pgsql'
127
                            ],
128
                            "strict" => \Zend\Validator\InArray::COMPARE_STRICT,
129
                            "messages" => [
130
                                \Zend\Validator\InArray::NOT_IN_ARRAY => 'Please use valid DBAL driver',
131
                            ],
132
                        ],
133
                    ],
134
                ],
135
            ],
136
            'port' => [
137
                'required' => false,
138
                'continue_if_empty' => true,
139
                'filters' => [
140
                    [
141
                        'name' => 'stringtrim',
142
                    ],
143
                ],
144
                'validators' => [
145
                    [
146
                        'name' => 'digits',
147
                    ],
148
                ],
149
            ],
150
            'url' => [
151
                'required' => false,
152
                'filters' => [
153
                    [
154
                        'name' => 'stringtrim',
155
                    ],
156
                ],
157
            ],
158
        ];
159
    }
160
161
    private function getAbstractFactoryConfig(): array
162
    {
163
        return [
164
            AddConfigAction::class => [
165
                EntityManagerRegistry::class,
166
                'DbConfigHydrator',
167
                LoggerInterface::class,
168
            ],
169
            UpdateConfigAction::class => [
170
                EntityManagerRegistry::class,
171
                'DbConfigHydrator',
172
                LoggerInterface::class,
173
            ],
174
            GetConfigsAction::class => [
175
                'DbConfigurationRepository',
176
                LoggerInterface::class,
177
                'DbConfigHydrator',
178
            ],
179
            GetConfigAction::class => [
180
                'DbConfigHydrator',
181
            ],
182
            DeleteConfigAction::class => [
183
                EntityManagerRegistry::class,
184
                LoggerInterface::class,
185
                'DbConfigHydrator',
186
            ],
187
        ];
188
    }
189
190
    public function getDependenciesConfig(): array
191
    {
192
        return [
193
            'factories' => [
194
                'DbConfigHydrator' => DbConfigHydratorFactory::class,
195
                'ConfigInputFilter' => ProxyFilterManagerFactory::class,
196
                'DbConfigResourceMiddleware' => DbConfigResourceMiddlewareFactory::class,
197
                'DbConfigurationRepository' => DbConfigurationRepositoryFactory::class,
198
                Validation::class => InvokableFactory::class,
199
                'ConfigInputFilterMiddleware' => InputFilterMiddlewareFactory::class,
200
            ],
201
        ];
202
    }
203
204
    public function getDoctrineConfig(): array
205
    {
206
        return [
207
            'entity_managers' => [
208
                'default' => [
209
                    'paths' => [
210
                        'src/Db/Entities',
211
                    ],
212
                ],
213
            ],
214
            'subscribers' => [
215
                Validation::class,
216
            ],
217
        ];
218
    }
219
220
    public function getRoutesConfig(): array
221
    {
222
        return [
223
            [
224
                'path' => '/config/{id:\d+}',
225
                'middleware' => [
226
                    TokenMiddleware::class,
227
                    'DbConfigResourceMiddleware',
228
                    ValidateOwnerMiddleware::class,
229
                    GetConfigAction::class,
230
                ],
231
                'name' => 'get_config',
232
                'allowed_methods' => ['GET'],
233
            ],
234
            [
235
                'path' => '/configs',
236
                'middleware' => [
237
                    TokenMiddleware::class,
238
                    SetOwnerFilterMiddleware::class,
239
                    GetConfigsAction::class,
240
                ],
241
                'name' => 'get_configs',
242
                'allowed_methods' => ['GET'],
243
            ],
244
            [
245
                'path' => '/config',
246
                'middleware' => [
247
                    TokenMiddleware::class,
248
                    BodyParamsMiddleware::class,
249
                    'ConfigInputFilterMiddleware',
250
                    SetOwnerMiddleware::class,
251
                    AddConfigAction::class,
252
                ],
253
                'name' => 'add_config',
254
                'allowed_methods' => ['POST'],
255
            ],
256
            [
257
                'path' => '/config/{id:\d+}',
258
                'middleware' => [
259
                    TokenMiddleware::class,
260
                    'DbConfigResourceMiddleware',
261
                    ValidateOwnerMiddleware::class,
262
                    BodyParamsMiddleware::class,
263
                    'ConfigInputFilterMiddleware',
264
                    SetOwnerMiddleware::class,
265
                    UpdateConfigAction::class,
266
                ],
267
                'name' => 'update_config',
268
                'allowed_methods' => ['PUT'],
269
            ],
270
            [
271
                'path' => '/config/{id:\d+}',
272
                'middleware' => [
273
                    TokenMiddleware::class,
274
                    'DbConfigResourceMiddleware',
275
                    ValidateOwnerMiddleware::class,
276
                    DeleteConfigAction::class,
277
                ],
278
                'name' => 'delete_config',
279
                'allowed_methods' => ['DELETE'],
280
            ],
281
        ];
282
    }
283
}
284