Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Response
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 withRedirect
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
5 **/
6
7declare(strict_types=1);
8
9namespace BO\Slim;
10
11use Fig\Http\Message\StatusCodeInterface;
12use Psr\Http\Message\UriInterface;
13
14class Response extends \Slim\Psr7\Response
15{
16    /**
17     * This method prepares the response object to return an HTTP Redirect
18     * response to the client.
19     *
20     * @param  string|UriInterface $url    The redirect destination.
21     * @param  int|null            $status The redirect HTTP status code.
22     *
23     * @return static
24     */
25    public function withRedirect($url, int $status = null): Response
26    {
27        /** @var Response $redirectResponse */
28        $redirectResponse = $this->withHeader('Location', (string) $url);
29
30        if ($status !== null) {
31            return $redirectResponse->withStatus($status);
32        }
33
34        if ($this->getStatusCode() === StatusCodeInterface::STATUS_OK) {
35            return $redirectResponse->withStatus(StatusCodeInterface::STATUS_FOUND);
36        }
37
38        // return with a former defined status code
39        return $redirectResponse;
40    }
41}