Skip to content
Permalink
Browse files

feat: Refactored `ResponseSubscriber` to use new `Version` service.

  • Loading branch information...
tarlepp committed Sep 18, 2019
1 parent cf137b5 commit 141f962739d12062e819b4738208be851fa48074
Showing with 18 additions and 20 deletions.
  1. +18 −20 src/EventSubscriber/ResponseSubscriber.php
@@ -8,12 +8,10 @@
namespace App\EventSubscriber;
use App\Utils\JSON;
use stdClass;
use App\Service\Version;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use function file_get_contents;
/**
* Class ResponseSubscriber
@@ -23,6 +21,21 @@
*/
class ResponseSubscriber implements EventSubscriberInterface
{
/**
* @var Version
*/
private $version;
/**
* ResponseSubscriber constructor.
*
* @param Version $version
*/
public function __construct(Version $version)
{
$this->version = $version;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
@@ -52,28 +65,13 @@ public static function getSubscribedEvents(): array
}
/**
* Subscriber method to log every response.
* Subscriber method to attach API version to every response.
*
* @param ResponseEvent $event
*/
public function onKernelResponse(ResponseEvent $event): void
{
$response = $event->getResponse();
// Attach new header
$response->headers->add(['X-API-VERSION' => $this->getApiVersion()]);
}
/**
* Method to get current version from composer.json file.
*
* @return string
*/
private function getApiVersion(): string
{
/** @var stdClass $data */
$data = JSON::decode((string)file_get_contents(__DIR__ . '/../../composer.json'));
return property_exists($data, 'version') ? (string)$data->version : 'unknown';
$event->getResponse()->headers->add(['X-API-VERSION' => $this->version->get()]);
}
}

0 comments on commit 141f962

Please sign in to comment.
You can’t perform that action at this time.