Skip to content

Commit

Permalink
Unnullableify Handler's $lastEvents
Browse files Browse the repository at this point in the history
addresses #47 "...should not be nullable, but instead be an empty results"
  • Loading branch information
pcrov authored and ekinhbayar committed Dec 13, 2020
1 parent 767d185 commit 65c5937
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Provider/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function listen(): Promise
return call(function () {
$response = yield $this->request();

return yield $this->resultFactory->build(self::EVENT_NAMESPACE, $response);
return yield $this->resultFactory->buildFromResponse(self::EVENT_NAMESPACE, $response);
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/Response/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ public function __construct(EventFactory $eventFactory, LoggerInterface $logger)
$this->logger = $logger;
}

public function build(string $eventNamespace, Response $response): Promise
public function build(): Results
{
return new Results($this->eventFactory, $this->logger);
}

public function buildFromResponse(string $eventNamespace, Response $response): Promise
{
return call(function() use ($eventNamespace, $response) {
$results = new Results($this->eventFactory, $this->logger);
$results = $this->build();

yield $results->appendResponse($eventNamespace, $response);

Expand Down
14 changes: 12 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,24 @@ private function getRouter(): Router

private function getWebSocket(): Websocket
{
$eventCollectionFactory = new EventCollectionFactory(
new EventFactory($this->configuration->getSpecialRepositories()),
$this->logger,
);

$gitHubListener = new GitHub(
HttpClientBuilder::buildDefault(),
$this->configuration->getGithubToken(),
new EventCollectionFactory(new EventFactory($this->configuration->getSpecialRepositories()), $this->logger),
$eventCollectionFactory,
$this->logger,
);

$clientHandler = new Handler($gitHubListener, $this->configuration, $this->logger);
$clientHandler = new Handler(
$gitHubListener,
$this->configuration,
$eventCollectionFactory->build(),
$this->logger,
);

return new Websocket($clientHandler);
}
Expand Down
15 changes: 9 additions & 6 deletions src/Websocket/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ class Handler implements ClientHandler, WebsocketServerObserver

private Configuration $configuration;

private ?Results $lastEvents = null;
private Results $lastEvents;

private LoggerInterface $logger;

public function __construct(Listener $provider, Configuration $configuration, LoggerInterface $logger)
{
public function __construct(
Listener $provider,
Configuration $configuration,
Results $results,
LoggerInterface $logger
) {
$this->provider = $provider;
$this->configuration = $configuration;
$this->lastEvents = $results;
$this->logger = $logger;
}

Expand All @@ -61,9 +66,7 @@ public function handleClient(Gateway $gateway, Client $client, Request $request,

yield $this->sendConnectedUsersCount(\count($gateway->getClients()));

if ($this->lastEvents) {
$client->send($this->lastEvents->jsonEncode());
}
$client->send($this->lastEvents->jsonEncode());

yield $client->receive();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testListenReturnsResults(): void

$this->factory
->expects($this->once())
->method('build')
->method('buildFromResponse')
->will($this->returnValue(new Success($this->createMock(Results::class))))
;

Expand Down
5 changes: 3 additions & 2 deletions tests/Response/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testBuildResultsUnknownEventDoesNotBubbleUp(): void

$factory = new Factory(new EventFactory([]), $logger);

$results = wait($factory->build('ekinhbayar\GitAmp\Event\GitHub', $response));
$results = wait($factory->buildFromResponse('ekinhbayar\GitAmp\Event\GitHub', $response));

$this->assertInstanceOf(Results::class, $results);
}
Expand Down Expand Up @@ -77,7 +77,8 @@ public function testBuildReturnsResult(): void

$logger = $this->createMock(LoggerInterface::class);

$results = (new Factory(new EventFactory([]), $logger))->build('ekinhbayar\GitAmp\Event\GitHub', $response);
$results = (new Factory(new EventFactory([]), $logger))
->buildFromResponse('ekinhbayar\GitAmp\Event\GitHub', $response);

$this->assertInstanceOf(Results::class, wait($results));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Websocket/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setUp(): void
->addWebsocketAddress(Uri::createFromString('https://gitamp.audio'))
->bind(new ServerAddress('127.0.0.1', 1337))
;
$results = $this->createMock(Results::class);

$this->logger = $this->createMock(Logger::class);
$this->gateway = $this->createMock(Gateway::class);
Expand All @@ -60,7 +61,7 @@ public function setUp(): void
}
), $this->logger);

$this->handler = new Handler($this->listener, $configuration, $this->logger);
$this->handler = new Handler($this->listener, $configuration, $results, $this->logger);
}

public function testHandleHandshakeReturnsForbiddenOnInvalidOrigin(): void
Expand Down

0 comments on commit 65c5937

Please sign in to comment.