Completed
Push — develop ( dcfc04...3d10a6 )
by Neomerx
04:33
created

FormValidatorFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 8
dl 0
loc 38
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createValidator() 0 20 1
1
<?php namespace Limoncello\Flute\Validation\Form\Execution;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Container\Traits\HasContainerTrait;
20
use Limoncello\Contracts\L10n\FormatterFactoryInterface;
21
use Limoncello\Contracts\Settings\SettingsProviderInterface;
22
use Limoncello\Flute\Contracts\Validation\FormValidatorFactoryInterface;
23
use Limoncello\Flute\Contracts\Validation\FormValidatorInterface;
24
use Limoncello\Flute\Package\FluteSettings as S;
25
use Limoncello\Flute\Validation\Form\FormValidator;
26
use Limoncello\Validation\Execution\ContextStorage;
27
use Psr\Container\ContainerInterface;
28
29
/**
30
 * @package Limoncello\Flute
31
 */
32
class FormValidatorFactory implements FormValidatorFactoryInterface
33
{
34
    use HasContainerTrait;
35
36
    /**
37
     * @param ContainerInterface $container
38
     */
39
    public function __construct(ContainerInterface $container)
40
    {
41
        $this->setContainer($container);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     *
47
     * @SuppressWarnings(PHPMD.StaticAccess)
48
     */
49
    public function createValidator(string $rulesClass): FormValidatorInterface
50
    {
51
        /** @var SettingsProviderInterface $settingsProvider */
52
        $settingsProvider = $this->getContainer()->get(SettingsProviderInterface::class);
53
        $serializedData   = S::getFormSerializedRules($settingsProvider->get(S::class));
54
55
        /** @var FormatterFactoryInterface $factory */
56
        $factory   = $this->getContainer()->get(FormatterFactoryInterface::class);
57
        $formatter = $factory->createFormatter(S::VALIDATION_NAMESPACE);
58
59
        $validator = new FormValidator(
60
            $rulesClass,
61
            FormRulesSerializer::class,
62
            $serializedData,
63
            new ContextStorage(FormRulesSerializer::readBlocks($serializedData), $this->getContainer()),
64
            $formatter
65
        );
66
67
        return $validator;
68
    }
69
}
70