Skip to content

Commit aead210

Browse files
committed
rename Client to CookieClientDecorator; update dev deps
1 parent 5c3ae9e commit aead210

10 files changed

+28
-78
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Cookie extension for PSR-18 HTTP client.
1212
Install this package and your favorite [psr-18 implementation](https://packagist.org/providers/psr/http-client-implementation).
1313

1414
```bash
15-
composer require webclient/ext-cookie:^1.0
15+
composer require webclient/ext-cookie:^2.0
1616
```
1717

1818
# Using
@@ -22,14 +22,14 @@ composer require webclient/ext-cookie:^1.0
2222

2323
use Psr\Http\Client\ClientInterface;
2424
use Psr\Http\Message\RequestInterface;
25-
use Webclient\Extension\Cookie\Client;
25+
use Webclient\Extension\Cookie\CookieClientDecorator;
2626
use Webclient\Extension\Cookie\Cookie\Storage;
2727

2828
/**
2929
* @var ClientInterface $client Your PSR-18 HTTP Client
3030
* @var Storage $storage Cookies storage. You may extends this class for implements your storage
3131
*/
32-
$http = new Client($client, $storage);
32+
$http = new CookieClientDecorator($client, $storage);
3333

3434
/** @var RequestInterface $request */
3535
$response = $http->sendRequest($request);

Diff for: composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=7.0",
15+
"php": "^7.4 || ^8.0",
1616
"ext-json": "*",
1717
"ext-mbstring": "*",
1818
"psr/http-client": "^1.0"
1919
},
2020
"require-dev": {
21-
"guzzlehttp/psr7": "^1.7",
22-
"phpunit/phpunit": ">=6.5",
23-
"squizlabs/php_codesniffer": "^3.5",
24-
"webclient/fake-http-client": "^1.0"
21+
"nyholm/psr7": "^1.5",
22+
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.5",
23+
"squizlabs/php_codesniffer": "^3.7",
24+
"webclient/fake-http-client": "^2.0"
2525
},
2626
"provide": {
2727
"psr/http-client-implementation": "1.0"

Diff for: phpcs.xml.dist

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
<arg name="colors"/>
99

1010
<!-- inherit rules from: -->
11-
<rule ref="PSR12">
12-
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
13-
</rule>
11+
<rule ref="PSR12"/>
1412

1513
<!-- Paths to check -->
1614
<file>src</file>

Diff for: src/Cookie/ArrayStorage.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
final class ArrayStorage extends Storage
88
{
9-
109
public function save()
1110
{
1211
}

Diff for: src/Cookie/NetscapeCookieFile.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525

2626
final class NetscapeCookieFile extends Storage
2727
{
28-
29-
/**
30-
* @var string
31-
*/
32-
private $file;
28+
private string $file;
3329

3430
public function __construct(string $file)
3531
{

Diff for: src/Cookie/Storage.php

+2-23
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,10 @@
66

77
use Psr\Http\Message\UriInterface;
88

9-
use function array_key_exists;
10-
use function ltrim;
11-
use function mb_strlen;
12-
use function mb_strpos;
13-
use function mb_strtolower;
14-
use function mb_substr;
15-
use function rtrim;
16-
use function str_pad;
17-
use function time;
18-
use function trim;
19-
209
abstract class Storage
2110
{
11+
private array $cookies = [];
2212

23-
/**
24-
* @var array
25-
*/
26-
private $cookies = [];
27-
28-
/**
29-
* @inheritDoc
30-
*/
3113
final public function get(UriInterface $uri): array
3214
{
3315
$cookies = [];
@@ -62,9 +44,6 @@ final public function get(UriInterface $uri): array
6244
return $cookies;
6345
}
6446

65-
/**
66-
* @inheritDoc
67-
*/
6847
final public function set(
6948
string $name,
7049
string $value,
@@ -97,7 +76,7 @@ final public function set(
9776
}
9877
$this->cookies[$scheme][$domain][$name] = [
9978
'value' => $value,
100-
'expired' => $expire > 0 ? $expire : 0,
79+
'expired' => max($expire, 0),
10180
'path' => '/' . ltrim($path, '/'),
10281
];
10382
}

Diff for: src/Client.php renamed to src/CookieClientDecorator.php

+5-19
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,10 @@
99
use Psr\Http\Message\ResponseInterface;
1010
use Webclient\Extension\Cookie\Cookie\Storage;
1111

12-
use function array_replace;
13-
use function explode;
14-
use function strtolower;
15-
use function strtotime;
16-
use function trim;
17-
18-
final class Client implements ClientInterface
12+
final class CookieClientDecorator implements ClientInterface
1913
{
20-
21-
/**
22-
* @var ClientInterface
23-
*/
24-
private $client;
25-
26-
/**
27-
* @var Storage
28-
*/
29-
private $storage;
14+
private ClientInterface $client;
15+
private Storage $storage;
3016

3117
public function __construct(ClientInterface $client, Storage $storage)
3218
{
@@ -94,14 +80,14 @@ private function setCookieFromHeader(string $header, string $domain)
9480
}
9581
break;
9682
case 'path':
97-
$path = $value ? $value : '/';
83+
$path = $value ?: '/';
9884
break;
9985
case 'secure':
10086
$secure = true;
10187
break;
10288
case 'expires':
10389
$timestamp = strtotime($value);
104-
$expires = $timestamp > 0 ? $timestamp : 0;
90+
$expires = max($timestamp, 0);
10591
break;
10692
case 'httponly':
10793
break;

Diff for: stuff/Handler.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
namespace Stuff\Webclient\Extension\Cookie;
66

7-
use GuzzleHttp\Psr7\Response;
7+
use Nyholm\Psr7\Response;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Psr\Http\Server\RequestHandlerInterface;
1111

12-
use function array_key_exists;
13-
use function date;
14-
use function implode;
15-
use function is_array;
16-
use function time;
17-
1812
class Handler implements RequestHandlerInterface
1913
{
20-
2114
/**
2215
* @param ServerRequestInterface $request
2316
* @return ResponseInterface

Diff for: stuff/Storage.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class Storage extends BaseStorage
1010
{
11-
1211
public function getItems(): array
1312
{
1413
$result = [];

Diff for: tests/CookieClientTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
namespace Tests\Webclient\Extension\Cookie;
66

7-
use GuzzleHttp\Psr7\Request;
7+
use Nyholm\Psr7\Request;
88
use PHPUnit\Framework\TestCase;
99
use Psr\Http\Client\ClientExceptionInterface;
1010
use Stuff\Webclient\Extension\Cookie\Handler;
1111
use Stuff\Webclient\Extension\Cookie\Storage;
12-
use Webclient\Extension\Cookie\Client;
13-
use Webclient\Fake\Client as FakeClient;
12+
use Webclient\Extension\Cookie\CookieClientDecorator;
13+
use Webclient\Fake\FakeHttpClient;
1414

1515
class CookieClientTest extends TestCase
1616
{
17-
1817
/**
1918
* @param bool $secure
2019
* @param string $host
@@ -29,7 +28,7 @@ public function testSetToStorage(bool $secure, string $host, string $path)
2928

3029
$storage = new Storage();
3130

32-
$client = new Client(new FakeClient(new Handler()), $storage);
31+
$client = new CookieClientDecorator(new FakeHttpClient(new Handler()), $storage);
3332

3433
$set = [
3534
'cookie' => [
@@ -44,13 +43,15 @@ public function testSetToStorage(bool $secure, string $host, string $path)
4443
$request = new Request('GET', $uri, ['Accept' => 'text/plain']);
4544
$client->sendRequest($request);
4645

47-
$path = $path ? $path : '/';
46+
if ($path === '') {
47+
$path = '/';
48+
}
4849
$cookies = $storage->getItems();
4950
foreach ($set['cookie'] as $name => $value) {
5051
$this->assertArrayHasKey($name, $cookies);
5152
$domain = in_array($name, $set['subdomain']) ? '.' . $host : $host;
5253
$temp = in_array($name, $set['temp']);
53-
$this->assertStoredCookie($cookies[$name], $value, $domain, $path, true, $secure, $temp);
54+
$this->assertStoredCookie($cookies[$name], $value, $domain, $path, $secure, $temp);
5455
}
5556
}
5657

@@ -129,7 +130,7 @@ public function testGetFromStorage(string $uri, array $expected)
129130
}
130131

131132

132-
$client = new Client(new FakeClient(new Handler()), $storage);
133+
$client = new CookieClientDecorator(new FakeHttpClient(new Handler()), $storage);
133134
$request = new Request('GET', $uri, ['Accept' => 'text/plain']);
134135
$response = $client->sendRequest($request);
135136
$json = $response->getBody()->__toString();
@@ -166,14 +167,13 @@ private function assertStoredCookie(
166167
string $value,
167168
string $domain,
168169
string $path,
169-
bool $httpOnly,
170170
bool $secure,
171171
bool $temporary
172172
) {
173173
$this->assertSame($value, $cookie['value']);
174174
$this->assertSame($domain, $cookie['domain']);
175175
$this->assertSame($path, $cookie['path']);
176-
$this->assertSame($httpOnly, $cookie['httpOnly']);
176+
$this->assertSame(true, $cookie['httpOnly']);
177177
$this->assertSame($secure, $cookie['secure']);
178178
$this->assertSame($temporary, $cookie['expired'] === 0);
179179
}

0 commit comments

Comments
 (0)