Passed
Push — master ( ff0e1a...d70f62 )
by Mr
02:29
created

HttpPipelineProvisioner::provision()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 60
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 45
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 60
ccs 0
cts 51
cp 0
crap 6
rs 9.2

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 declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Service\Provisioner;
10
11
use Aura\Router\RouterContainer;
12
use Auryn\Injector;
13
use Daikon\Boot\Config\RoutingConfigLoader;
14
use Daikon\Boot\Middleware\PipelineBuilderInterface;
15
use Daikon\Boot\Middleware\RoutingHandler;
16
use Daikon\Boot\Service\ServiceDefinitionInterface;
17
use Daikon\Config\ConfigProviderInterface;
18
use Middlewares\ContentEncoding;
19
use Middlewares\ContentLanguage;
20
use Middlewares\ContentType;
21
use Middlewares\JsonPayload;
22
use Middlewares\RequestHandler;
23
use Middlewares\UrlEncodePayload;
24
use Neomerx\Cors\Analyzer;
25
use Neomerx\Cors\Contracts\AnalyzerInterface;
26
use Neomerx\Cors\Strategies\Settings;
27
use Psr\Container\ContainerInterface;
28
29
final class HttpPipelineProvisioner implements ProvisionerInterface
30
{
31
    public function provision(
32
        Injector $injector,
33
        ConfigProviderInterface $config,
34
        ServiceDefinitionInterface $serviceDefinition
35
    ): void {
36
        $serviceClass = $serviceDefinition->getServiceClass();
37
        $settings = $serviceDefinition->getSettings();
38
39
        $injector
40
            ->define($serviceClass, [':settings' => $settings])
41
            ->share($serviceClass)
42
            ->alias(PipelineBuilderInterface::class, $serviceClass)
43
            // Content Negotiation
44
            ->define(ContentLanguage::class, [':languages' => $config->get('project.negotiation.languages', ['en'])])
45
            ->define(ContentEncoding::class, [':encodings' => ['gzip', 'deflate']])
46
            ->delegate(ContentType::class, function () use ($config): ContentType {
47
                return (new ContentType($config->get('project.negotiation.content_types')))
48
                    ->charsets($config->get('project.negotiation.charsets', ['UTF-8']))
49
                    ->nosniff($config->get('project.negotiation.nosniff', true))
50
                    ->errorResponse();
51
            })
52
            // Cors
53
            ->share(AnalyzerInterface::class)
54
            ->alias(AnalyzerInterface::class, Analyzer::class)
55
            ->delegate(Analyzer::class, function () use ($config): AnalyzerInterface {
56
                $corsSettings = (new Settings)
57
                    ->setServerOrigin(
58
                        $config->get('project.cors.scheme'),
59
                        $config->get('project.cors.host'),
60
                        $config->get('project.cors.port')
61
                    )->setAllowedOrigins(
62
                        $config->get('project.cors.request.allowed_origins', [])
63
                    )->setAllowedHeaders(
64
                        $config->get('project.cors.request.allowed_headers', [])
65
                    )->setAllowedMethods(
66
                        $config->get('project.cors.request.allowed_methods', [])
67
                    )->setPreFlightCacheMaxAge(
68
                        $config->get('project.cors.response.preflight_cache_max_age', 0)
69
                    )->setExposedHeaders(
70
                        $config->get('project.cors.response.exposed_headers', [])
71
                    )->enableCheckHost();
72
                if ($config->get('project.cors.request.allowed_credentials') === true) {
73
                    $corsSettings = $corsSettings->setCredentialsSupported();
74
                }
75
                return Analyzer::instance($corsSettings);
76
            })
77
            // Request
78
            ->share(JsonPayload::class)
79
            ->delegate(JsonPayload::class, fn(): JsonPayload => (new JsonPayload)->depth(8)->override(true))
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 79 at column 47
Loading history...
80
            ->share(UrlEncodePayload::class)
81
            ->delegate(UrlEncodePayload::class, fn(): UrlEncodePayload => (new UrlEncodePayload)->override(true))
82
            ->share(RequestHandler::class)
83
            ->delegate(
84
                RequestHandler::class,
85
                function (ContainerInterface $container): RequestHandler {
86
                    return (new RequestHandler($container))->handlerAttribute(RoutingHandler::ATTR_REQUEST_HANDLER);
87
                }
88
            )
89
            // Routing
90
            ->share(RoutingHandler::class)
91
            ->delegate(
92
                RoutingHandler::class,
93
                function (ContainerInterface $container) use ($config): RoutingHandler {
94
                    return new RoutingHandler($this->routerFactory($config), $container);
95
                }
96
            );
97
    }
98
99
    private function routerFactory(ConfigProviderInterface $config): RouterContainer
100
    {
101
        $appContext = $config->get('app.context');
102
        $appEnv = $config->get('app.env');
103
        $appConfigDir = $config->get('app.config_dir');
104
        $router = new RouterContainer;
105
        (new RoutingConfigLoader($router, $config))->load(
106
            array_merge([$appConfigDir], (array)$config->get('crates.*.config_dir', [])),
107
            [
108
                'routing.php',
109
                "routing.$appContext.php",
110
                "routing.$appEnv.php",
111
                "routing.$appContext.$appEnv.php"
112
            ]
113
        );
114
        return $router;
115
    }
116
}
117