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

JsonApiParserFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 48 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 12
dl 36
loc 75
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createDataParser() 17 17 1
A createQueryParser() 19 19 1
A getFluteSettings() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Limoncello\Flute\Validation\JsonApi\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\JsonApiDataParserInterface;
23
use Limoncello\Flute\Contracts\Validation\JsonApiParserFactoryInterface;
24
use Limoncello\Flute\Contracts\Validation\JsonApiQueryParserInterface;
25
use Limoncello\Flute\Package\FluteSettings;
26
use Limoncello\Flute\Validation\JsonApi\DataParser;
27
use Limoncello\Flute\Validation\JsonApi\QueryParser;
28
use Limoncello\Validation\Captures\CaptureAggregator;
29
use Limoncello\Validation\Errors\ErrorAggregator;
30
use Limoncello\Validation\Execution\ContextStorage;
31
use Psr\Container\ContainerExceptionInterface;
32
use Psr\Container\ContainerInterface;
33
use Psr\Container\NotFoundExceptionInterface;
34
35
/**
36
 * @package Limoncello\Flute
37
 *
38
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
39
 */
40
class JsonApiParserFactory implements JsonApiParserFactoryInterface
41
{
42
    use HasContainerTrait;
43
44
    /**
45
     * @param ContainerInterface $container
46
     */
47
    public function __construct(ContainerInterface $container)
48
    {
49
        $this->setContainer($container);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     *
55
     * @SuppressWarnings(PHPMD.StaticAccess)
56
     */
57 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...
58
    {
59
        $serializedData = FluteSettings::getJsonDataSerializedRules($this->getFluteSettings());
60
61
        /** @var FormatterFactoryInterface $formatterFactory */
62
        $formatterFactory = $this->getContainer()->get(FormatterFactoryInterface::class);
63
        $parser           = new DataParser(
64
            $rulesClass,
65
            JsonApiDataRulesSerializer::class,
66
            $serializedData,
67
            new ContextStorage(JsonApiQueryRulesSerializer::readBlocks($serializedData), $this->getContainer()),
68
            new JsonApiErrorCollection($formatterFactory->createFormatter(FluteSettings::VALIDATION_NAMESPACE)),
69
            $this->getContainer()->get(FormatterFactoryInterface::class)
70
        );
71
72
        return $parser;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     *
78
     * @SuppressWarnings(PHPMD.StaticAccess)
79
     */
80 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...
81
    {
82
        $serializedData = FluteSettings::getJsonQuerySerializedRules($this->getFluteSettings());
83
84
        /** @var FormatterFactoryInterface $formatterFactory */
85
        $formatterFactory = $this->getContainer()->get(FormatterFactoryInterface::class);
86
        $parser           = new QueryParser(
87
            $rulesClass,
88
            JsonApiQueryRulesSerializer::class,
89
            $serializedData,
90
            new ContextStorage(JsonApiQueryRulesSerializer::readBlocks($serializedData), $this->getContainer()),
91
            new CaptureAggregator(),
92
            new ErrorAggregator(),
93
            new JsonApiErrorCollection($formatterFactory->createFormatter(FluteSettings::VALIDATION_NAMESPACE)),
94
            $this->getContainer()->get(FormatterFactoryInterface::class)
95
        );
96
97
        return $parser;
98
    }
99
100
    /**
101
     * @return array
102
     *
103
     * @throws ContainerExceptionInterface
104
     * @throws NotFoundExceptionInterface
105
     */
106
    private function getFluteSettings(): array
107
    {
108
        /** @var SettingsProviderInterface $settingsProvider */
109
        $settingsProvider = $this->getContainer()->get(SettingsProviderInterface::class);
110
        $settings         = $settingsProvider->get(FluteSettings::class);
111
112
        return $settings;
113
    }
114
}
115