Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

Http::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 4
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Http;
13
14
use Psr\Container\ContainerInterface;
15
use Psr\Http\Message\ResponseFactoryInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use Spiral\Http\Config\HttpConfig;
20
use Spiral\Http\Exception\HttpException;
21
22
final class Http implements RequestHandlerInterface
23
{
24
    /** @var HttpConfig */
25
    protected $config;
26
27
    /** @var Pipeline */
28
    protected $pipeline;
29
30
    /** @var ResponseFactoryInterface */
31
    protected $responseFactory;
32
33
    /** @var ContainerInterface */
34
    protected $container;
35
36
    /** @var RequestHandlerInterface */
37
    protected $handler;
38
39
    /**
40
     * @param HttpConfig               $config
41
     * @param Pipeline                 $pipeline
42
     * @param ResponseFactoryInterface $responseFactory
43
     * @param ContainerInterface       $container
44
     */
45
    public function __construct(
46
        HttpConfig $config,
47
        Pipeline $pipeline,
48
        ResponseFactoryInterface $responseFactory,
49
        ContainerInterface $container
50
    ) {
51
        $this->config = $config;
52
        $this->pipeline = $pipeline;
53
        $this->responseFactory = $responseFactory;
54
        $this->container = $container;
55
56
        foreach ($this->config->getMiddleware() as $middleware) {
57
            $this->pipeline->pushMiddleware($this->container->get($middleware));
58
        }
59
    }
60
61
    /**
62
     * @return Pipeline
63
     */
64
    public function getPipeline(): Pipeline
65
    {
66
        return $this->pipeline;
67
    }
68
69
    /**
70
     * @param RequestHandlerInterface|callable $handler
71
     * @return Http
72
     */
73
    public function setHandler($handler): self
74
    {
75
        if ($handler instanceof RequestHandlerInterface) {
76
            $this->handler = $handler;
77
        } elseif (is_callable($handler)) {
78
            $this->handler = new CallableHandler($handler, $this->responseFactory);
79
        } else {
80
            throw new HttpException(
81
                'Invalid handler is given, expects callable or RequestHandlerInterface.'
82
            );
83
        }
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param ServerRequestInterface $request
90
     * @return ResponseInterface
91
     *
92
     * @throws HttpException
93
     */
94
    public function handle(ServerRequestInterface $request): ResponseInterface
95
    {
96
        if (empty($this->handler)) {
97
            throw new HttpException('Unable to run HttpCore, no handler is set.');
98
        }
99
100
        return $this->pipeline->withHandler($this->handler)->handle($request);
101
    }
102
}
103