PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / myyoast-client / domain / http-response.php

http-response.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.7, at src/myyoast-client/domain/http-response.php

124 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
4 namespace Yoast\WP\SEO\MyYoast_Client\Domain;
5
6 /**
7 * Immutable value object for an HTTP response returned by the OAuth client.
8 *
9 * Carries the HTTP status code (0 on transport failure), response headers,
10 * and parsed body. The body is either an associative array (for JSON
11 * responses) or a string (for everything else).
12 *
13 * phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- Response bodies are heterogeneous by design.
14 */
15 final class HTTP_Response {
16
17 /**
18 * HTTP status code. Zero indicates a transport-level failure
19 * (e.g. DNS, TLS, timeout) where no response was received.
20 *
21 * @var int
22 */
23 private $status;
24
25 /**
26 * Response headers, keyed by header name.
27 *
28 * @var array<string, string|string[]>
29 */
30 private $headers;
31
32 /**
33 * Parsed response body. An associative array for JSON responses,
34 * otherwise the raw body string.
35 *
36 * @var array<string, mixed>|string
37 */
38 private $body;
39
40 /**
41 * Constructs an HTTP_Response.
42 *
43 * @param int $status The HTTP status code.
44 * @param array<string, string|string[]> $headers The response headers.
45 * @param array<string, mixed>|string $body The parsed body.
46 */
47 public function __construct( int $status, array $headers, $body ) {
48 $this->status = $status;
49 $this->headers = $headers;
50 $this->body = $body;
51 }
52
53 /**
54 * Returns the HTTP status code.
55 *
56 * @return int The HTTP status code (0 on transport failure).
57 */
58 public function get_status(): int {
59 return $this->status;
60 }
61
62 /**
63 * Returns the response headers.
64 *
65 * @return array<string, string|string[]> The response headers.
66 */
67 public function get_headers(): array {
68 return $this->headers;
69 }
70
71 /**
72 * Returns the parsed response body.
73 *
74 * @return array<string, mixed>|string The parsed body.
75 */
76 public function get_body() {
77 return $this->body;
78 }
79
80 /**
81 * Whether the body is an associative array (decoded JSON).
82 *
83 * @return bool True if the body is an associative array.
84 */
85 public function has_array_body(): bool {
86 return \is_array( $this->body );
87 }
88
89 /**
90 * Returns a value from an array body by key, or a fallback when the
91 * body is not an array or the key is missing.
92 *
93 * @param string $key The key to look up.
94 * @param mixed $fallback The fallback value.
95 *
96 * @return mixed The looked-up value or the fallback.
97 */
98 public function get_body_value( string $key, $fallback = null ) {
99 if ( ! \is_array( $this->body ) ) {
100 return $fallback;
101 }
102
103 return ( $this->body[ $key ] ?? $fallback );
104 }
105
106 /**
107 * Whether the status indicates a successful response (2xx).
108 *
109 * @return bool True if the status is 2xx.
110 */
111 public function is_successful(): bool {
112 return ( $this->status >= 200 && $this->status < 300 );
113 }
114
115 /**
116 * Whether no response was received (transport failure).
117 *
118 * @return bool True if the status is 0.
119 */
120 public function is_transport_failure(): bool {
121 return ( $this->status === 0 );
122 }
123 }
124