Passed
Push — master ( 1a055f...41df92 )
by Riikka
02:09
created

ApplicationProvider::getServerRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Application;
4
5
use Interop\Http\Factory\RequestFactoryInterface;
6
use Interop\Http\Factory\ResponseFactoryInterface;
7
use Interop\Http\Factory\ServerRequestFactoryInterface;
8
use Interop\Http\Factory\StreamFactoryInterface;
9
use Interop\Http\Factory\UploadedFileFactoryInterface;
10
use Interop\Http\Factory\UriFactoryInterface;
11
use Psr\Container\ContainerInterface;
12
use Simply\Application\Handler\ErrorHandler;
13
use Simply\Application\Handler\MiddlewareHandler;
14
use Simply\Application\Handler\NotFoundHandler;
15
use Simply\Application\HttpFactory\DiactorosHttpFactory;
16
use Simply\Application\HttpFactory\HttpFactoryInterface;
17
use Simply\Application\Middleware\ErrorHandlerMiddleware;
18
use Simply\Application\Middleware\RouterMiddleware;
19
use Simply\Container\AbstractEntryProvider;
20
use Simply\Router\Router;
21
22
/**
23
 * Provides the default dependencies required by the application.
24
 * @author Riikka Kalliomäki <[email protected]>
25
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
26
 * @license http://opensource.org/licenses/mit-license.php MIT License
27
 */
28
class ApplicationProvider extends AbstractEntryProvider
29
{
30
    /**
31
     * Returns the configured web application.
32
     * @param ContainerInterface $container The container used to resolve dependencies
33
     * @return Application The configured web application
34
     */
35 11
    public function getApplication(ContainerInterface $container): Application
36
    {
37 11
        return new Application(
38 11
            $container->get(HttpFactoryInterface::class),
39 11
            $container->get(MiddlewareHandler::class),
40 11
            $container->get(HttpClient::class)
41
        );
42
    }
43
44
    /**
45
     * Returns the factory used create requests and responses.
46
     * @return HttpFactoryInterface The factory for creating requests and responses
47
     */
48 5
    public function getHttpFactory(): HttpFactoryInterface
49
    {
50 5
        return new DiactorosHttpFactory();
51
    }
52
53
    /**
54
     * Returns the default middleware stack that is run for every request.
55
     * @param ContainerInterface $container The container used to resolve dependencies
56
     * @return MiddlewareHandler The default middleware stack that is run for every request
57
     */
58 11
    public function getMiddlewareHandler(ContainerInterface $container): MiddlewareHandler
59
    {
60 11
        $stack = new MiddlewareHandler($container->get(NotFoundHandler::class));
61
62 11
        $stack->push($container->get(ErrorHandlerMiddleware::class));
63 11
        $stack->push($container->get(RouterMiddleware::class));
64
65 11
        return $stack;
66
    }
67
68
    /**
69
     * Returns the request handler used for 404 responses.
70
     * @param ContainerInterface $container The container used to resolve dependencies
71
     * @return NotFoundHandler Rhe request handler used for 404 responses
72
     */
73 11
    public function getNotFoundHandler(ContainerInterface $container): NotFoundHandler
74
    {
75 11
        return new NotFoundHandler(
76 11
            $container->get(ResponseFactoryInterface::class),
77 11
            $container->get(StreamFactoryInterface::class)
78
        );
79
    }
80
81
    /**
82
     * Returns the middleware used to route exceptions to the error handler.
83
     * @param ContainerInterface $container The container used to resolve dependencies
84
     * @return ErrorHandlerMiddleware The middleware used to route exceptions to the error handler
85
     */
86 11
    public function getErrorHandlerMiddleware(ContainerInterface $container): ErrorHandlerMiddleware
87
    {
88 11
        return new ErrorHandlerMiddleware(
89 11
            $container->get(ErrorHandler::class)
90
        );
91
    }
92
93
    /**
94
     * Returns the request handler for handling exception responses.
95
     * @param ContainerInterface $container The container used to resolve dependencies
96
     * @return ErrorHandler The request handler for handling exception responses
97
     */
98 11
    public function getErrorHandler(ContainerInterface $container): ErrorHandler
99
    {
100 11
        return new ErrorHandler(
101 11
            $container->get(ResponseFactoryInterface::class),
102 11
            $container->get(StreamFactoryInterface::class)
103
        );
104
    }
105
106
    /**
107
     * Returns the router middleware used to route requests to appropriate handlers.
108
     * @param ContainerInterface $container The container used to resolve dependencies
109
     * @return RouterMiddleware The router middleware used to route requests to appropriate handlers
110
     */
111 11
    public function getRouterMiddleware(ContainerInterface $container): RouterMiddleware
112
    {
113 11
        return new RouterMiddleware(
114 11
            $container->get(Router::class),
115 11
            $container,
116 11
            $container->get(ResponseFactoryInterface::class),
117 11
            $container->get(StreamFactoryInterface::class)
118
        );
119
    }
120
121
    /**
122
     * Returns the http client used to communicate to the browser.
123
     * @return HttpClient The http client used to communicate to the browser
124
     */
125 1
    public function getHttpClient(): HttpClient
126
    {
127 1
        return new HttpClient();
128
    }
129
130
    /**
131
     * Returns the Http factory for the purpose of filling the standard.
132
     * @param ContainerInterface $container The container used to resolve dependencies
133
     * @return RequestFactoryInterface
134
     */
135 1
    public function getRequestFactory(ContainerInterface $container): RequestFactoryInterface
136
    {
137 1
        return $container->get(HttpFactoryInterface::class);
138
    }
139
140
    /**
141
     * Returns the standard Response Factory.
142
     * @param ContainerInterface $container The container used to resolve dependencies
143
     * @return ResponseFactoryInterface
144
     */
145 11
    public function getResponseFactory(ContainerInterface $container): ResponseFactoryInterface
146
    {
147 11
        return $container->get(HttpFactoryInterface::class);
148
    }
149
150
    /**
151
     * @param ContainerInterface $container The container used to resolve dependencies
152
     * @return ServerRequestFactoryInterface
153
     */
154 1
    public function getServerRequestFactory(ContainerInterface $container): ServerRequestFactoryInterface
155
    {
156 1
        return $container->get(HttpFactoryInterface::class);
157
    }
158
159
    /**
160
     * @param ContainerInterface $container The container used to resolve dependencies
161
     * @return StreamFactoryInterface
162
     */
163 11
    public function getStreamFactory(ContainerInterface $container): StreamFactoryInterface
164
    {
165 11
        return $container->get(HttpFactoryInterface::class);
166
    }
167
168
    /**
169
     * @param ContainerInterface $container The container used to resolve dependencies
170
     * @return UploadedFileFactoryInterface
171
     */
172 1
    public function getUploadedFileFactory(ContainerInterface $container): UploadedFileFactoryInterface
173
    {
174 1
        return $container->get(HttpFactoryInterface::class);
175
    }
176
177
    /**
178
     * @param ContainerInterface $container The container used to resolve dependencies
179
     * @return UriFactoryInterface
180
     */
181 1
    public function getUriFactory(ContainerInterface $container): UriFactoryInterface
182
    {
183 1
        return $container->get(HttpFactoryInterface::class);
184
    }
185
}
186