-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
048bc29
commit b1c58ab
Showing
5 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,93 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Entity\Disaster; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
class FemaApiController extends AbstractController | ||
{ | ||
private mixed $repository; | ||
private EntityManagerInterface $em; | ||
|
||
public function __construct( | ||
EntityManagerInterface $entityManager, | ||
private HttpClientInterface $client | ||
) { | ||
define('FEMA_API_DISASTERS_URL', $_SERVER['FEMA_API_DISASTERS_URL']); | ||
define('DEFAULT_COUNT', $_SERVER['DEFAULT_COUNT']); | ||
$this->em = $entityManager; | ||
$this->repository = $this->em->getRepository(Disaster::class); | ||
} | ||
|
||
#[Route('/femaapi', name: 'app_fema_api')] | ||
public function index(): Response | ||
{ | ||
return $this->render('fema_api/index.html.twig', [ | ||
'controller_name' => 'FemaApiController', | ||
]); | ||
} | ||
|
||
/** | ||
* @throws TransportExceptionInterface | ||
*/ | ||
#[Route('/femaapi/fetch-new-items', name: 'fetch_new_items')] | ||
public function fetchNewItems(): void | ||
{ | ||
$latestDisasterNumber = $this->repository->findLatestDisasterNumber(); | ||
$this->fetchItemsUsingDisasterNumber($latestDisasterNumber, DEFAULT_COUNT); | ||
} | ||
|
||
/** | ||
* Returns ID of the latest Disaster entry | ||
* | ||
* @param int $disasterNumber | ||
* @param int $count | ||
* @return array | ||
* @throws TransportExceptionInterface | ||
* @throws ClientExceptionInterface | ||
* @throws DecodingExceptionInterface | ||
* @throws RedirectionExceptionInterface | ||
* @throws ServerExceptionInterface | ||
*/ | ||
public function fetchItemsUsingDisasterNumber(int $disasterNumber, int $count): array | ||
{ | ||
$parameters = []; | ||
|
||
// greater than disaster number | ||
$parameters[] = '$filter=disasterNumber ge ' . $disasterNumber; | ||
|
||
// top | ||
$parameters[] = '$top=' . $count; | ||
|
||
// order by | ||
$parameters[] = '$orderby=disasterNumber asc'; | ||
|
||
$parametersFormatted = '?' . implode('&', $parameters); | ||
|
||
$response = $this->client->request( | ||
'GET', | ||
FEMA_API_DISASTERS_URL . $parametersFormatted | ||
); | ||
|
||
$statusCode = $response->getStatusCode(); | ||
// $statusCode = 200 | ||
$contentType = $response->getHeaders()['content-type'][0]; | ||
// $contentType = 'application/json' | ||
$content = $response->getContent(); | ||
// $content = '{"id":521583, "name":"symfony-docs", ...}' | ||
$content = $response->toArray(); | ||
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...] | ||
|
||
return $content; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block title %}Hello FemaApiController!{% endblock %} | ||
|
||
{% block body %} | ||
<style> | ||
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; } | ||
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; } | ||
</style> | ||
|
||
<div class="example-wrapper"> | ||
<h1>Hello {{ controller_name }}! ✅</h1> | ||
|
||
This friendly message is coming from: | ||
<ul> | ||
<li>Your controller at <code><a href="{{ 'C:/apps/femadisasters/src/Controller/FemaApiController.php'|file_link(0) }}">src/Controller/FemaApiController.php</a></code></li> | ||
<li>Your template at <code><a href="{{ 'C:/apps/femadisasters/templates/fema_api/index.html.twig'|file_link(0) }}">templates/fema_api/index.html.twig</a></code></li> | ||
</ul> | ||
</div> | ||
{% endblock %} |
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,13 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class FemaApiControllerTest extends TestCase | ||
{ | ||
public function testFetchItemsUsingDisasterNumber(): void | ||
{ | ||
} | ||
} |