Skip to content

Commit

Permalink
Make NcipService extend EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Aug 10, 2014
1 parent 8d51328 commit 62fcc0e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ if ($request->is('LookupUser')) {
}
```

### Events:

The client emits events on each request to the server. This can be useful to implement logging.
The events are `request.item`, `request.user`, `request.checkout`, `request.checkin` and `request.renew`.

```php
$client->on('request.checkout', function($userId, $itemId) {
$log->addInfo('Requested checkout of item "' . $itemId . '" for user "' . $userId . '"');
}
```
16 changes: 6 additions & 10 deletions src/Scriptotek/Ncip/NcipClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@
*/

use Danmichaelo\QuiteSimpleXMLElement\InvalidXMLException;
use Evenement\EventEmitter;

class NcipClient extends NcipService {

protected $connector;

protected $emitter;

/**
* Create a new Ncip client
*
* @param NcipConnector $connector
* @param EventEmitter $emitter
* @return void
*/
public function __construct(NcipConnector $connector, EventEmitter $emitter = null)
public function __construct(NcipConnector $connector)
{
$this->connector = $connector;
$this->emitter = $emitter ?: new EventEmitter;
parent::__construct();
}

Expand Down Expand Up @@ -56,7 +52,7 @@ public function post(Request $request)
public function lookupUser($user_id)
{
$request = new UserRequest($user_id);
$this->emitter->emit('request.user', array($user_id));
$this->emit('request.user', array($user_id));
$response = $this->post($request);
return new UserResponse($response);
}
Expand All @@ -71,7 +67,7 @@ public function lookupUser($user_id)
public function checkOutItem($user_id, $item_id)
{
$request = new CheckOutRequest($this->connector->agency_id, $user_id, $item_id);
$this->emitter->emit('request.checkout', array($user_id, $item_id));
$this->emit('request.checkout', array($user_id, $item_id));
$response = $this->post($request);
return new CheckOutResponse($response);
}
Expand All @@ -85,7 +81,7 @@ public function checkOutItem($user_id, $item_id)
public function checkInItem($item_id)
{
$request = new CheckInRequest($this->connector->agency_id, $item_id);
$this->emitter->emit('request.checkin', array($item_id));
$this->emit('request.checkin', array($item_id));
$response = $this->post($request);
return new CheckInResponse($response);
}
Expand All @@ -100,7 +96,7 @@ public function checkInItem($item_id)
public function renewItem($user_id, $item_id)
{
$request = new RenewRequest($user_id, $item_id);
$this->emitter->emit('request.renew', array($user_id, $item_id));
$this->emit('request.renew', array($user_id, $item_id));
$response = $this->post($request);
return new RenewResponse($response);
}
Expand All @@ -114,7 +110,7 @@ public function renewItem($user_id, $item_id)
public function lookupItem($item_id)
{
$request = new ItemRequest($item_id);
$this->emitter->emit('request.item', array($item_id));
$this->emit('request.item', array($item_id));
$response = $this->post($request);
return new ItemResponse($response);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Scriptotek/Ncip/NcipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/

use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
use Evenement\EventEmitter;

class NcipService {
class NcipService extends EventEmitter {

public $namespaces;

Expand Down
27 changes: 27 additions & 0 deletions tests/NcipClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,31 @@ public function testInvalidResponse() {
$response = $this->ncip->lookupUser('test123456');
}

public function testEvents()
{
$u = 'Mr. Nelson';
$i = 'Unit testing decoded';
$this->conn->shouldReceive('post')
->once()
->andReturn($this->dummyCheckoutResponse(array(
'userId' => $u,
'itemId' => $i,
))->xml());

$listenerCalled = 0;
$receivedUserId = '';
$receivedItemId = '';
$this->ncip->on('request.checkout', function($userId, $itemId) use (&$listenerCalled, &$receivedUserId, &$receivedItemId) {
$listenerCalled++;
$receivedUserId = $userId;
$receivedItemId = $itemId;
});

$this->assertEquals(0, $listenerCalled);
$this->ncip->checkOutItem($u, $i);
$this->assertEquals(1, $listenerCalled);
$this->assertEquals($u, $receivedUserId);
$this->assertEquals($i, $receivedItemId);
}

}

0 comments on commit 62fcc0e

Please sign in to comment.