diff --git a/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php b/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php index b2a3db3ac..2daf280bb 100644 --- a/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php +++ b/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php @@ -17,6 +17,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Utils; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; /** * Queries for ROME partenaires api. @@ -39,9 +40,12 @@ class PartenaireRomeAppellation */ protected $logger; + private const BASE = 'https://api.pole-emploi.io/partenaire/rome-metiers/v1/metiers/'; + public function __construct( ApiWrapper $wrapper, - LoggerInterface $logger + LoggerInterface $logger, + private \Symfony\Contracts\HttpClient\HttpClientInterface $httpClient, ) { $this->wrapper = $wrapper; $this->logger = $logger; @@ -58,10 +62,7 @@ class PartenaireRomeAppellation ]); } - /** - * @param string $search - */ - public function getListeAppellation($search) + public function getListeAppellation(string $search): array { $bearer = $this->getBearer(); @@ -84,27 +85,24 @@ class PartenaireRomeAppellation } } - public function getAppellation($code) + public function getAppellation(string $code): array { $bearer = $this->getBearer(); - $request = new Request('GET', sprintf('appellation/%s', $code)); - $parameters = [ - 'headers' => [ - 'Authorization' => 'Bearer '.$bearer, - ], - 'query' => [ - 'champs' => 'code,libelle,metier(code,libelle)', - ], - ]; + try { + $response = $this->httpClient->request('GET', sprintf(self::BASE . 'appellation/%s', $code), [ + 'headers' => [ + 'Authorization' => 'Bearer '.$bearer, + 'Accept' => 'application/json', + ], + 'query' => [ + 'champs' => 'code,libelle,metier(code,libelle)', + ], + ]); - $response = $this->handleRequest( - $request, - $parameters, - $this->client, - $this->logger - ); - - return Utils::jsonDecode((string) $response->getBody()); + return $response->toArray(); + } catch (HttpExceptionInterface $exception) { + throw $exception; + } } } diff --git a/src/Bundle/ChillFranceTravailApiBundle/src/Controller/RomeController.php b/src/Bundle/ChillFranceTravailApiBundle/src/Controller/RomeController.php index ef7a0619f..5a26d590c 100644 --- a/src/Bundle/ChillFranceTravailApiBundle/src/Controller/RomeController.php +++ b/src/Bundle/ChillFranceTravailApiBundle/src/Controller/RomeController.php @@ -41,8 +41,8 @@ class RomeController extends AbstractController $results = []; foreach ($appellations as $appellation) { - $appellation->id = 'original-'.$appellation->code; - $appellation->text = $appellation->libelle; + $appellation['id'] = 'original-'.$appellation['code']; + $appellation['text'] = $appellation['libelle']; $results[] = $appellation; } diff --git a/src/Bundle/ChillFranceTravailApiBundle/src/Resources/config/services.yml b/src/Bundle/ChillFranceTravailApiBundle/src/Resources/config/services.yml index 2bb364f7a..658a152dc 100644 --- a/src/Bundle/ChillFranceTravailApiBundle/src/Resources/config/services.yml +++ b/src/Bundle/ChillFranceTravailApiBundle/src/Resources/config/services.yml @@ -1,12 +1,14 @@ services: + _defaults: + autowire: true + autoconfigure: true + Chill\FranceTravailApiBundle\ApiHelper\ApiWrapper: $clientId: '%env(FRANCE_TRAVAIL_CLIENT_ID)%' $clientSecret: '%env(FRANCE_TRAVAIL_CLIENT_SECRET)%' $redis: '@Chill\MainBundle\Redis\ChillRedis' - Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation: - $wrapper: '@Chill\FranceTravailApiBundle\ApiHelper\ApiWrapper' - $logger: '@Psr\Log\LoggerInterface' + Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation: ~ Chill\FranceTravailApiBundle\Controller\RomeController: autowire: true diff --git a/src/Bundle/ChillJobBundle/src/Entity/Rome/Metier.php b/src/Bundle/ChillJobBundle/src/Entity/Rome/Metier.php index 668b2283f..ce44a6acf 100644 --- a/src/Bundle/ChillJobBundle/src/Entity/Rome/Metier.php +++ b/src/Bundle/ChillJobBundle/src/Entity/Rome/Metier.php @@ -36,7 +36,7 @@ class Metier * @var \Doctrine\Common\Collections\Collection */ #[ORM\OneToMany(targetEntity: Appellation::class, mappedBy: 'metier')] - private readonly \Doctrine\Common\Collections\Collection $appellations; + private \Doctrine\Common\Collections\Collection $appellations; public function __construct() { diff --git a/src/Bundle/ChillJobBundle/src/Form/ChoiceLoader/RomeAppellationChoiceLoader.php b/src/Bundle/ChillJobBundle/src/Form/ChoiceLoader/RomeAppellationChoiceLoader.php index 83f1f85a1..9e849ab5e 100644 --- a/src/Bundle/ChillJobBundle/src/Form/ChoiceLoader/RomeAppellationChoiceLoader.php +++ b/src/Bundle/ChillJobBundle/src/Form/ChoiceLoader/RomeAppellationChoiceLoader.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\JobBundle\Form\ChoiceLoader; +use App\App; use Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation; use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; use Symfony\Component\Form\ChoiceList\ChoiceListInterface; @@ -50,6 +51,11 @@ class RomeAppellationChoiceLoader implements ChoiceLoaderInterface */ protected $validator; + /** + * @var array + */ + private $toBeCreated = []; + public function __construct( EntityManagerInterface $em, PartenaireRomeAppellation $apiAppellation, @@ -80,33 +86,41 @@ class RomeAppellationChoiceLoader implements ChoiceLoaderInterface if (str_starts_with($v, 'original-')) { $code = \substr($v, \strlen('original-')); $appellation = $this->appellationRepository->findOneBy(['code' => $code]); + + if (null === $appellation) { + if (array_key_exists($v, $this->toBeCreated)) { + [$appellation, $metier] = $this->toBeCreated[$v]; + } + } } else { $id = $v; $appellation = $this->appellationRepository->find($id); + $metier = $appellation->getMetier(); } if (null === $appellation && '' !== $code) { $def = $this->apiAppellation->getAppellation($code); - $metier = $this->em->getRepository(Metier::class) - ->findOneBy(['code' => $def->metier->code]) - ; - if (null === $metier) { - $metier = (new Metier()) - ->setCode($def->metier->code) - ->setLibelle($def->metier->libelle) - ; - } + $metier = $metier + ?? $this->em->getRepository(Metier::class) + ->findOneBy(['code' => $def['metier']['code']]) + ?? (new Metier()) + ->setCode($def['metier']['code']) + ->setLibelle($def['metier']['libelle']); $appellation = new Appellation(); $appellation - ->setCode($def->code) - ->setLibelle($def->libelle) + ->setCode($def['code']) + ->setLibelle($def['libelle']) ->setMetier($metier) ; - if ([] === $this->validator->validate($appellation) && [] === $this->validator->validate($metier)) { + $errorsAppellation = $this->validator->validate($appellation); + $errorsMetier = $this->validator->validate($metier); + + if ($errorsAppellation->count() === 0 && $errorsMetier->count() === 0) { + $this->toBeCreated[$v] = [$appellation, $metier]; $this->em->persist($appellation); } } diff --git a/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php b/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php index e3327648a..a326e0ff5 100644 --- a/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php +++ b/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php @@ -151,7 +151,7 @@ final class PermissionsGroupController extends AbstractController /** * remove an association between permissionsGroup and roleScope. */ - #[\Symfony\Component\Routing\Annotation\Route(path: '/{_locale}/admin/permissionsgroup/{pgid}/delete_link_role_scope/{rsid}', name: 'admin_permissionsgroup_delete_role_scope', methods: ['DELETE'])] + #[\Symfony\Component\Routing\Annotation\Route(path: '/{_locale}/admin/permissionsgroup/{pgid}/delete_link_role_scope/{rsid}', name: 'admin_permissionsgroup_delete_role_scope', methods: ['POST'])] public function deleteLinkRoleScopeAction(int $pgid, int $rsid): Response { $permissionsGroup = $this->permissionsGroupRepository->find($pgid);