src/Service/UtilsService.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Publications;
  4. use App\Repository\MembreRepository;
  5. use App\Repository\PublicationsRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Doctrine\Persistence\ObjectManager;
  8. use Symfony\Component\Process\Process;
  9. class UtilsService
  10. {
  11.     private $phpPath;
  12.     private $projectDir;
  13.     private $doctrine;
  14.     public function __construct(string $phpPathstring $projectDirManagerRegistry $doctrine)
  15.     {
  16.         $this->phpPath $phpPath == "PHP_BINARY" PHP_BINARY $phpPath;
  17.         $this->projectDir $projectDir;
  18.         $this->doctrine $doctrine;
  19.     }
  20.     public function fetchPublications(
  21.         string                 $scholarId,
  22.                                $membre,
  23.         MembreRepository       $membreRepository,
  24.         ScholarService $scholarService
  25.     )
  26.     {
  27.         $data $scholarService->fetchScholarData($scholarId);
  28.         if (isset($data['publications']) && is_array($data['publications'])) {
  29.             $publications $data['publications'];
  30.             $syncedGoogleScholarInterval intval($_ENV['SYNCED_GOOGLE_SCHOLAR_INTERVAL'] ?? 30);
  31.             $lastSyncedGoogleScholarDate $membre->getLastSyncedGoogleScholarDate();
  32.             $currentDate = new \DateTime();
  33.             $shouldUpdate = !$lastSyncedGoogleScholarDate || ($lastSyncedGoogleScholarDate->diff($currentDate)->days $syncedGoogleScholarInterval);
  34.             if ($shouldUpdate) {
  35.                 // Étape 1 : Supprimer toutes les publications existantes
  36.                 $membre->removeAllPublications();
  37.                 // Étape 2 : Ajouter les nouvelles publications
  38.                 foreach ($publications as $publication) {
  39.                     $pub = new Publications();
  40.                     $pub->setTitre($publication['titre']);
  41.                     $pub->setAuthors($publication['authors']);
  42.                     $pub->setVenue($publication['venue']);
  43.                     $pub->setCitations($publication['citations']);
  44.                     $pub->setYear($publication['year']);
  45.                     $pub->setLink($publication['link']);
  46.                     $this->doctrine->getManager()->persist($pub);
  47.                     $membre->addPublicationEdit($pub);
  48.                 }
  49.                 $membre->setLastSyncedGoogleScholarDate($currentDate);
  50.             }
  51.         } else {
  52.             $publications $membreRepository->showPublications($membre->getId());
  53.         }
  54.         // Étape 3 : Sauvegarder toutes les modifications
  55.         $this->doctrine->getManager()->flush();
  56.         return $publications ?? [];
  57.     }
  58. }