Skip to content

Commit f2a6212

Browse files
committed
change methods return type
1 parent 44c021a commit f2a6212

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

data/pdp-PSL_FULL_5a3cc7f81795bb2e48e848af42d287b4.cache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Installer.php

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
use function file_exists;
2525
use function filter_var_array;
2626
use function fwrite;
27+
use function implode;
2728
use const FILTER_FLAG_STRIP_LOW;
2829
use const FILTER_SANITIZE_STRING;
2930
use const FILTER_VALIDATE_BOOLEAN;
31+
use const PHP_EOL;
3032
use const STDERR;
33+
use const STDOUT;
3134

3235
/**
3336
* A class to install and update local cache.
@@ -78,7 +81,7 @@ public static function createFromCacheDir(LoggerInterface $logger, string $cache
7881
return new self(new Manager(new Cache($cacheDir), new CurlHttpClient()), $logger);
7982
}
8083

81-
public function refresh(array $context = []): int
84+
public function refresh(array $context = []): bool
8285
{
8386
$context = filter_var_array(array_replace(self::DEFAULT_CONTEXT, $context), [
8487
self::REFRESH_PSL_KEY => FILTER_VALIDATE_BOOLEAN,
@@ -94,16 +97,14 @@ public function refresh(array $context = []): int
9497
}
9598

9699
try {
97-
$retVal = $this->execute($context);
100+
return $this->execute($context);
98101
} catch (PsrCacheException $exception) {
99102
$this->logger->error('Local cache update failed with {exception}', ['exception' => $exception->getMessage()]);
100-
$retVal = 1;
103+
return false;
101104
} catch (Throwable $exception) {
102105
$this->logger->error('Local cache update failed with {exception}', ['exception' => $exception->getMessage()]);
103-
$retVal = 1;
106+
return false;
104107
}
105-
106-
return $retVal;
107108
}
108109

109110
/**
@@ -113,7 +114,7 @@ public function refresh(array $context = []): int
113114
*
114115
* @throws PsrCacheException
115116
*/
116-
private function execute(array $arguments = []): int
117+
private function execute(array $arguments = []): bool
117118
{
118119
if ($arguments[self::REFRESH_PSL_KEY]) {
119120
if (!$this->manager->refreshRules($arguments[self::REFRESH_PSL_URL_KEY], $arguments[self::TTL_KEY])) {
@@ -122,7 +123,7 @@ private function execute(array $arguments = []): int
122123
'ttl' => $arguments[self::TTL_KEY],
123124
]);
124125

125-
return 1;
126+
return false;
126127
}
127128

128129
$this->logger->info('Public Suffix List Cache updated for {ttl} using {psl_url}', [
@@ -132,7 +133,7 @@ private function execute(array $arguments = []): int
132133
}
133134

134135
if (!$arguments[self::REFRESH_RZD_KEY]) {
135-
return 0;
136+
return true;
136137
}
137138

138139
$this->logger->info('Updating your IANA Root Zone Database copy.');
@@ -142,15 +143,15 @@ private function execute(array $arguments = []): int
142143
'ttl' => $arguments[self::TTL_KEY],
143144
]);
144145

145-
return 0;
146+
return true;
146147
}
147148

148149
$this->logger->error('Unable to update the IANA Root Zone Database Cache using {rzd_url} with a TTL of {ttl}', [
149150
'rzd_url' => $arguments[self::REFRESH_RZD_URL_KEY],
150151
'ttl' => $arguments[self::TTL_KEY],
151152
]);
152153

153-
return 1;
154+
return false;
154155
}
155156

156157
/**
@@ -160,18 +161,18 @@ private function execute(array $arguments = []): int
160161
*/
161162
public static function updateLocalCache(Event $event = null)
162163
{
164+
$io = self::getIO($event);
163165
if (!extension_loaded('curl')) {
164-
fwrite(STDERR, 'The PHP cURL extension is missing.');
165-
166+
$io->writeError('The PHP cURL extension is missing.');
166167
die(1);
167168
}
168169

169170
$vendor = self::getVendorPath($event);
170171
if (null === $vendor) {
171-
fwrite(STDERR, implode(PHP_EOL, [
172+
$io->writeError([
172173
'You must set up the project dependencies using composer',
173174
'see https://getcomposer.org',
174-
]).PHP_EOL);
175+
]);
175176
die(1);
176177
}
177178

@@ -191,13 +192,41 @@ public static function updateLocalCache(Event $event = null)
191192
$logger = new Logger();
192193
$installer = self::createFromCacheDir($logger, $arguments[Installer::CACHE_DIR_KEY]);
193194
$logger->info('Updating your Pdp local cache.');
194-
$retVal = $installer->refresh($arguments);
195-
if (0 === $retVal) {
195+
if ($installer->refresh($arguments)) {
196196
$logger->info('Pdp local cache successfully updated.');
197-
} else {
198-
$logger->error('The command failed to update Pdp local cache successfully.');
197+
die(0);
199198
}
200-
die($retVal);
199+
200+
$logger->error('The command failed to update Pdp local cache.');
201+
die(1);
202+
}
203+
204+
/**
205+
* Detect the I/O interface to use.
206+
*
207+
* @param Event|null $event
208+
*
209+
* @return mixed
210+
*/
211+
private static function getIO(Event $event = null)
212+
{
213+
return null !== $event ? $event->getIO() : new class() {
214+
public function write($messages, bool $newline = true, int $verbosity = 2)
215+
{
216+
$this->doWrite($messages, $newline, false, $verbosity);
217+
}
218+
public function writeError($messages, bool $newline = true, int $verbosity = 2)
219+
{
220+
$this->doWrite($messages, $newline, true, $verbosity);
221+
}
222+
private function doWrite($messages, bool $newline, bool $stderr, int $verbosity)
223+
{
224+
fwrite(
225+
$stderr ? STDERR : STDOUT,
226+
implode($newline ? PHP_EOL : '', (array) $messages).PHP_EOL
227+
);
228+
}
229+
};
201230
}
202231

203232
/**

tests/InstallerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ public function tearDown()
9090
/**
9191
* @dataProvider contextDataProvider
9292
* @param array $context
93-
* @param int $retval
93+
* @param bool $retval
9494
* @param array $logs
9595
*/
96-
public function testRefreshDefault(array $context, int $retval, array $logs)
96+
public function testRefreshDefault(array $context, bool $retval, array $logs)
9797
{
9898
$manager = new Manager($this->cachePool, $this->client);
9999
$installer = new Installer($manager, $this->logger);
@@ -109,7 +109,7 @@ public function contextDataProvider(): array
109109
return [
110110
'default' => [
111111
'context' =>[],
112-
'retval' => 0,
112+
'retval' => true,
113113
'log' => [
114114
'Public Suffix List Cache updated for 1 DAY using '.Manager::PSL_URL,
115115
'IANA Root Zone Database Cache updated for 1 DAY using '.Manager::RZD_URL,
@@ -119,7 +119,7 @@ public function contextDataProvider(): array
119119
'context' => [
120120
Installer::REFRESH_PSL_KEY => true,
121121
],
122-
'retval' => 0,
122+
'retval' => true,
123123
'log' => [
124124
'Public Suffix List Cache updated for 1 DAY using '.Manager::PSL_URL,
125125
],
@@ -128,7 +128,7 @@ public function contextDataProvider(): array
128128
'context' => [
129129
Installer::REFRESH_RZD_KEY => true,
130130
],
131-
'retval' => 0,
131+
'retval' => true,
132132
'log' => [
133133
'IANA Root Zone Database Cache updated for 1 DAY using '.Manager::RZD_URL,
134134
],
@@ -138,7 +138,7 @@ public function contextDataProvider(): array
138138
Installer::REFRESH_PSL_KEY => true,
139139
Installer::REFRESH_PSL_URL_KEY => 'http://localhost/',
140140
],
141-
'retval' => 1,
141+
'retval' => false,
142142
'log' => [
143143
'Local cache update failed with invalid url: http://localhost/',
144144
],

0 commit comments

Comments
 (0)