Completed
Push — master ( 3eb757...3bc7c8 )
by Oleg
03:46
created

ConfigProvider::__invoke()   A

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\Db\Controller\AddConfigAction;
8
use SlayerBirden\DataFlowServer\Db\Controller\DeleteConfigAction;
9
use SlayerBirden\DataFlowServer\Db\Controller\GetConfigAction;
10
use SlayerBirden\DataFlowServer\Db\Controller\GetConfigsAction;
11
use SlayerBirden\DataFlowServer\Db\Controller\UpdateConfigAction;
12
use SlayerBirden\DataFlowServer\Db\Factory\DbConfigHydratorFactory;
13
use SlayerBirden\DataFlowServer\Db\Factory\DbConfigResourceMiddlewareFactory;
14
use SlayerBirden\DataFlowServer\Db\Repository\DbConfigurationRepository;
15
use SlayerBirden\DataFlowServer\Db\Validation\ConfigValidator;
16
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
17
use SlayerBirden\DataFlowServer\Zend\InputFilter\ProxyFilterManagerFactory;
18
use Zend\Expressive\Application;
19
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
20
use Zend\ServiceManager\Factory\InvokableFactory;
21
22
class ConfigProvider
23
{
24
    public function __invoke(): array
25
    {
26
        return [
27
            ConfigAbstractFactory::class => $this->getAbstractFactoryConfig(),
28
            'dependencies' => $this->getDependenciesConfig(),
29
            'doctrine' => $this->getDoctrineConfig(),
30
            'validators' => $this->getValidatorsConfig(),
31
            'input_filter_specs' => [
32
                'ConfigInputFilter' => $this->getConfigInputFilterSpec(),
33
            ]
34
        ];
35
    }
36
37
    private function getConfigInputFilterSpec(): array
38
    {
39
        return [
40
            'title' => [
41
                'filters' => [
42
                    [
43
                        'name' => 'stringtrim',
44
                    ]
45
                ],
46
                'validators' => [
47
                    [
48
                        'name' => 'notempty',
49
                    ],
50
                ]
51
            ],
52
            'dbname' => [
53
                'required' => false,
54
                'continue_if_empty' => true,
55
                'filters' => [
56
                    [
57
                        'name' => 'stringtrim',
58
                    ]
59
                ],
60
                'validators' => [
61
                    [
62
                        'name' => 'configValidator',
63
                    ]
64
                ]
65
            ],
66
            'user' => [
67
                'required' => false,
68
                'continue_if_empty' => true,
69
                'filters' => [
70
                    [
71
                        'name' => 'stringtrim',
72
                    ]
73
                ],
74
                'validators' => [
75
                    [
76
                        'name' => 'configValidator',
77
                    ]
78
                ]
79
            ],
80
            'password' => [
81
                'required' => false,
82
                'continue_if_empty' => true,
83
                'filters' => [
84
                    [
85
                        'name' => 'stringtrim',
86
                    ]
87
                ],
88
                'validators' => [
89
                    [
90
                        'name' => 'configValidator',
91
                    ]
92
                ]
93
            ],
94
            'host' => [
95
                'required' => false,
96
                'continue_if_empty' => true,
97
                'filters' => [
98
                    [
99
                        'name' => 'stringtrim',
100
                    ]
101
                ],
102
                'validators' => [
103
                    [
104
                        'name' => 'configValidator',
105
                    ]
106
                ]
107
            ],
108
            'driver' => [
109
                'required' => false,
110
                'continue_if_empty' => true,
111
                'filters' => [
112
                    [
113
                        'name' => 'stringtrim',
114
                    ]
115
                ],
116
                'validators' => [
117
                    [
118
                        'name' => 'configValidator',
119
                    ]
120
                ]
121
            ],
122
            'port' => [
123
                'required' => false,
124
                'continue_if_empty' => true,
125
                'filters' => [
126
                    [
127
                        'name' => 'stringtrim',
128
                    ]
129
                ],
130
                'validators' => [
131
                    [
132
                        'name' => 'configValidator',
133
                    ]
134
                ]
135
            ],
136
            'url' => [
137
                'required' => false,
138
                'filters' => [
139
                    [
140
                        'name' => 'stringtrim',
141
                    ]
142
                ],
143
            ],
144
        ];
145
    }
146
147
    private function getAbstractFactoryConfig(): array
148
    {
149
        return [
150
            DbConfigurationRepository::class => [
151
                EntityManagerRegistry::class,
152
            ],
153
            AddConfigAction::class => [
154
                EntityManagerRegistry::class,
155
                'DbConfigHydrator',
156
                'ConfigInputFilter',
157
                LoggerInterface::class,
158
            ],
159
            UpdateConfigAction::class => [
160
                EntityManagerRegistry::class,
161
                'DbConfigHydrator',
162
                'ConfigInputFilter',
163
                LoggerInterface::class,
164
            ],
165
            GetConfigsAction::class => [
166
                DbConfigurationRepository::class,
167
                LoggerInterface::class,
168
                'DbConfigHydrator',
169
            ],
170
            GetConfigAction::class => [
171
                'DbConfigHydrator',
172
            ],
173
            DeleteConfigAction::class => [
174
                EntityManagerRegistry::class,
175
                LoggerInterface::class,
176
                'DbConfigHydrator',
177
            ],
178
        ];
179
    }
180
181
    public function getDependenciesConfig(): array
182
    {
183
        return [
184
            'delegators' => [
185
                Application::class => [
186
                    Factory\RoutesDelegator::class,
187
                ],
188
            ],
189
            'factories' => [
190
                'DbConfigHydrator' => DbConfigHydratorFactory::class,
191
                'ConfigInputFilter' => ProxyFilterManagerFactory::class,
192
                'DbConfigResourceMiddleware' => DbConfigResourceMiddlewareFactory::class,
193
            ],
194
        ];
195
    }
196
197
    public function getDoctrineConfig(): array
198
    {
199
        return [
200
            'entity_managers' => [
201
                'default' => [
202
                    'paths' => [
203
                        'src/Db/Entities',
204
                    ],
205
                ],
206
            ],
207
        ];
208
    }
209
210
    public function getValidatorsConfig(): array
211
    {
212
        return [
213
            'aliases' => [
214
                'configValidator' => ConfigValidator::class,
215
            ],
216
            'factories' => [
217
                ConfigValidator::class => InvokableFactory::class,
218
            ],
219
        ];
220
    }
221
}
222