From e8f9d74283950a1a3d28bcaea84a67a6baaa457a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 9 Jun 2020 00:56:00 +0200 Subject: [PATCH] MAPG-115 adapt existing controllers to the new request classes --- public/index.php | 7 ++-- src/Controller/GameController.php | 24 ++++++++---- src/Controller/MapAdminController.php | 19 ++++++---- src/Controller/PositionController.php | 53 ++++++++++++++++----------- 4 files changed, 65 insertions(+), 38 deletions(-) diff --git a/public/index.php b/public/index.php index acc664f..bc3ea01 100644 --- a/public/index.php +++ b/public/index.php @@ -30,9 +30,10 @@ $match = Container::$routeCollection->match($method, explode('/', $url)); if ($match !== null) { list($route, $params) = $match; - $handler = $route->getHandler(); + $request = new MapGuesser\Request\Request($_GET, $params, $_POST, $_SESSION); - $controller = new $handler[0]; + $handler = $route->getHandler(); + $controller = new $handler[0]($request); if ($controller instanceof MapGuesser\Interfaces\Authorization\ISecured) { $authorized = $controller->authorize(); @@ -41,7 +42,7 @@ if ($match !== null) { } if ($authorized) { - $response = call_user_func([$controller, $handler[1]], $params); + $response = call_user_func([$controller, $handler[1]]); if ($response instanceof MapGuesser\Interfaces\Response\IContent) { header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); diff --git a/src/Controller/GameController.php b/src/Controller/GameController.php index 246f9b8..1664623 100644 --- a/src/Controller/GameController.php +++ b/src/Controller/GameController.php @@ -2,6 +2,7 @@ use MapGuesser\Database\Query\Select; use MapGuesser\Interfaces\Database\IResultSet; +use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Util\Geo\Bounds; use MapGuesser\Response\HtmlContent; use MapGuesser\Response\JsonContent; @@ -9,16 +10,23 @@ use MapGuesser\Interfaces\Response\IContent; class GameController { - public function getGame(array $parameters): IContent + private IRequest $request; + + public function __construct(IRequest $request) { - $mapId = (int) $parameters['mapId']; + $this->request = $request; + } + + public function getGame(): IContent + { + $mapId = (int) $this->request->query('mapId'); $data = $this->prepareGame($mapId); return new HtmlContent('game', $data); } - public function getGameJson(array $parameters): IContent + public function getGameJson(): IContent { - $mapId = (int) $parameters['mapId']; + $mapId = (int) $this->request->query('mapId'); $data = $this->prepareGame($mapId); return new JsonContent($data); } @@ -27,12 +35,14 @@ class GameController { $bounds = $this->getMapBounds($mapId); - if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { - $_SESSION['state'] = [ + $session = $this->request->session(); + + if (($state = $session->get('state')) && $state['mapId'] !== $mapId) { + $session->set('state', [ 'mapId' => $mapId, 'area' => $bounds->calculateApproximateArea(), 'rounds' => [] - ]; + ]); } return ['mapId' => $mapId, 'bounds' => $bounds->toArray()]; diff --git a/src/Controller/MapAdminController.php b/src/Controller/MapAdminController.php index 59a2c3f..d96f8dc 100644 --- a/src/Controller/MapAdminController.php +++ b/src/Controller/MapAdminController.php @@ -1,8 +1,10 @@ request = $request; $this->placeRepository = new PlaceRepository(); } public function authorize(): bool { - //TODO + $user = $this->request->user(); - return false; + return $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN); } public function getMaps(): IContent @@ -32,9 +37,9 @@ class MapAdminController implements ISecured return new HtmlContent('admin/maps'); } - public function getMapEditor(array $parameters): IContent + public function getMapEditor(): IContent { - $mapId = (int) $parameters['mapId']; + $mapId = (int) $this->request->query('mapId'); $bounds = $this->getMapBounds($mapId); @@ -44,9 +49,9 @@ class MapAdminController implements ISecured return new HtmlContent('admin/map_editor', $data); } - public function getPlace(array $parameters) + public function getPlace() { - $placeId = (int) $parameters['placeId']; + $placeId = (int) $this->request->query('placeId'); $placeData = $this->placeRepository->getById($placeId); diff --git a/src/Controller/PositionController.php b/src/Controller/PositionController.php index 1564730..b804797 100644 --- a/src/Controller/PositionController.php +++ b/src/Controller/PositionController.php @@ -1,5 +1,6 @@ request = $request; $this->placeRepository = new PlaceRepository(); } - public function getPosition(array $parameters): IContent + public function getPosition(): IContent { - $mapId = (int) $parameters['mapId']; + $mapId = (int) $this->request->query('mapId'); - if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { + $session = $this->request->session(); + + if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { $data = ['error' => 'no_session_found']; return new JsonContent($data); } - if (count($_SESSION['state']['rounds']) === 0) { + if (count($state['rounds']) === 0) { $newPosition = $this->placeRepository->getForMapWithValidPano($mapId); - $_SESSION['state']['rounds'][] = $newPosition; + $state['rounds'][] = $newPosition; + $session->set('state', $state); $data = ['panoId' => $newPosition['panoId']]; } else { - $rounds = count($_SESSION['state']['rounds']); - $last = $_SESSION['state']['rounds'][$rounds - 1]; + $rounds = count($state['rounds']); + $last = $state['rounds'][$rounds - 1]; $history = []; for ($i = 0; $i < $rounds - 1; ++$i) { - $round = $_SESSION['state']['rounds'][$i]; + $round = $state['rounds'][$i]; $history[] = [ 'position' => $round['position']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(), @@ -55,41 +62,45 @@ class PositionController return new JsonContent($data); } - public function evaluateGuess(array $parameters): IContent + public function evaluateGuess(): IContent { - $mapId = (int) $parameters['mapId']; + $mapId = (int) $this->request->query('mapId'); - if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { + $session = $this->request->session(); + + if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { $data = ['error' => 'no_session_found']; return new JsonContent($data); } - $last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1]; + $last = $state['rounds'][count($state['rounds']) - 1]; $position = $last['position']; - $guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']); - - $last['guessPosition'] = $guessPosition; + $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng')); $distance = $this->calculateDistance($position, $guessPosition); - $score = $this->calculateScore($distance, $_SESSION['state']['area']); + $score = $this->calculateScore($distance, $state['area']); + $last['guessPosition'] = $guessPosition; $last['distance'] = $distance; $last['score'] = $score; + $state['rounds'][count($state['rounds']) - 1] = $last; - if (count($_SESSION['state']['rounds']) < static::NUMBER_OF_ROUNDS) { + if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) { $exclude = []; - foreach ($_SESSION['state']['rounds'] as $round) { + foreach ($state['rounds'] as $round) { $exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]); } $newPosition = $this->placeRepository->getForMapWithValidPano($mapId, $exclude); - $_SESSION['state']['rounds'][] = $newPosition; + $state['rounds'][] = $newPosition; + $session->set('state', $state); $panoId = $newPosition['panoId']; } else { - $_SESSION['state']['rounds'] = []; + $state['rounds'] = []; + $session->set('state', $state); $panoId = null; }