This commit is contained in:
Ruben 2017-03-31 12:24:28 +02:00
parent 4716733328
commit 4aacc786d4
4 changed files with 57 additions and 2 deletions

View file

@ -119,7 +119,7 @@ class InterfaceControllerProvider implements ControllerProviderInterface
$controllers->get('/images', function (Application $app) {
$http_origin = $_SERVER['HTTP_ORIGIN'];
$http_origin = $_SERVER['HTTP_ORIGIN'] ?? null;
if ($http_origin == "https://emotionhero.com" || $http_origin == "http://emotionhero.com")
{
header("Access-Control-Allow-Origin: $http_origin");

View file

@ -0,0 +1,41 @@
<?php
namespace EmotionHero\Api;
use EmotionHero\Application as EH;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Silex\Api\ControllerProviderInterface;
use EmotionHero\Models;
use EmotionHero\Server\Websockets;
class StatsControllerProvider 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'];
$gamesRepo = $this->_eh->getEm()->getRepository(Models\Game::class);
$controllers->get('/', function (Application $app) {
$stats = [
'new_games' => [],
'new_users' => [],
];
for ($i=0; $i < 14; $i++) {
$day = date('Y-m-d',strtotime("-$i days"));
$stats['new_games'] = $gameRepo->getCreatedCountOnDate($day);
}
return $app['serializer']->serialize($stats, 'json');
});
}

View file

@ -44,4 +44,17 @@ class GameRepository extends EntityRepository
]);
return $query->getSingleScalarResult();
}
/**
* @return int
*/
public function getCreatedCountOnDate($date) {
$query = $this->_em->createQuery(
"SELECT COUNT(g.id) FROM ".Game::class." g WHERE DATE(g.createdAt) = :date"
)
->setParameters([
'date'=> $date,
]);
return (int) $query->getSingleScalarResult();
}
}

View file

@ -71,7 +71,7 @@ $app['serializer.json'] = function () use ($app) {
$app['security.firewalls'] = array(
'login' => [
'pattern' => 'login|register|oauth|token|interface',
'pattern' => 'login|register|oauth|token|interface|stats',
'anonymous' => true,
],
'secured' => array(
@ -176,6 +176,7 @@ $app->get('/api/protected_resource', function() use ($app){
$app->mount('/', new EmotionHero\Api\ScoreControllerProvider());
$app->mount('/interface', new EmotionHero\Api\InterfaceControllerProvider());
$app->mount('/stats', new EmotionHero\Api\StatsControllerProvider());
// middlewares
$appStack = new EmotionHero\Api\ThrottleMiddleware($app, ['pdo'=>$eh->getEm()->getConnection()->getWrappedConnection()] );