-
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.
♻️: add one service for each feature (#60)
- Loading branch information
Showing
8 changed files
with
204 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,53 @@ | ||
<?php | ||
|
||
namespace App\Controller\Api; | ||
|
||
use App\Service\Scrapper\StudentSpaceScrapperService; | ||
use App\Service\LoginService; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
|
||
class LoginController extends AbstractController | ||
{ | ||
private StudentSpaceScrapperService $studentScrapper; | ||
private LoginService $loginService; | ||
|
||
public function __construct(StudentSpaceScrapperService $studentScrapper, LoginService $loginService) | ||
{ | ||
$this->studentScrapper = $studentScrapper; | ||
$this->loginService = $loginService; | ||
} | ||
|
||
/** | ||
* @throws ServerExceptionInterface | ||
* @throws RedirectionExceptionInterface | ||
* @throws GuzzleException | ||
* @throws ClientExceptionInterface | ||
*/ | ||
#[Route('/api/login', name: 'app_api_student_login')] | ||
public function login(Request $request): Response | ||
{ | ||
$username = $request->get('username'); | ||
$password = $request->get('password'); | ||
|
||
if (null === $username || null === $password) { | ||
return $this->json(['message' => 'Veuillez renseigner un nom d\'utilisateur et un mot de passe'], 400); | ||
} | ||
|
||
$htmlContent = $this->studentScrapper->getLoginToken(); | ||
$cookieJar = $this->studentScrapper->getCookies(); | ||
$login = $this->loginService->login($username, $password, $htmlContent, $cookieJar); | ||
|
||
if (false === $login) { | ||
return $this->json(['message' => 'Erreur de connexion'], 401); | ||
} else { | ||
return $this->json(['message' => 'Connexion réussie', 'studentId' => $login], 200); | ||
} | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace App\Controller\Api; | ||
|
||
use App\Service\StudentService; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class StudentController extends AbstractController | ||
{ | ||
private StudentService $studentService; | ||
|
||
public function __construct(StudentService $studentService) | ||
{ | ||
$this->studentService = $studentService; | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
#[Route('/api/student/marks/{studentId}', name: 'app_api_student_rating')] | ||
public function getMarks(int $studentId): Response | ||
{ | ||
$pdfContent = $this->studentService->marksSheet($studentId); | ||
if (!$pdfContent) { | ||
return $this->json(['message' => 'Erreur de récupération du PDF'], 500); | ||
} else { | ||
return new Response($pdfContent, 200, [ | ||
'Content-Type' => 'application/pdf', | ||
'Content-Disposition' => 'inline; filename="releve.pdf"', | ||
]); | ||
} | ||
} | ||
|
||
#[Route('/api/student/absences/{studentId}', name: 'app_api_student_absence')] | ||
public function getAbsence(int $studentId): Response | ||
{ | ||
$pdfContent = $this->studentService->absencesSheet($studentId); | ||
if (!$pdfContent) { | ||
return $this->json(['message' => 'Erreur de récupération du PDF'], 500); | ||
} else { | ||
return new Response($pdfContent, 200, [ | ||
'Content-Type' => 'application/pdf', | ||
'Content-Disposition' => 'inline; filename="releve.pdf"', | ||
]); | ||
} | ||
} | ||
} |
This file was deleted.
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
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,43 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
|
||
class StudentService | ||
{ | ||
|
||
private string $marksSheetUrl; | ||
private string $absencesSheetUrl; | ||
|
||
public function __construct(string $marksSheetUrl, string $absencesSheetUrl) | ||
{ | ||
$this->marksSheetUrl = $marksSheetUrl; | ||
$this->absencesSheetUrl = $absencesSheetUrl; | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
public function marksSheet(int $studentId): string | ||
{ | ||
$client = new Client(); | ||
$url = str_replace('{studentId}', $studentId, $this->marksSheetUrl); | ||
$response = $client->request('GET', $url); | ||
|
||
return $response->getBody()->getContents(); | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
public function absencesSheet(int $studentId): string | ||
{ | ||
$client = new Client(); | ||
$url = str_replace('{studentId}', $studentId, $this->absencesSheetUrl); | ||
$response = $client->request('GET', $url); | ||
|
||
return $response->getBody()->getContents(); | ||
} | ||
} |