Passed
Pull Request — master (#257)
by Wilmer
15:17
created

MockRequestHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 4
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests\Middleware\Mock;
5
6
use Nyholm\Psr7\Response;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class MockRequestHandler implements RequestHandlerInterface
12
{
13
    /**
14
     * @var ServerRequestInterface
15
     */
16
    public $processedRequest;
17
18
    /**
19
     * @var int
20
     */
21
    private $responseStatus;
22
23
    /**
24
     * @var \Throwable|null
25
     */
26
    private $handleException;
27
28
    public function __construct(int $responseStatus = 200)
29
    {
30
        $this->responseStatus = $responseStatus;
31
    }
32
33
    /**
34
     * @return static
35
     */
36
    public function setHandleExcaption(?\Throwable $throwable)
37
    {
38
        $this->handleException = $throwable;
39
        return $this;
40
    }
41
42
    public function handle(ServerRequestInterface $request): ResponseInterface
43
    {
44
        if ($this->handleException !== null) {
45
            throw $this->handleException;
46
        }
47
        $this->processedRequest = $request;
48
        return new Response($this->responseStatus);
49
    }
50
}
51