Redirect   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 98
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 3
A permanent() 0 5 1
A toRoute() 0 6 1
A __construct() 0 4 1
A withStatus() 0 5 1
A toUrl() 0 5 1
A temporary() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Middleware;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use RuntimeException;
13
use Yiisoft\Http\Status;
14
use Yiisoft\Router\UrlGeneratorInterface;
15
16
/**
17
 * This middleware generates and adds a `Location` header to the response.
18
 */
19
final class Redirect implements MiddlewareInterface
20
{
21
    private ?string $uri = null;
22
    private ?string $route = null;
23
    /**
24
     * @var array<string, scalar|\Stringable|null> $parameters
25
     */
26
    private array $parameters = [];
27
    private int $statusCode = Status::MOVED_PERMANENTLY;
28
29 7
    public function __construct(
30
        private ResponseFactoryInterface $responseFactory,
31
        private UrlGeneratorInterface $urlGenerator,
32
    ) {
33 7
    }
34
35
    /**
36
     * Returns a new instance with the specified URL for redirection.
37
     *
38
     * @param string $url URL for redirection.
39
     */
40 2
    public function toUrl(string $url): self
41
    {
42 2
        $new = clone $this;
43 2
        $new->uri = $url;
44 2
        return $new;
45
    }
46
47
    /**
48
     * Returns a new instance with the specified route data for redirection.
49
     *
50
     * If you've set a redirect URL with {@see toUrl()}, the middleware ignores the route data, since the URL
51
     * is a priority.
52
     *
53
     * @param string $name The route name for redirection.
54
     * @param array<string, scalar|\Stringable|null> $parameters $parameters The route parameters for redirection.
55
     */
56 5
    public function toRoute(string $name, array $parameters = []): self
57
    {
58 5
        $new = clone $this;
59 5
        $new->route = $name;
60 5
        $new->parameters = $parameters;
61 5
        return $new;
62
    }
63
64
    /**
65
     * Returns a new instance with the specified status code of the response for redirection.
66
     *
67
     * @param int $statusCode The status code of the response for redirection.
68
     */
69 2
    public function withStatus(int $statusCode): self
70
    {
71 2
        $new = clone $this;
72 2
        $new->statusCode = $statusCode;
73 2
        return $new;
74
    }
75
76
    /**
77
     * Returns a new instance with the response status code of permanent redirection.
78
     *
79
     * @see Status::MOVED_PERMANENTLY
80
     */
81 2
    public function permanent(): self
82
    {
83 2
        $new = clone $this;
84 2
        $new->statusCode = Status::MOVED_PERMANENTLY;
85 2
        return $new;
86
    }
87
88
    /**
89
     * Returns a new instance with the response status code of temporary redirection.
90
     *
91
     * @see Status::FOUND
92
     */
93 2
    public function temporary(): self
94
    {
95 2
        $new = clone $this;
96 2
        $new->statusCode = Status::FOUND;
97 2
        return $new;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     *
103
     * @throws RuntimeException If the data for redirection wasn't set earlier.
104
     */
105 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
106
    {
107 6
        if ($this->route === null && $this->uri === null) {
108 1
            throw new RuntimeException('Either `toUrl()` or `toRoute()` method should be used.');
109
        }
110
111
        /** @psalm-suppress PossiblyNullArgument */
112 5
        $uri = $this->uri ?? $this->urlGenerator->generate($this->route, $this->parameters);
0 ignored issues
show
Bug introduced by
It seems like $this->route can also be of type null; however, parameter $name of Yiisoft\Router\UrlGeneratorInterface::generate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        $uri = $this->uri ?? $this->urlGenerator->generate(/** @scrutinizer ignore-type */ $this->route, $this->parameters);
Loading history...
113
114 5
        return $this->responseFactory
115 5
            ->createResponse($this->statusCode)
116 5
            ->withAddedHeader('Location', $uri);
117
    }
118
}
119