From 954f29f8790d7e440bc4bb535f89b5d5900b405f Mon Sep 17 00:00:00 2001
From: Alex Bilbie <hello@alexbilbie.com>
Date: Fri, 11 Jul 2014 15:13:28 +0100
Subject: [PATCH] Added league/event and implemented SessionOwnerEvent

---
 composer.json                   |  3 +-
 src/AbstractServer.php          | 32 +++++++++++++++++++++
 src/AuthorizationServer.php     |  2 ++
 src/Entity/SessionEntity.php    |  2 ++
 src/Event/SessionOwnerEvent.php | 51 +++++++++++++++++++++++++++++++++
 src/ResourceServer.php          |  2 ++
 6 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 src/Event/SessionOwnerEvent.php

diff --git a/composer.json b/composer.json
index 24bffa85..24105daa 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,8 @@
     "license": "MIT",
     "require": {
         "php": ">=5.4.0",
-        "symfony/http-foundation": "~2.1"
+        "symfony/http-foundation": "~2.1",
+        "league/event": "0.1"
     },
     "require-dev": {
         "phpunit/phpunit": "~4.0",
diff --git a/src/AbstractServer.php b/src/AbstractServer.php
index 8439fa85..d3ddcbb4 100644
--- a/src/AbstractServer.php
+++ b/src/AbstractServer.php
@@ -14,6 +14,7 @@ namespace League\OAuth2\Server;
 use League\OAuth2\Server\Exception;
 use League\OAuth2\Server\TokenType\TokenTypeInterface;
 use Symfony\Component\HttpFoundation\Request;
+use League\Event\Emitter;
 
 /**
  * OAuth 2.0 Resource Server
@@ -40,6 +41,37 @@ abstract class AbstractServer
      */
     protected $tokenType;
 
+    /**
+     * Event emitter
+     */
+    protected $eventEmitter;
+
+    /**
+     * Abstract server constructor
+     */
+    public function __construct()
+    {
+        $this->eventEmitter = $this->setEventEmitter();
+    }
+
+    /**
+     * Set an event emitter
+     * @param object $emitter Event emitter object
+     */
+    public function setEventEmitter($emitter = null)
+    {
+        if ($emitter === null) {
+            $this->eventEmitter = new Emitter;
+        } else {
+            $this->eventEmitter = $emitter;
+        }
+    }
+
+    public function addEventListener($eventName, callable $listener)
+    {
+        $this->eventEmitter->addListener($eventName, $listener);
+    }
+
     /**
      * Sets the Request Object
      * @param \Symfony\Component\HttpFoundation\Request The Request Object
diff --git a/src/AuthorizationServer.php b/src/AuthorizationServer.php
index eb94c6b7..b0b0bc14 100644
--- a/src/AuthorizationServer.php
+++ b/src/AuthorizationServer.php
@@ -80,6 +80,8 @@ class AuthorizationServer extends AbstractServer
         // Set Bearer as the default token type
         $this->setTokenType(new Bearer);
 
+        parent::__construct();
+
         return $this;
     }
 
diff --git a/src/Entity/SessionEntity.php b/src/Entity/SessionEntity.php
index f8c14301..ef5f8d0e 100644
--- a/src/Entity/SessionEntity.php
+++ b/src/Entity/SessionEntity.php
@@ -228,6 +228,8 @@ class SessionEntity
         $this->ownerType = $type;
         $this->ownerId = $id;
 
+        $this->server->eventEmitter->emit(new Event\SessionOwnerEvent($this));
+
         return $this;
     }
 
diff --git a/src/Event/SessionOwnerEvent.php b/src/Event/SessionOwnerEvent.php
new file mode 100644
index 00000000..f8c6be76
--- /dev/null
+++ b/src/Event/SessionOwnerEvent.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * OAuth 2.0 session owner event
+ *
+ * @package     league/oauth2-server
+ * @author      Alex Bilbie <hello@alexbilbie.com>
+ * @copyright   Copyright (c) Alex Bilbie
+ * @license     http://mit-license.org/
+ * @link        https://github.com/thephpleague/oauth2-server
+ */
+
+namespace League\OAuth2\Server\Event;
+
+use League\Event\EventAbstract;
+use League\OAuth2\Server\Entity\SessionEntity;
+
+class SessionOwnerEvent extends EventAbstract
+{
+    /**
+     * Session entity
+     * @var \League\OAuth2\Server\Entity\SessionEntity
+     */
+    private $session;
+
+    /**
+     * Init the event with a session
+     * @param \League\OAuth2\Server\Entity\SessionEntity $session
+     */
+    public function __construct(SessionEntity $session)
+    {
+        $this->session = $session;
+    }
+
+    /**
+     * The name of the event
+     * @return string
+     */
+    public function getName()
+    {
+        return 'session.owner';
+    }
+
+    /**
+     * Return session
+     * @return \League\OAuth2\Server\Entity\SessionEntity
+     */
+    public function getSession()
+    {
+        return $this->session;
+    }
+}
diff --git a/src/ResourceServer.php b/src/ResourceServer.php
index 853a5f19..f2a8da7f 100644
--- a/src/ResourceServer.php
+++ b/src/ResourceServer.php
@@ -66,6 +66,8 @@ class ResourceServer extends AbstractServer
         // Set Bearer as the default token type
         $this->setTokenType(new Bearer);
 
+        parent::__construct();
+
         return $this;
     }