Skip to content

Commit c7704e6

Browse files
committed
Improve Installer code
1 parent 4327ef3 commit c7704e6

File tree

2 files changed

+33
-52
lines changed

2 files changed

+33
-52
lines changed

src/Installer.php

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(Manager $manager, LoggerInterface $logger)
7373
}
7474

7575
/**
76-
* Creates a new installer instance with a Pdp\Cache.
76+
* Creates a new instance with a Pdp\Cache object and the cURL HTTP client.
7777
* @param LoggerInterface $logger
7878
* @param string $cacheDir
7979
*/
@@ -82,6 +82,10 @@ public static function createFromCacheDir(LoggerInterface $logger, string $cache
8282
return new self(new Manager(new Cache($cacheDir), new CurlHttpClient()), $logger);
8383
}
8484

85+
/**
86+
* Refresh the locale cache.
87+
* @param array $context
88+
*/
8589
public function refresh(array $context = []): bool
8690
{
8791
$context = filter_var_array(array_replace(self::DEFAULT_CONTEXT, $context), [
@@ -110,10 +114,7 @@ public function refresh(array $context = []): bool
110114

111115
/**
112116
* Refreshes the cache.
113-
*
114117
* @param array $arguments
115-
*
116-
* @throws PsrCacheException
117118
*/
118119
private function execute(array $arguments = []): bool
119120
{
@@ -157,8 +158,7 @@ private function execute(array $arguments = []): bool
157158

158159
/**
159160
* Script to update the local cache using composer hook.
160-
*
161-
* @param Event $event
161+
* @param null|Event $event
162162
*/
163163
public static function updateLocalCache(Event $event = null)
164164
{
@@ -181,6 +181,7 @@ public static function updateLocalCache(Event $event = null)
181181

182182
require $vendor.'/autoload.php';
183183

184+
$logger = new Logger();
184185
$arguments = [
185186
self::CACHE_DIR_KEY => '',
186187
self::REFRESH_PSL_KEY => false,
@@ -189,17 +190,19 @@ public static function updateLocalCache(Event $event = null)
189190
];
190191

191192
if (null !== $event) {
193+
/** @var BaseIO $logger */
194+
$logger = $event->getIO();
192195
$arguments = array_replace($arguments, $event->getArguments());
193196
}
194197

195-
$installer = self::createFromCacheDir(self::getLogger($event), $arguments[Installer::CACHE_DIR_KEY]);
196-
$io->info('Updating your Pdp local cache.');
198+
$installer = self::createFromCacheDir($logger, $arguments[Installer::CACHE_DIR_KEY]);
199+
$io->write('Updating your Pdp local cache.');
197200
if ($installer->refresh($arguments)) {
198-
$io->info('Pdp local cache successfully updated.');
201+
$io->write('Pdp local cache successfully updated.');
199202
die(0);
200203
}
201204

202-
$io->error('The command failed to update Pdp local cache.');
205+
$io->writeError('The command failed to update Pdp local cache.');
203206
die(1);
204207
}
205208

@@ -231,25 +234,6 @@ private function doWrite($messages, bool $newline, bool $stderr, int $verbosity)
231234
};
232235
}
233236

234-
/**
235-
* Detect the Logger instance to use.
236-
*
237-
* @param Event|null $event
238-
*
239-
* @return LoggerInterface
240-
*/
241-
private static function getLogger(Event $event = null): LoggerInterface
242-
{
243-
if (null !== $event) {
244-
/** @var BaseIO $logger */
245-
$logger = $event->getIO();
246-
247-
return $logger;
248-
}
249-
250-
return new Logger();
251-
}
252-
253237
/**
254238
* Detect the vendor path.
255239
*

tests/InstallerTest.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
use Pdp\HttpClient;
2121
use Pdp\HttpClientException;
2222
use Pdp\Installer;
23+
use Pdp\Logger;
2324
use Pdp\Manager;
2425
use PHPUnit\Framework\TestCase;
25-
use Psr\Log\AbstractLogger;
2626
use function file_get_contents;
27+
use function rewind;
2728
use function sprintf;
29+
use function stream_get_contents;
2830

2931
/**
3032
* @coversDefaultClass \Pdp\Installer
@@ -37,6 +39,16 @@ class InstallerTest extends TestCase
3739
protected $client;
3840
protected $logger;
3941

42+
/**
43+
* @return resource
44+
*/
45+
private function setStream()
46+
{
47+
/** @var resource $stream */
48+
$stream = fopen('php://memory', 'r+');
49+
return $stream;
50+
}
51+
4052
public function setUp()
4153
{
4254
$this->root = vfsStream::setup('pdp');
@@ -57,25 +69,6 @@ public function getContent(string $url): string
5769
throw new HttpClientException(sprintf('invalid url: %s', $url));
5870
}
5971
};
60-
61-
$this->logger = new class() extends AbstractLogger {
62-
private $data = [];
63-
64-
public function log($level, $message, array $context = [])
65-
{
66-
$replace = [];
67-
foreach ($context as $key => $val) {
68-
$replace['{'.$key.'}'] = $val;
69-
}
70-
71-
$this->data[] = strtr($message, $replace);
72-
}
73-
74-
public function getLogs(string $level = null): array
75-
{
76-
return $this->data;
77-
}
78-
};
7972
}
8073

8174
public function tearDown()
@@ -95,12 +88,16 @@ public function tearDown()
9588
*/
9689
public function testRefreshDefault(array $context, bool $retval, array $logs)
9790
{
91+
$stream = $this->setStream();
92+
$logger = new Logger($stream, $stream);
9893
$manager = new Manager($this->cachePool, $this->client);
99-
$installer = new Installer($manager, $this->logger);
100-
94+
$installer = new Installer($manager, $logger);
10195
self::assertSame($retval, $installer->refresh($context));
96+
rewind($stream);
97+
/** @var string $data */
98+
$data = stream_get_contents($stream);
10299
foreach ($logs as $log) {
103-
self::assertContains($log, $this->logger->getLogs());
100+
self::assertContains($log, $data);
104101
}
105102
}
106103

0 commit comments

Comments
 (0)