Completed
Push — 8.0 ( bf11e8...f611ca )
by David
02:23
created

SplashServiceProvider::getRootUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Mouf\Mvc\Splash\DI;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Interop\Container\ContainerInterface;
7
use Interop\Container\Factories\Parameter;
8
use Interop\Container\ServiceProvider;
9
use Mouf\Mvc\Splash\Routers\SplashDefaultRouter;
10
use Mouf\Mvc\Splash\Services\ControllerAnalyzer;
11
use Mouf\Mvc\Splash\Services\ControllerRegistry;
12
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry;
13
use Mouf\Mvc\Splash\Services\SplashRequestFetcher;
14
use Mouf\Mvc\Splash\Services\SplashRequestParameterFetcher;
15
use Mouf\Mvc\Splash\Services\SplashUtils;
16
use Psr\Cache\CacheItemPoolInterface;
17
use Psr\Log\LoggerInterface;
18
19
class SplashServiceProvider implements ServiceProvider
20
{
21
    const PACKAGE_NAME = 'thecodingmachine/splash';
22
23
    /**
24
     * Returns a list of all container entries registered by this service provider.
25
     *
26
     * - the key is the entry name
27
     * - the value is a callable that will return the entry, aka the **factory**
28
     *
29
     * Factories have the following signature:
30
     *        function(ContainerInterface $container, callable $getPrevious = null)
31
     *
32
     * About factories parameters:
33
     *
34
     * - the container (instance of `Interop\Container\ContainerInterface`)
35
     * - a callable that returns the previous entry if overriding a previous entry, or `null` if not
36
     *
37
     * @return callable[]
38
     */
39
    public function getServices()
40
    {
41
        return [
42
            SplashDefaultRouter::class => [self::class, 'createDefaultRouter'],
43
            'thecodingmachine.splash.route-providers' => [self::class, 'createRouteProviders'],
44
            ControllerRegistry::class => [self::class, 'createControllerRegistry'],
45
            ControllerAnalyzer::class => [self::class, 'createControllerAnalyzer'],
46
            ParameterFetcherRegistry::class => [self::class, 'createParameterFetcherRegistry'],
47
            'thecodingmachine.splash.parameter-fetchers' => [self::class, 'createParameterFetchers'],
48
            SplashRequestFetcher::class => [self::class, 'createSplashRequestFetcher'],
49
            SplashRequestParameterFetcher::class => [self::class, 'createSplashRequestParameterFetcher'],
50
            'thecodingmachine.splash.mode' => new Parameter(SplashUtils::MODE_STRICT),
51
        ];
52
    }
53
54
    public static function createDefaultRouter(ContainerInterface $container) : SplashDefaultRouter
55
    {
56
        if ($container->has(CacheItemPoolInterface::class)) {
57
            $cache = $container->get(CacheItemPoolInterface::class);
58
        } else {
59
            $cache = null;
60
        }
61
62
        if ($container->has(LoggerInterface::class)) {
63
            $logger = $container->get(LoggerInterface::class);
64
        } else {
65
            $logger = null;
66
        }
67
68
        $routeProviders = $container->get('thecodingmachine.splash.route-providers');
69
70
        $router = new SplashDefaultRouter($container, $routeProviders, $container->get(ParameterFetcherRegistry::class), $cache, $logger, self::getRootUrl($container));
71
72
        return $router;
73
    }
74
75
    private static function getRootUrl(ContainerInterface $container)
76
    {
77
        if ($container->has('thecodingmachine.splash.root_url')) {
78
            return $container->get('thecodingmachine.splash.root_url');
79
        } elseif ($container->has('root_url')) {
80
            return $container->get('root_url');
81
        } else {
82
            return '/';
83
        }
84
    }
85
86
    public static function createRouteProviders(ContainerInterface $container) : array
87
    {
88
        return [
89
            $container->get(ControllerRegistry::class),
90
        ];
91
    }
92
93
    public static function createControllerRegistry(ContainerInterface $container) : ControllerRegistry
94
    {
95
        return new ControllerRegistry($container->get(ControllerAnalyzer::class),
96
            $container->get('thecodingmachine.splash.controllers'));
97
    }
98
99
    public static function createControllerAnalyzer(ContainerInterface $container) : ControllerAnalyzer
100
    {
101
        return new ControllerAnalyzer($container, $container->get(ParameterFetcherRegistry::class),
102
            $container->get(Reader::class));
103
    }
104
105
    public static function createParameterFetcherRegistry(ContainerInterface $container) : ParameterFetcherRegistry
106
    {
107
        return new ParameterFetcherRegistry($container->get('thecodingmachine.splash.parameter-fetchers'));
108
    }
109
110
    public static function createParameterFetchers(ContainerInterface $container) : array
111
    {
112
        return [
113
            $container->get(SplashRequestFetcher::class),
114
            $container->get(SplashRequestParameterFetcher::class),
115
        ];
116
    }
117
118
    public static function createSplashRequestFetcher() : SplashRequestFetcher
119
    {
120
        return new SplashRequestFetcher();
121
    }
122
123
    public static function createSplashRequestParameterFetcher() : SplashRequestParameterFetcher
124
    {
125
        return new SplashRequestParameterFetcher();
126
    }
127
}
128