FormValidatorFactory::createValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

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