From 39ca62ce5264fc32376cc06e2fb07275be921a70 Mon Sep 17 00:00:00 2001
From: Pierre Ducoudray <pducoudray@worldia.com>
Date: Wed, 25 May 2016 10:48:00 +0200
Subject: [PATCH] Fix event dispatched on document completion because of
 asynchronous behaviour on TM side (#67)

---
 lib/Textmaster/Model/AbstractObject.php | 12 ++++++------
 lib/Textmaster/Model/Document.php       |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/Textmaster/Model/AbstractObject.php b/lib/Textmaster/Model/AbstractObject.php
index 5180fe7..6d712d9 100644
--- a/lib/Textmaster/Model/AbstractObject.php
+++ b/lib/Textmaster/Model/AbstractObject.php
@@ -94,7 +94,7 @@ public function save()
     final protected function update()
     {
         $this->data = $this->getApi()->update($this->getId(), $this->data);
-        $this->dispatchEvent($this->data);
+        $this->dispatchEvent($this->getStatus(), $this->data);
 
         return $this;
     }
@@ -107,7 +107,7 @@ final protected function update()
     final protected function create()
     {
         $this->data = $this->getApi()->create($this->data);
-        $this->dispatchEvent($this->data);
+        $this->dispatchEvent($this->getStatus(), $this->data);
 
         return $this;
     }
@@ -167,14 +167,14 @@ protected function getProperty($property)
     /**
      * Dispatch an event.
      *
-     * @param array $data Optional data to dispatch with the event.
+     * @param string $status
+     * @param array  $data   Optional data to dispatch with the event.
      */
-    protected function dispatchEvent(array $data = array())
+    protected function dispatchEvent($status, array $data = array())
     {
-        $name = $this->getEventNamePrefix().'.'.$this->getStatus();
+        $name = $this->getEventNamePrefix().'.'.$status;
 
         $event = new GenericEvent($this, $data);
-
         $this->client->getEventDispatcher()->dispatch($name, $event);
     }
 
diff --git a/lib/Textmaster/Model/Document.php b/lib/Textmaster/Model/Document.php
index 8b7b903..de97014 100644
--- a/lib/Textmaster/Model/Document.php
+++ b/lib/Textmaster/Model/Document.php
@@ -259,7 +259,7 @@ final public function complete($satisfaction = null, $message = null)
         }
 
         $this->data = $this->getApi()->complete($this->getId(), $satisfaction, $message);
-        $this->dispatchEvent($this->data);
+        $this->dispatchEvent(DocumentInterface::STATUS_COMPLETED, $this->data);
 
         return $this;
     }