Passed
Push — master ( 74a67e...bf1276 )
by Atanas
01:58
created

RedirectResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
ccs 9
cts 12
cp 0.75
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A to() 0 3 1
A back() 0 13 3
1
<?php
2
3
namespace WPEmerge\Responses;
4
5
use GuzzleHttp\Psr7;
6
use GuzzleHttp\Psr7\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WPEmerge\Responses\Response.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Psr\Http\Message\ResponseInterface;
8
use WPEmerge\Requests\Request;
9
10
/**
11
 * A collection of tools for the creation of responses
12
 */
13
class RedirectResponse extends Response {
14
	/**
15
	 * Current request.
16
	 *
17
	 * @var Request
18
	 */
19
	protected $request = null;
20
21
	/**
22
	 * Constructor.
23
	 *
24
	 * @param Request $request
25
	 */
26
	public function __construct( Request $request ) {
27
		$this->request = $request;
28
	}
29
30
	/**
31
	 * Get a response redirecting to a specific url.
32
	 *
33
	 * @param  string            $url
34
	 * @param  integer           $status
35
	 * @return ResponseInterface
36
	 */
37 2
	public function to( $url, $status = 302 ) {
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
38 2
		return $this->withHeader( 'Location', $url )->withStatus( $status );
39
	}
40
41
	/**
42
	 * Get a response redirecting back to the referrer or a fallback.
43
	 *
44
	 * @param  string            $fallback
45
	 * @param  integer           $status
46
	 * @return ResponseInterface
47
	 */
48 4
	public function back( $fallback = '', $status = 302 ) {
49 4
		$url = $this->request->headers( 'Referer' );
50
51 4
		if ( empty( $url ) ) {
52 2
			$url = $fallback;
53
		}
54
55 4
		if ( empty( $url ) ) {
56 1
			$url = $this->request->getUrl();
57
		}
58
59 4
		return $this->to( $url, $status );
60
	}
61
}
62
63
64
65