diff --git a/src/Api/InterfaceControllerProvider.php b/src/Api/InterfaceControllerProvider.php index 774a080..6d5547b 100644 --- a/src/Api/InterfaceControllerProvider.php +++ b/src/Api/InterfaceControllerProvider.php @@ -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"); diff --git a/src/Api/StatsControllerProvider.php b/src/Api/StatsControllerProvider.php new file mode 100644 index 0000000..6a1cdd5 --- /dev/null +++ b/src/Api/StatsControllerProvider.php @@ -0,0 +1,41 @@ +_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'); + }); +} diff --git a/src/Models/GameRepository.php b/src/Models/GameRepository.php index f81f9d9..ac5e3af 100644 --- a/src/Models/GameRepository.php +++ b/src/Models/GameRepository.php @@ -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(); + } } \ No newline at end of file diff --git a/www/index.php b/www/index.php index e38b2bb..9aac093 100644 --- a/www/index.php +++ b/www/index.php @@ -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()] );