Completed
Push — develop ( b6f561...b77323 )
by Neomerx
01:59
created

FluteContainerConfigurator::configureContainer()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 80
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 6.5597
c 0
b 0
f 0
ccs 43
cts 43
cp 1
cc 7
eloc 44
nc 16
nop 1
crap 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Limoncello\Flute\Package;
2
3
use Doctrine\DBAL\Connection;
4
use Doctrine\DBAL\Types\Type;
5
use Limoncello\Contracts\Application\ApplicationSettingsInterface as A;
6
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
7
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
8
use Limoncello\Contracts\Data\ModelSchemeInfoInterface;
9
use Limoncello\Contracts\Exceptions\ThrowableHandlerInterface;
10
use Limoncello\Contracts\L10n\FormatterFactoryInterface;
11
use Limoncello\Contracts\Settings\SettingsProviderInterface;
12
use Limoncello\Flute\Adapters\FilterOperations;
13
use Limoncello\Flute\Adapters\PaginationStrategy;
14
use Limoncello\Flute\Contracts\Adapters\FilterOperationsInterface;
15
use Limoncello\Flute\Contracts\Adapters\PaginationStrategyInterface;
16
use Limoncello\Flute\Contracts\Adapters\RepositoryInterface;
17
use Limoncello\Flute\Contracts\Encoder\EncoderInterface;
18
use Limoncello\Flute\Contracts\FactoryInterface;
19
use Limoncello\Flute\Contracts\Schema\JsonSchemesInterface;
20
use Limoncello\Flute\Contracts\Validation\JsonApiValidatorFactoryInterface;
21
use Limoncello\Flute\Factory;
22
use Limoncello\Flute\Http\Errors\FluteThrowableHandler;
23
use Limoncello\Flute\L10n\Messages;
24
use Limoncello\Flute\Types\DateJsonApiStringType;
25
use Limoncello\Flute\Types\DateTimeJsonApiStringType;
26
use Limoncello\Flute\Types\JsonApiDateTimeType;
27
use Limoncello\Flute\Types\JsonApiDateType;
28
use Limoncello\Flute\Validation\Execution\JsonApiValidatorFactory;
29
use Neomerx\JsonApi\Contracts\Http\Query\QueryParametersParserInterface;
30
use Neomerx\JsonApi\Encoder\EncoderOptions;
31
use Psr\Container\ContainerInterface as PsrContainerInterface;
32
use Psr\Log\LoggerInterface;
33
34
/**
35
 * @package Limoncello\Flute
36
 *
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
 */
39
class FluteContainerConfigurator implements ContainerConfiguratorInterface
40
{
41
    /** @var callable */
42
    const CONFIGURE_EXCEPTION_HANDLER = [self::class, 'configureExceptionHandler'];
43
44
    /**
45
     * @inheritdoc
46
     *
47 1
     * @SuppressWarnings(PHPMD.StaticAccess)
48
     */
49 1
    public static function configureContainer(LimoncelloContainerInterface $container): void
50
    {
51 1
        $factory = new Factory($container);
52
53 1
        $container[FactoryInterface::class] = $factory;
54 1
55
        $container[QueryParametersParserInterface::class] = function () use ($factory) {
56
            return $factory->getJsonApiFactory()->createQueryParametersParser();
57 1
        };
58 1
59 1
        $container[JsonSchemesInterface::class] = function (PsrContainerInterface $container) use ($factory) {
60
            $settings     = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class);
61 1
            $modelSchemes = $container->get(ModelSchemeInfoInterface::class);
62
63
            return $factory->createJsonSchemes($settings[FluteSettings::KEY_MODEL_TO_SCHEME_MAP], $modelSchemes);
64 1
        };
65
66 1
        $container[EncoderInterface::class] = function (PsrContainerInterface $container) use ($factory) {
67 1
            /** @var JsonSchemesInterface $jsonSchemes */
68 1
            $jsonSchemes = $container->get(JsonSchemesInterface::class);
69 1
            $settings    = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class);
70 1
            $encoder     = $factory->createEncoder($jsonSchemes, new EncoderOptions(
71 1
                $settings[FluteSettings::KEY_JSON_ENCODE_OPTIONS],
72
                $settings[FluteSettings::KEY_URI_PREFIX],
73 1
                $settings[FluteSettings::KEY_JSON_ENCODE_DEPTH]
74 1
            ));
75
            isset($settings[FluteSettings::KEY_META]) ? $encoder->withMeta($settings[FluteSettings::KEY_META]) : null;
76 1
            ($settings[FluteSettings::KEY_IS_SHOW_VERSION] ?? false) ? $encoder->withJsonApiVersion() : null;
77
78
            return $encoder;
79 1
        };
80 1
81
        $container[FilterOperationsInterface::class] = function (PsrContainerInterface $container) {
82
            return new FilterOperations($container);
83 1
        };
84 1
85
        $container[RepositoryInterface::class] = function (PsrContainerInterface $container) use ($factory) {
86 1
            $connection = $container->get(Connection::class);
87
            /** @var ModelSchemeInfoInterface $modelSchemes */
88
            $modelSchemes = $container->get(ModelSchemeInfoInterface::class);
89 1
90
            /** @var FilterOperationsInterface $filerOps */
91
            $filerOps = $container->get(FilterOperationsInterface::class);
92 1
93 1
            /** @var FormatterFactoryInterface $formatterFactory */
94
            $formatterFactory = $container->get(FormatterFactoryInterface::class);
95 1
            $formatter        = $formatterFactory->createFormatter(Messages::RESOURCES_NAMESPACE);
96
97
            return $factory->createRepository($connection, $modelSchemes, $filerOps, $formatter);
98 1
        };
99 1
100
        $container[PaginationStrategyInterface::class] = function (PsrContainerInterface $container) {
101 1
            $settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class);
102 1
103 1
            return new PaginationStrategy(
104
                $settings[FluteSettings::KEY_DEFAULT_PAGING_SIZE],
105
                $settings[FluteSettings::KEY_MAX_PAGING_SIZE]
106
            );
107 1
        };
108 1
109
        $container[JsonApiValidatorFactoryInterface::class] = function (PsrContainerInterface $container) {
110 1
            $factory = new JsonApiValidatorFactory($container);
111
112
            return $factory;
113
        };
114 1
115 1
        // register date/date time types
116
        if (Type::hasType(DateTimeJsonApiStringType::NAME) === false) {
117 1
            Type::addType(DateTimeJsonApiStringType::NAME, DateTimeJsonApiStringType::class);
118 1
        }
119
        if (Type::hasType(DateJsonApiStringType::NAME) === false) {
120 1
            Type::addType(DateJsonApiStringType::NAME, DateJsonApiStringType::class);
121 1
        }
122
        if (Type::hasType(JsonApiDateTimeType::NAME) === false) {
123 1
            Type::addType(JsonApiDateTimeType::NAME, JsonApiDateTimeType::class);
124 1
        }
125
        if (Type::hasType(JsonApiDateType::NAME) === false) {
126
            Type::addType(JsonApiDateType::NAME, JsonApiDateType::class);
127
        }
128
    }
129
130
    /**
131
     * @param LimoncelloContainerInterface $container
132
     *
133
     * @return void
134
     */
135 1
    public static function configureExceptionHandler(LimoncelloContainerInterface $container)
136 1
    {
137
        $container[ThrowableHandlerInterface::class] = function (PsrContainerInterface $container) {
138
            $appSettings   = $container->get(SettingsProviderInterface::class)->get(A::class);
139
            $fluteSettings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class);
140
141
            $isLogEnabled = $appSettings[A::KEY_IS_LOG_ENABLED];
142
            $isDebug      = $appSettings[A::KEY_IS_DEBUG];
143
144
            $ignoredErrorClasses   = $fluteSettings[FluteSettings::KEY_DO_NOT_LOG_EXCEPTIONS_LIST__AS_KEYS];
145
            $httpCodeForUnexpected = $fluteSettings[FluteSettings::KEY_HTTP_CODE_FOR_UNEXPECTED_THROWABLE];
146
147
            /** @var EncoderInterface $encoder */
148
            $encoder = $container->get(EncoderInterface::class);
149
150
            $handler = new FluteThrowableHandler($encoder, $ignoredErrorClasses, $httpCodeForUnexpected, $isDebug);
151
152
            if ($isLogEnabled === true && $container->has(LoggerInterface::class) === true) {
153
                /** @var LoggerInterface $logger */
154
                $logger = $container->get(LoggerInterface::class);
155
                $handler->setLogger($logger);
156
            }
157
158
            return $handler;
159
        };
160
    }
161
}
162