api.emotionhero.com/src/Api/ScoreControllerProvider.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2016-09-01 12:15:17 +02:00
<?php
namespace EmotionHero\Api;
use EmotionHero\Application as EH;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Silex\Api\ControllerProviderInterface;
use EmotionHero\Models;
class ScoreControllerProvider implements ControllerProviderInterface
{
/** @var EH */
protected $_eh;
public function __construct()
{
$this->_eh = EH::getInstance();
}
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
$controllers->get('/', function (Application $app) {
return "OK";
});
$controllers->get('/levels', function (Application $app) {
$levels = $this->_eh->getEm()->getRepository(Models\Level::class)->findAll();
return $app['serializer']->serialize($levels, 'json');
});
$controllers->get('/emotions', function (Application $app) {
$levels = $this->_eh->getEm()->getRepository(Models\Emotion::class)->findAll();
return $app['serializer']->serialize($levels, 'json');
});
$controllers->get('/me', function (Application $app) {
$token = $app['security.token_storage']->getToken();
$user = $token->getUser();
return $app['serializer']->serialize($user, 'json');
});
$controllers->post('/me/games', function (Request $request, Application $app) {
$vars = json_decode($request->getContent(), true);
var_dump($vars);
return $app['serializer']->serialize($levels, 'json');
});
return $controllers;
}
}