<?php
namespace App\Service;
use App\Entity\Publications;
use App\Repository\MembreRepository;
use App\Repository\PublicationsRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Process\Process;
class UtilsService
{
private $phpPath;
private $projectDir;
private $doctrine;
public function __construct(string $phpPath, string $projectDir, ManagerRegistry $doctrine)
{
$this->phpPath = $phpPath == "PHP_BINARY" ? PHP_BINARY : $phpPath;
$this->projectDir = $projectDir;
$this->doctrine = $doctrine;
}
public function fetchPublications(
string $scholarId,
$membre,
MembreRepository $membreRepository,
ScholarService $scholarService
)
{
$data = $scholarService->fetchScholarData($scholarId);
if (isset($data['publications']) && is_array($data['publications'])) {
$publications = $data['publications'];
$syncedGoogleScholarInterval = intval($_ENV['SYNCED_GOOGLE_SCHOLAR_INTERVAL'] ?? 30);
$lastSyncedGoogleScholarDate = $membre->getLastSyncedGoogleScholarDate();
$currentDate = new \DateTime();
$shouldUpdate = !$lastSyncedGoogleScholarDate || ($lastSyncedGoogleScholarDate->diff($currentDate)->days > $syncedGoogleScholarInterval);
if ($shouldUpdate) {
// Étape 1 : Supprimer toutes les publications existantes
$membre->removeAllPublications();
// Étape 2 : Ajouter les nouvelles publications
foreach ($publications as $publication) {
$pub = new Publications();
$pub->setTitre($publication['titre']);
$pub->setAuthors($publication['authors']);
$pub->setVenue($publication['venue']);
$pub->setCitations($publication['citations']);
$pub->setYear($publication['year']);
$pub->setLink($publication['link']);
$this->doctrine->getManager()->persist($pub);
$membre->addPublicationEdit($pub);
}
$membre->setLastSyncedGoogleScholarDate($currentDate);
}
} else {
$publications = $membreRepository->showPublications($membre->getId());
}
// Étape 3 : Sauvegarder toutes les modifications
$this->doctrine->getManager()->flush();
return $publications ?? [];
}
}