3 use Psr\Log\LoggerInterface;
56 if ( empty(
$params[
'url'] ) ) {
57 throw new InvalidArgumentException(
'URL parameter is required' );
59 if ( empty(
$params[
'client'] ) ) {
65 foreach ( [
'caBundlePath',
'proxy' ]
as $key ) {
67 $clientParams[$key] =
$params[$key];
72 $this->client =
$params[
'client'];
77 $this->url = rtrim(
$params[
'url'],
'/' ) .
'/';
84 $this->client->setLogger(
$logger );
92 protected function doGet( $key, $flags = 0 ) {
95 'url' => $this->url . rawurlencode( $key ),
98 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
99 if ( $rcode === 200 ) {
100 if ( is_string( $rbody ) ) {
105 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
106 return $this->
handleError(
"Failed to fetch $key", $rcode, $rerr );
119 $this->logger->error(
"$msg : ({code}) {error}", [
123 $this->
setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
136 public function set( $key,
$value, $exptime = 0, $flags = 0 ) {
139 'url' => $this->url . rawurlencode( $key ),
142 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
143 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
146 return $this->
handleError(
"Failed to store $key", $rcode, $rerr );
155 public function delete( $key ) {
157 'method' =>
'DELETE',
158 'url' => $this->url . rawurlencode( $key ),
160 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
161 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
164 return $this->
handleError(
"Failed to delete $key", $rcode, $rerr );