Completed
Pull Request — 8.2 (#40)
by David
11:30
created

SplashServiceProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 8
dl 0
loc 107
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactories() 0 15 1
A getExtensions() 0 6 1
A createDefaultRouter() 0 20 3
A getRootUrl() 0 10 3
A createRouteProviders() 0 6 1
A createControllerRegistry() 0 5 1
A createControllerAnalyzer() 0 5 1
A createParameterFetcherRegistry() 0 4 1
A createParameterFetchers() 0 7 1
A createSplashRequestFetcher() 0 4 1
A createSplashRequestParameterFetcher() 0 4 1
A updatePriorityQueue() 0 5 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\ServiceProviderInterface;
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
use TheCodingMachine\MiddlewareListServiceProvider;
19
use TheCodingMachine\MiddlewareOrder;
20
21
class SplashServiceProvider implements ServiceProviderInterface
22
{
23
    const PACKAGE_NAME = 'thecodingmachine/splash';
24
25
    public function getFactories()
26
    {
27
        return [
28
            SplashDefaultRouter::class => [self::class, 'createDefaultRouter'],
29
            'thecodingmachine.splash.route-providers' => [self::class, 'createRouteProviders'],
30
            ControllerRegistry::class => [self::class, 'createControllerRegistry'],
31
            ControllerAnalyzer::class => [self::class, 'createControllerAnalyzer'],
32
            ParameterFetcherRegistry::class => [self::class, 'createParameterFetcherRegistry'],
33
            'thecodingmachine.splash.parameter-fetchers' => [self::class, 'createParameterFetchers'],
34
            SplashRequestFetcher::class => [self::class, 'createSplashRequestFetcher'],
35
            SplashRequestParameterFetcher::class => [self::class, 'createSplashRequestParameterFetcher'],
36
            'thecodingmachine.splash.mode' => new Parameter(SplashUtils::MODE_STRICT),
37
            'thecodingmachine.splash.controllers' => new Parameter([])
38
        ];
39
    }
40
41
    public function getExtensions()
42
    {
43
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(\TheCodingM...updatePriorityQueue')); (array<*,string[]>) is incompatible with the return type declared by the interface Interop\Container\Servic...nterface::getExtensions of type callable[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
44
            MiddlewareListServiceProvider::MIDDLEWARES_QUEUE => [self::class, 'updatePriorityQueue'],
45
        ];
46
    }
47
48
    public static function createDefaultRouter(ContainerInterface $container) : SplashDefaultRouter
49
    {
50
        if ($container->has(CacheItemPoolInterface::class)) {
51
            $cache = $container->get(CacheItemPoolInterface::class);
52
        } else {
53
            $cache = null;
54
        }
55
56
        if ($container->has(LoggerInterface::class)) {
57
            $logger = $container->get(LoggerInterface::class);
58
        } else {
59
            $logger = null;
60
        }
61
62
        $routeProviders = $container->get('thecodingmachine.splash.route-providers');
63
64
        $router = new SplashDefaultRouter($container, $routeProviders, $container->get(ParameterFetcherRegistry::class), $cache, $logger, SplashUtils::MODE_STRICT, true, self::getRootUrl($container));
65
66
        return $router;
67
    }
68
69
    private static function getRootUrl(ContainerInterface $container)
70
    {
71
        if ($container->has('thecodingmachine.splash.root_url')) {
72
            return $container->get('thecodingmachine.splash.root_url');
73
        } elseif ($container->has('root_url')) {
74
            return $container->get('root_url');
75
        } else {
76
            return '/';
77
        }
78
    }
79
80
    public static function createRouteProviders(ContainerInterface $container) : array
81
    {
82
        return [
83
            $container->get(ControllerRegistry::class),
84
        ];
85
    }
86
87
    public static function createControllerRegistry(ContainerInterface $container) : ControllerRegistry
88
    {
89
        return new ControllerRegistry($container->get(ControllerAnalyzer::class),
90
            $container->get('thecodingmachine.splash.controllers'));
91
    }
92
93
    public static function createControllerAnalyzer(ContainerInterface $container) : ControllerAnalyzer
94
    {
95
        return new ControllerAnalyzer($container, $container->get(ParameterFetcherRegistry::class),
96
            $container->get(Reader::class));
97
    }
98
99
    public static function createParameterFetcherRegistry(ContainerInterface $container) : ParameterFetcherRegistry
100
    {
101
        return new ParameterFetcherRegistry($container->get('thecodingmachine.splash.parameter-fetchers'));
102
    }
103
104
    public static function createParameterFetchers(ContainerInterface $container) : array
105
    {
106
        return [
107
            $container->get(SplashRequestFetcher::class),
108
            $container->get(SplashRequestParameterFetcher::class),
109
        ];
110
    }
111
112
    public static function createSplashRequestFetcher() : SplashRequestFetcher
113
    {
114
        return new SplashRequestFetcher();
115
    }
116
117
    public static function createSplashRequestParameterFetcher() : SplashRequestParameterFetcher
118
    {
119
        return new SplashRequestParameterFetcher();
120
    }
121
122
    public static function updatePriorityQueue(ContainerInterface $container, \SplPriorityQueue $priorityQueue) : \SplPriorityQueue
123
    {
124
        $priorityQueue->insert($container->get(SplashDefaultRouter::class), MiddlewareOrder::ROUTER);
125
        return $priorityQueue;
126
    }
127
}
128