Skip to content

Commit 017c87e

Browse files
Make HttpAdapter explicit dependency for PublicSuffixListManager (#200)
1 parent 4e8ae3a commit 017c87e

File tree

6 files changed

+25
-49
lines changed

6 files changed

+25
-49
lines changed

bin/update-psl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ if ($argc == 2 && is_dir($argv[1])) {
2121

2222
try {
2323
echo 'Updating Public Suffix List.' . PHP_EOL;
24-
$manager = new \Pdp\PublicSuffixListManager($cacheDir);
24+
/**
25+
* @TODO: Having the adapter baked in with no way for the library user to provide
26+
* their own is problematic. Fix it or leave it? No one has ever complained.
27+
*/
28+
$manager = new \Pdp\PublicSuffixListManager(new \Pdp\Http\CurlHttpAdapter(), $cacheDir);
2529
$manager->refreshPublicSuffixList();
2630
echo 'Update complete.' . PHP_EOL;
2731
exit(0);

src/HttpAdapter/CurlHttpAdapter.php renamed to src/Http/CurlHttpAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
declare(strict_types=1);
1111

12-
namespace Pdp\HttpAdapter;
12+
namespace Pdp\Http;
1313

1414
/**
1515
* cURL http adapter.
@@ -22,7 +22,7 @@
2222
* @author William Durand <[email protected]>
2323
* @author Jeremy Kendall <[email protected]>
2424
*/
25-
class CurlHttpAdapter implements HttpAdapterInterface
25+
class CurlHttpAdapter implements HttpAdapter
2626
{
2727
/**
2828
* {@inheritdoc}

src/HttpAdapter/HttpAdapterInterface.php renamed to src/Http/HttpAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
declare(strict_types=1);
1111

12-
namespace Pdp\HttpAdapter;
12+
namespace Pdp\Http;
1313

1414
/**
1515
* Interface for http adapters.
@@ -22,7 +22,7 @@
2222
* @author William Durand <[email protected]>
2323
* @author Jeremy Kendall <[email protected]>
2424
*/
25-
interface HttpAdapterInterface
25+
interface HttpAdapter
2626
{
2727
/**
2828
* Returns the content fetched from a given URL.

src/PublicSuffixListManager.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
namespace Pdp;
1313

1414
use Exception;
15-
use Pdp\HttpAdapter\CurlHttpAdapter;
16-
use Pdp\HttpAdapter\HttpAdapterInterface;
15+
use Pdp\Http\HttpAdapter;
1716
use SplFileObject;
1817

1918
/**
@@ -51,42 +50,22 @@ class PublicSuffixListManager
5150
private $cacheDir;
5251

5352
/**
54-
* @var HttpAdapterInterface Http adapter
53+
* @var HttpAdapter Http adapter
5554
*/
5655
private $httpAdapter;
5756

5857
/**
5958
* Public constructor.
6059
*
61-
* @param string $cacheDir Optional cache directory
60+
* @param HttpAdapter $httpAdapter
61+
* @param string $cacheDir Optional cache directory
6262
*/
63-
public function __construct(string $cacheDir = null)
63+
public function __construct(HttpAdapter $httpAdapter, string $cacheDir = null)
6464
{
6565
$this->cacheDir = $cacheDir ?? realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data');
66-
}
67-
68-
/**
69-
* Sets http adapter.
70-
*
71-
* @param HttpAdapterInterface $httpAdapter
72-
*/
73-
public function setHttpAdapter(HttpAdapterInterface $httpAdapter)
74-
{
7566
$this->httpAdapter = $httpAdapter;
7667
}
7768

78-
/**
79-
* Returns http adapter. Returns default http adapter if one is not set.
80-
*
81-
* @return HttpAdapterInterface
82-
*/
83-
public function getHttpAdapter(): HttpAdapterInterface
84-
{
85-
$this->httpAdapter = $this->httpAdapter ?? new CurlHttpAdapter();
86-
87-
return $this->httpAdapter;
88-
}
89-
9069
/**
9170
* Gets Public Suffix List.
9271
*
@@ -111,7 +90,7 @@ public function getList($list = self::ALL_DOMAINS): PublicSuffixList
11190
*/
11291
public function refreshPublicSuffixList()
11392
{
114-
$publicSuffixList = $this->getHttpAdapter()->getContent(self::PUBLIC_SUFFIX_LIST_URL);
93+
$publicSuffixList = $this->httpAdapter->getContent(self::PUBLIC_SUFFIX_LIST_URL);
11594
$this->cache(self::PDP_PSL_TEXT_FILE, $publicSuffixList);
11695

11796
$publicSuffixListArray = $this->convertListToArray();

tests/HttpAdapter/CurlHttpAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pdp\Tests\HttpAdapter;
44

5-
use Pdp\HttpAdapter\CurlHttpAdapter;
5+
use Pdp\Http\CurlHttpAdapter;
66
use PHPUnit\Framework\TestCase;
77

88
/**

tests/PublicSuffixListManagerTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Exception;
88
use org\bovigo\vfs\vfsStream;
99
use org\bovigo\vfs\vfsStreamDirectory;
10-
use Pdp\HttpAdapter\CurlHttpAdapter;
11-
use Pdp\HttpAdapter\HttpAdapterInterface;
10+
use Pdp\Http\CurlHttpAdapter;
11+
use Pdp\Http\HttpAdapter;
1212
use Pdp\PublicSuffixList;
1313
use Pdp\PublicSuffixListManager;
1414
use PHPUnit\Framework\TestCase;
@@ -46,7 +46,7 @@ class PublicSuffixListManagerTest extends TestCase
4646
protected $dataDir;
4747

4848
/**
49-
* @var HttpAdapterInterface|\PHPUnit_Framework_MockObject_MockObject Http adapter
49+
* @var HttpAdapter|\PHPUnit_Framework_MockObject_MockObject Http adapter
5050
*/
5151
protected $httpAdapter;
5252

@@ -60,10 +60,9 @@ protected function setUp()
6060
vfsStream::create(['cache' => []], $this->root);
6161
$this->cacheDir = vfsStream::url('pdp/cache');
6262

63-
$this->listManager = new PublicSuffixListManager($this->cacheDir);
63+
$this->httpAdapter = $this->getMock(HttpAdapter::class);
6464

65-
$this->httpAdapter = $this->getMock(HttpAdapterInterface::class);
66-
$this->listManager->setHttpAdapter($this->httpAdapter);
65+
$this->listManager = new PublicSuffixListManager($this->httpAdapter, $this->cacheDir);
6766
}
6867

6968
protected function tearDown()
@@ -104,17 +103,11 @@ public function testRefreshPublicSuffixList()
104103
);
105104
}
106105

107-
public function testGetHttpAdapterReturnsDefaultCurlAdapterIfAdapterNotSet()
108-
{
109-
$listManager = new PublicSuffixListManager($this->cacheDir);
110-
$this->assertInstanceOf(CurlHttpAdapter::class, $listManager->getHttpAdapter());
111-
}
112-
113106
public function testWriteThrowsExceptionIfCanNotWrite()
114107
{
115108
$this->expectException(Exception::class);
116109
$this->expectExceptionMessage("Cannot write '/does/not/exist/public-suffix-list.txt'");
117-
$manager = new PublicSuffixListManager('/does/not/exist');
110+
$manager = new PublicSuffixListManager(new CurlHttpAdapter(), '/does/not/exist');
118111
$manager->refreshPublicSuffixList();
119112
}
120113

@@ -142,7 +135,7 @@ public function testGetListWithoutCache()
142135

143136
/** @var PublicSuffixListManager|\PHPUnit_Framework_MockObject_MockObject $listManager */
144137
$listManager = $this->getMockBuilder(PublicSuffixListManager::class)
145-
->setConstructorArgs([$this->cacheDir])
138+
->setConstructorArgs([$this->httpAdapter, $this->cacheDir])
146139
->setMethods(['refreshPublicSuffixList'])
147140
->getMock();
148141

@@ -162,7 +155,7 @@ public function testGetListWithoutCache()
162155
public function testGetProvidedListFromDefaultCacheDir()
163156
{
164157
// By not providing cache I'm forcing use of default cache dir
165-
$listManager = new PublicSuffixListManager();
158+
$listManager = new PublicSuffixListManager($this->httpAdapter);
166159
$publicSuffixList = $listManager->getList();
167160
$this->assertGreaterThanOrEqual(300, count($publicSuffixList->getRules()));
168161
}
@@ -176,7 +169,7 @@ private function getMock(string $class)
176169

177170
public function testGetDifferentPublicList()
178171
{
179-
$listManager = new PublicSuffixListManager();
172+
$listManager = new PublicSuffixListManager($this->httpAdapter);
180173
$publicList = $listManager->getList();
181174
$invalidList = $listManager->getList('invalid type');
182175
$this->assertEquals($publicList, $invalidList);

0 commit comments

Comments
 (0)