Response_Factory::success()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Creates the responses used for ajax calls.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Ajax
23
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
24
 */
25
26
namespace PinkCrab\Ajax\Dispatcher;
27
28
use Nyholm\Psr7\Stream;
29
use PinkCrab\HTTP\HTTP;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ResponseFactoryInterface;
32
33
class Response_Factory implements ResponseFactoryInterface {
34
35
	protected HTTP $http;
36
37
	public function __construct( HTTP $http ) {
38
		$this->http = $http;
39
	}
40
41
	/**
42
	 * Create a new response.
43
	 *
44
	 * @param int $code The HTTP status code. Defaults to 200.
45
	 * @param string $reasonPhrase
46
	 */
47
	public function createResponse( int $code = 200, string $reasonPhrase = '' ): ResponseInterface {
48
		return $this->http->psr7_response()
49
			->withStatus( $code )
50
			->withHeader( 'Content-Type', 'application/json; charset=' . get_option( 'blog_charset' ) )
0 ignored issues
show
Bug introduced by
Are you sure get_option('blog_charset') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

50
			->withHeader( 'Content-Type', 'application/json; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ) )
Loading history...
51
			->withBody( Stream::create( $reasonPhrase ) );
52
	}
53
54
	/**
55
	 * Return a 200 response, with the passed payload
56
	 *
57
	 * @param array<mixed> $payload
58
	 * @return ResponseInterface
59
	 */
60
	public function success( array $payload = array() ): ResponseInterface {
61
		return $this->createResponse( 200, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
62
	}
63
64
	/**
65
	 * Returns a 401 response, with an optional payload.
66
	 * Defaults to [ 'error' => 'unauthorised' ]
67
	 *
68
	 * @param array<mixed> $payload
69
	 * @return ResponseInterface
70
	 */
71
	public function unauthorised( array $payload = array( 'error' => 'unauthorised' ) ): ResponseInterface {
72
		return $this->createResponse( 401, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
73
	}
74
75
	/**
76
	 * Returns a 500 response, with an optional payload.
77
	 * Defaults to [ 'error' => 'error' ]
78
	 *
79
	 * @param array<mixed> $payload
80
	 * @return ResponseInterface
81
	 */
82
	public function failure( array $payload = array( 'error' => 'error' ) ): ResponseInterface {
83
		return $this->createResponse( 500, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
84
	}
85
86
	/**
87
	 * Returns a 404 response, with an optional payload.
88
	 * Defaults to [ 'error' => 'not found' ]
89
	 *
90
	 * @param array<mixed> $payload
91
	 * @return ResponseInterface
92
	 */
93
	public function not_found( array $payload = array( 'error' => 'not found' ) ): ResponseInterface {
94
		return $this->createResponse( 404, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
95
	}
96
}
97