Skip to content

Instantly share code, notes, and snippets.

@Tobur
Created January 8, 2014 08:03
Show Gist options
  • Save Tobur/8313383 to your computer and use it in GitHub Desktop.
Save Tobur/8313383 to your computer and use it in GitHub Desktop.
<?php
/**
*
* @Service("core.event_listener.kernel")
*
* @Tag("kernel.event_listener", attributes = { "event" = "kernel.request", "method" = "processApi" })
*/
class Kernel
{
// Not sure about this path, because we use something different. Check please.
const API_CONTROLLER ='/FOS/OAuthServerBundle/Controller/TokenController.php::tokenAction';
protected $em;
/**
* @InjectParams({
* "em" = @Inject("doctrine.orm.entity_manager")
* })
*/
public function __construct($em)
{
$this->em = $em;
}
public function processApi(GetResponseEvent $event)
{
$request = $event->getRequest();
$controller = $event->getRequest()->attributes->get('_controller');
if ($controller !== self::API_CONTROLLER) {
return;
}
$clients = $this->em->getRepository('DataBundle:Client')->findAll();
if (!$clients || !isset($clients[0])) {
throw new HttpException('API clients are empty. Please, configure them.');
}
if (count($clients) != 1) {
throw new HttpException('There are more than one API client. Please, configure it correctly.');
}
/**
* @var Client $client
*/
$client = reset($clients);
// Setup client id and secret id
if ($request->getMethod() === 'POST') {
$request->request->set('client_id', $client->getPublicId());
$request->request->set('client_secret', $client->getSecret());
return;
}
$request->query->set('client_id', $client->getPublicId());
$request->query->set('client_secret', $client->getSecret());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment