Skip to content

Commit

Permalink
Forgive me Java for I have sinned
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveRandom committed Mar 20, 2017
1 parent 1931397 commit 07a2ea8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/Room/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Room11\StackChat\Auth\ActiveSessionTracker;
use Room11\StackChat\Auth\Authenticator;
use Room11\StackChat\Auth\Session;
use Room11\StackChat\WebSocket\EventDispatcherFactory;
use Room11\StackChat\WebSocket\HandlerFactory as WebSocketHandlerFactory;
use function Amp\resolve;
use function Amp\websocket;
Expand All @@ -15,15 +16,18 @@ class Connector
{
private $authenticator;
private $sessions;
private $eventDispatcherFactory;
private $handlerFactory;

public function __construct(
Authenticator $authenticator,
ActiveSessionTracker $sessions,
EventDispatcherFactory $eventDispatcherFactory,
WebSocketHandlerFactory $handlerFactory
) {
$this->authenticator = $authenticator;
$this->sessions = $sessions;
$this->eventDispatcherFactory = $eventDispatcherFactory;
$this->handlerFactory = $handlerFactory;
}

Expand All @@ -36,7 +40,9 @@ public function connect(Identifier $identifier, bool $permanent): Promise

$handshake = (new Handshake($session->getWebSocketUrl()))
->setHeader('Origin', 'https://' . $identifier->getHost());
$handler = $this->handlerFactory->build($identifier);

$eventDispatcher = $this->eventDispatcherFactory->createEventDispatcher($identifier);
$handler = $this->handlerFactory->build($identifier, $eventDispatcher);

yield websocket($handler, $handshake);

Expand Down
13 changes: 13 additions & 0 deletions src/WebSocket/EventDispatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Room11\StackChat\WebSocket;

use Room11\StackChat\Room\Identifier;

/**
* @todo kill this before it lays eggs
*/
interface EventDispatcherFactory
{
function createEventDispatcher(Identifier $room): EventDispatcher;
}
4 changes: 2 additions & 2 deletions src/WebSocket/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class Handler implements Websocket

public function __construct(
EventBuilder $eventBuilder,
EventDispatcher $globalEventDispatcher,
EventDispatcher $eventDispatcher,
EndpointCollection $endpoints,
Logger $logger,
ChatRoomIdentifier $roomIdentifier
) {
$this->eventBuilder = $eventBuilder;
$this->eventDispatcher = $globalEventDispatcher;
$this->eventDispatcher = $eventDispatcher;
$this->endpoints = $endpoints;
$this->logger = $logger;
$this->roomIdentifier = $roomIdentifier;
Expand Down
7 changes: 2 additions & 5 deletions src/WebSocket/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@
class HandlerFactory
{
private $eventBuilder;
private $eventDispatcher;
private $endpointCollection;
private $logger;

public function __construct(
EventBuilder $eventBuilder,
EventDispatcher $eventDispatcher,
EndpointCollection $endpointCollection,
Logger $logger
) {
$this->eventBuilder = $eventBuilder;
$this->eventDispatcher = $eventDispatcher;
$this->endpointCollection = $endpointCollection;
$this->logger = $logger;
}

public function build(ChatRoomIdentifier $identifier)
public function build(ChatRoomIdentifier $identifier, EventDispatcher $eventDispatcher)
{
return new Handler($this->eventBuilder, $this->eventDispatcher, $this->endpointCollection, $this->logger, $identifier);
return new Handler($this->eventBuilder, $eventDispatcher, $this->endpointCollection, $this->logger, $identifier);
}
}

0 comments on commit 07a2ea8

Please sign in to comment.