Completed
Push — develop ( 26b032...524257 )
by Neomerx
18:05 queued 09:45
created

FluteContainerConfigurator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 11
dl 0
loc 117
rs 10
c 3
b 0
f 0
ccs 56
cts 56
cp 1

2 Methods

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