JsonApiParserFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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\JsonApi\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\JsonApiDataParserInterface;
25
use Limoncello\Flute\Contracts\Validation\JsonApiParserFactoryInterface;
26
use Limoncello\Flute\Contracts\Validation\JsonApiQueryParserInterface;
27
use Limoncello\Flute\L10n\Messages;
28
use Limoncello\Flute\Package\FluteSettings;
29
use Limoncello\Flute\Validation\JsonApi\DataParser;
30
use Limoncello\Flute\Validation\JsonApi\QueryParser;
31
use Limoncello\Validation\Captures\CaptureAggregator;
32
use Limoncello\Validation\Errors\ErrorAggregator;
33
use Limoncello\Validation\Execution\ContextStorage;
34
use Psr\Container\ContainerExceptionInterface;
35
use Psr\Container\ContainerInterface;
36
use Psr\Container\NotFoundExceptionInterface;
37
38
/**
39
 * @package Limoncello\Flute
40
 *
41
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
42
 */
43
class JsonApiParserFactory implements JsonApiParserFactoryInterface
44
{
45
    use HasContainerTrait;
46
47
    /**
48
     * @param ContainerInterface $container
49
     */
50 31
    public function __construct(ContainerInterface $container)
51
    {
52 31
        $this->setContainer($container);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     */
60 14 View Code Duplication
    public function createDataParser(string $rulesClass): JsonApiDataParserInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62 14
        $serializedData = FluteSettings::getJsonDataSerializedRules($this->getFluteSettings());
63
64
        /** @var FormatterFactoryInterface $formatterFactory */
65 14
        $formatterFactory = $this->getContainer()->get(FormatterFactoryInterface::class);
66 14
        $parser           = new DataParser(
67 14
            $rulesClass,
68 14
            JsonApiDataRulesSerializer::class,
69 14
            $serializedData,
70 14
            new ContextStorage(JsonApiQueryRulesSerializer::readBlocks($serializedData), $this->getContainer()),
71 14
            new JsonApiErrorCollection($formatterFactory->createFormatter(Messages::NAMESPACE_NAME)),
72 14
            $this->getContainer()->get(FormatterFactoryInterface::class)
73
        );
74
75 14
        return $parser;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     *
81
     * @SuppressWarnings(PHPMD.StaticAccess)
82
     */
83 23 View Code Duplication
    public function createQueryParser(string $rulesClass): JsonApiQueryParserInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85 23
        $serializedData = FluteSettings::getJsonQuerySerializedRules($this->getFluteSettings());
86
87
        /** @var FormatterFactoryInterface $formatterFactory */
88 23
        $formatterFactory = $this->getContainer()->get(FormatterFactoryInterface::class);
89 23
        $parser           = new QueryParser(
90 23
            $rulesClass,
91 23
            JsonApiQueryRulesSerializer::class,
92 23
            $serializedData,
93 23
            new ContextStorage(JsonApiQueryRulesSerializer::readBlocks($serializedData), $this->getContainer()),
94 23
            new CaptureAggregator(),
95 23
            new ErrorAggregator(),
96 23
            new JsonApiErrorCollection($formatterFactory->createFormatter(Messages::NAMESPACE_NAME)),
97 23
            $this->getContainer()->get(FormatterFactoryInterface::class)
98
        );
99
100 23
        return $parser;
101
    }
102
103
    /**
104
     * @return array
105
     *
106
     * @throws ContainerExceptionInterface
107
     * @throws NotFoundExceptionInterface
108
     */
109 31
    private function getFluteSettings(): array
110
    {
111
        /** @var SettingsProviderInterface $settingsProvider */
112 31
        $settingsProvider = $this->getContainer()->get(SettingsProviderInterface::class);
113 31
        $settings         = $settingsProvider->get(FluteSettings::class);
114
115 31
        return $settings;
116
    }
117
}
118