Skip to content

Commit

Permalink
Adding option to dispatch events from http client adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Brunette <n@tebru.net>
  • Loading branch information
natebrunette committed May 4, 2016
1 parent 0af5766 commit 480c3e7
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 44 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -15,7 +15,7 @@
},
"require-dev": {
"mockery/mockery": "^0.9.4",
"phpunit/phpunit": ">= 4, < 6",
"phpunit/phpunit": "^4.0",
"guzzlehttp/guzzle": "^5.3|^6.0",
"symfony/symfony": "^3.0",
"sensio/framework-extra-bundle": "^3.0.2",
Expand Down
70 changes: 37 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/Adapter/Rest/RestAdapterBuilder.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tebru;
use Tebru\Retrofit\Adapter\HttpClientAdapter;
use Tebru\Retrofit\Event\EventDispatcherAware;
use Tebru\Retrofit\Exception\RetrofitException;
use Tebru\Retrofit\HttpClient\ClientProvider;
use Tebru\Retrofit\Subscriber\LogSubscriber;
Expand Down Expand Up @@ -230,9 +231,15 @@ public function build()
$this->eventDispatcher->addSubscriber(new LogSubscriber($this->logger));
}

$client = $this->clientProvider->getClient();

if ($client instanceof EventDispatcherAware) {
$client->setEventDispatcher($this->eventDispatcher);
}

$adapter = new RestAdapter(
$this->baseUrl,
$this->clientProvider->getClient(),
$client,
$this->serializer,
$this->eventDispatcher
);
Expand Down
25 changes: 25 additions & 0 deletions src/Event/EventDispatcherAware.php
@@ -0,0 +1,25 @@
<?php
/*
* Copyright (c) Nate Brunette.
* Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/

namespace Tebru\Retrofit\Event;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Interface EventDispatcherAware
*
* @author Nate Brunette <n@tebru.net>
*/
interface EventDispatcherAware
{
/**
* Set a symfony event dispatcher
*
* @param EventDispatcherInterface $eventDispatcher
* @return null
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher);
}
9 changes: 6 additions & 3 deletions src/Generation/Handler/ResponseHandler.php
Expand Up @@ -56,8 +56,11 @@ public function __invoke(HandlerContext $context)
$context->body()->add('$exception = $apiExceptionEvent->getException();');
$context->body()->add('throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);');
$context->body()->add('}');
$context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);');
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);');
$context->body()->add('$response = $afterSendEvent->getResponse();');

if (null === $callback) {
$context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);');
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);');
$context->body()->add('$response = $afterSendEvent->getResponse();');
}
}
}
10 changes: 10 additions & 0 deletions tests/Unit/Annotation/BodyTest.php
Expand Up @@ -42,4 +42,14 @@ public function testJsonSerializableIsDeprecated()
$body = new Body(['value' => '$body']);
$this->assertTrue($body->isJsonSerializable());
}

public function testJsonSerializable()
{
$this->disableDeprecationWarning();

$body = new Body(['value' => '$body', 'jsonSerializable' => true]);
$this->assertTrue($body->isJsonSerializable());

$this->enableDeprecationWarning();
}
}
@@ -0,0 +1,3 @@
<?php

$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
3 changes: 0 additions & 3 deletions tests/resources/generation/testResponseAsyncNotOptional.php
Expand Up @@ -12,6 +12,3 @@
$exception = $apiExceptionEvent->getException();
throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);
}
$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);
$this->eventDispatcher->dispatch('retrofit.afterSend', $afterSendEvent);
$response = $afterSendEvent->getResponse();
3 changes: 0 additions & 3 deletions tests/resources/generation/testResponseAsyncOptional.php
Expand Up @@ -16,6 +16,3 @@
$exception = $apiExceptionEvent->getException();
throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);
}
$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);
$this->eventDispatcher->dispatch('retrofit.afterSend', $afterSendEvent);
$response = $afterSendEvent->getResponse();

0 comments on commit 480c3e7

Please sign in to comment.