-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from cocoders/feature/trm24-parser
Refresh available bikes functionality
- Loading branch information
Showing
4 changed files
with
104 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/AppBundle/Controller/UpdateAvailableBikesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace AppBundle\Controller; | ||
|
||
use AppBundle\Command\UpdateAvailableBikesFromProviderCommand; | ||
use Cocoders\CityBike\DockingStationsProvider; | ||
use Cocoders\UseCase\UpdateAvailableBikes\Command; | ||
use Cocoders\UseCase\UpdateAvailableBikes\Responder; | ||
use Cocoders\UseCase\UpdateAvailableBikes\Response; | ||
use Cocoders\UseCase\UpdateAvailableBikes\UpdateAvailableBikes; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
|
||
class UpdateAvailableBikesController extends Controller implements Responder | ||
{ | ||
private $response; | ||
|
||
/** | ||
* @Route("/app/refresh", name="refresh_stations") | ||
* @param Request $request | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function updateAvailableBikesAction() | ||
{ | ||
$stations = $this->parseDockingStations(); | ||
|
||
foreach ($stations as $station) { | ||
$this->updateAvailableBikes()->execute(new Command( | ||
$station['id'], | ||
$station['availableBikes']), $this); | ||
} | ||
return $this->response; | ||
} | ||
|
||
public function updatedAvailableBikesOnStations(Response $response) | ||
{ | ||
$this->getFlashMessage( | ||
'notice', | ||
['Available bikes successfully refreshed!'] | ||
); | ||
|
||
$this->response = $this->redirectToRoute('form'); | ||
} | ||
|
||
public function invalidDockingStation(Response $response) | ||
{ | ||
$this->getFlashMessage( | ||
'notice', | ||
['Available bikes NOT refreshed! Please review docking station repository.'] | ||
); | ||
|
||
$this->response = $this->redirectToRoute('form'); | ||
} | ||
|
||
private function parseDockingStations() | ||
{ | ||
return $this->get('cocoders.trm24.parser')->getDockingStations(); | ||
} | ||
|
||
private function updateAvailableBikes() | ||
{ | ||
return $this->get('cocoders.use_case.update_available_bikes'); | ||
} | ||
|
||
private function getFlashMessage($type, $message) | ||
{ | ||
return $this->container->get('session')->getFlashBag()->set( | ||
$type, | ||
$message | ||
); | ||
} | ||
} |