-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFetchResponse.php
More file actions
127 lines (111 loc) · 3.08 KB
/
FetchResponse.php
File metadata and controls
127 lines (111 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace ConfigCat;
use ConfigCat\Cache\ConfigEntry;
use Throwable;
/**
* Represents a fetch response, including its state and body.
*
* @internal
*/
final class FetchResponse
{
/** @var int */
public const FETCHED = 0;
/** @var int */
public const NOT_MODIFIED = 1;
/** @var int */
public const FAILED = 3;
private function __construct(
private readonly int $status,
private readonly ConfigEntry $cacheEntry,
private readonly ?string $errorMessage = null,
private readonly ?Throwable $errorException = null
) {}
/**
* Creates a new response with FAILED status.
*
* @param string $errorMessage the reason of the failure
* @param ?Throwable $errorException the related error exception (if any)
*
* @return FetchResponse the response
*/
public static function failure(string $errorMessage, ?Throwable $errorException = null): FetchResponse
{
return new FetchResponse(self::FAILED, ConfigEntry::empty(), $errorMessage, $errorException);
}
/**
* Creates a new response with NOT_MODIFIED status.
*
* @return FetchResponse the response
*/
public static function notModified(): FetchResponse
{
return new FetchResponse(self::NOT_MODIFIED, ConfigEntry::empty());
}
/**
* Creates a new response with FETCHED status.
*
* @param ConfigEntry $entry the produced config entry
*
* @return FetchResponse the response
*/
public static function success(ConfigEntry $entry): FetchResponse
{
return new FetchResponse(self::FETCHED, $entry);
}
/**
* Returns true when the response is in fetched state.
*
* @return bool true if the fetch succeeded, otherwise false
*/
public function isFetched(): bool
{
return self::FETCHED === $this->status;
}
/**
* Returns true when the response is in not modified state.
*
* @return bool true if the fetched configurations was not modified, otherwise false
*/
public function isNotModified(): bool
{
return self::NOT_MODIFIED === $this->status;
}
/**
* Returns true when the response is in failed state.
*
* @return bool true if the fetch failed, otherwise false
*/
public function isFailed(): bool
{
return self::FAILED === $this->status;
}
/**
* Returns the produced config entry.
*
* @return ConfigEntry the produced config entry
*/
public function getConfigEntry(): ConfigEntry
{
return $this->cacheEntry;
}
/**
* Returns the error message if the fetch failed.
*
* @return ?string the error message
*/
public function getErrorMessage(): ?string
{
return $this->errorMessage;
}
/**
* Returns the `Throwable` object related to the error in case of failure (if any).
*
* @return ?Throwable the error exception
*/
public function getErrorException(): ?Throwable
{
return $this->errorException;
}
}