Permalink
Browse files

Version23

Adding an event subscriber to log when a room is entered.

I again used the adapter approach and created my own interface for
logging/event subscribing. I then have an adapter for example using PSRLogger which is easily
replaceable should I need to.
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent 2c2ec90 commit dcb675d273260b6a9e0af7948a26945ca2605a01
@@ -0,0 +1,30 @@
<?php
namespace Jenko\House\Adapter;
use Jenko\House\Logger\LoggerInterface as LocalLoggerInterface;
use Psr\Log\LoggerInterface;
class PsrLoggerAdapter implements LocalLoggerInterface
{
/**
* @var LoggerInterface $logger
*/
private $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array())
{
$this->logger->log($level, $message, $context);
}
}
@@ -0,0 +1,13 @@
<?php
namespace Jenko\House\Adapter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class SymfonyEventSubscriberAdapter implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return static::getSubscribedEvents();
}
}
@@ -0,0 +1,43 @@
<?php
namespace Jenko\House\Event;
use Jenko\House\Logger\LoggerInterface;
use Jenko\House\Adapter\SymfonyEventSubscriberAdapter;
class EnteredRoomSubscriber extends SymfonyEventSubscriberAdapter implements EventSubscriberInterface
{
/**
* Log level info
*/
const INFO = 200;
/**
* @var LoggerInterface $logger
*/
private $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(EnteredRoomEvent::NAME => 'onRoomEntered');
}
/**
* @param EnteredRoomEvent $event
*/
public function onRoomEntered(EnteredRoomEvent $event)
{
$this->logger->log(self::INFO, 'Entered the room ' . $event->roomName);
}
}
@@ -0,0 +1,11 @@
<?php
namespace Jenko\House\Event;
interface EventSubscriberInterface
{
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents();
}
@@ -18,6 +18,9 @@
*/
private $dispatcher;
/**
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->house = HomeAloneHouseFactory::getHouse();
@@ -0,0 +1,16 @@
<?php
namespace Jenko\House\Logger;
interface LoggerInterface
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = array());
}
@@ -22,4 +22,17 @@ services:
jenko.house.adapters.symfony_event_dispatcher:
class: Jenko\House\Adapter\SymfonyEventDispatcherAdapter
arguments:
- @event_dispatcher
- @event_dispatcher
jenko.house.subscribers.enter_room_subscriber:
class: Jenko\House\Event\EnteredRoomSubscriber
arguments:
- @jenko.house.adapters.psr_logger
tags:
- { name: kernel.event_subscriber }
jenko.house.adapters.psr_logger:
class: Jenko\House\Adapter\PsrLoggerAdapter
arguments:
- @logger

0 comments on commit dcb675d

Please sign in to comment.