src/Bundles/ApiBundle/v1/Social/Controller/LinkedinController.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\ApiBundle\v1\Social\Controller;
  4. use App\Bundles\ApiBundle\v1\Social\Resource\PreRegistrationResource;
  5. use App\Bundles\SocialBundle\DTO\SocialUserDTO;
  6. use App\Bundles\SocialBundle\Service\SocialUserService;
  7. use App\Bundles\UserBundle\Config\UserNotificatorConfig;
  8. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  9. use KnpU\OAuth2ClientBundle\Client\Provider\LinkedInClient;
  10. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  11. use League\OAuth2\Client\Provider\LinkedInResourceOwner;
  12. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class LinkedinController extends AbstractController
  19. {
  20.     public function __construct(
  21.         private SocialUserService $socialUserService,
  22.         private JWTTokenManagerInterface $JWTTokenManager,
  23.         private UserNotificatorConfig $config
  24.     ) {
  25.     }
  26.     #[Route('/api/v1/social/linkedin'name'api.v1.social.linkedin'methods: ["GET"])]
  27.     public function connect(ClientRegistry $clientRegistry): RedirectResponse
  28.     {
  29.         // https://www.linkedin.com/developers/apps/[APP_ID]/auth
  30.         // Block - OAuth 2.0 scopes
  31.         return $clientRegistry->getClient('linkedin_main')->redirect([
  32.             'r_emailaddress',
  33.             'r_liteprofile',
  34.         ], []);
  35.     }
  36.     #[Route('/api/v1/social/linkedin/check'name'api.v1.social.linkedin.check'methods: ["GET"])]
  37.     public function check(Request $requestClientRegistry $clientRegistry): Response
  38.     {
  39.         $error              $request->get('error');
  40.         $errorDescription   $request->get('error_description');
  41.         if (!empty($error) && !empty($errorDescription)) {
  42.             $result = [
  43.                 'type' => 'error',
  44.                 'data' => $errorDescription,
  45.             ];
  46.         } else {
  47.             /** @var LinkedInClient $client */
  48.             $client $clientRegistry->getClient('linkedin_main');
  49.             try {
  50.                 /** @var LinkedInResourceOwner $linkedInUser */
  51.                 $linkedInUser   $client->fetchUser();
  52.                 $socialUserDTO  SocialUserDTO::createFromLinkedInUser($linkedInUser);
  53.                 $user           $this->socialUserService->resolve($socialUserDTO);
  54.                 if (!$user) {
  55.                     $socialUser $this->socialUserService->create($socialUserDTO);
  56.                     $token      $this->socialUserService->generateRegistrationToken($socialUser);
  57.                     $result     = [
  58.                         'type' => 'pre-registration',
  59.                         'data' => (new PreRegistrationResource($token$socialUserDTO))->jsonSerialize(),
  60.                     ];
  61.                 } else {
  62.                     $result = [
  63.                         'type' => 'token',
  64.                         'data' => $this->JWTTokenManager->create($user),
  65.                     ];
  66.                 }
  67.             } catch (IdentityProviderException $e) {
  68.                 $result = [
  69.                     'type' => 'error',
  70.                     'data' => $e->getMessage(),
  71.                 ];
  72.             }
  73.         }
  74.         return $this->render('@Api/api/v1/social/oauth-response.html.twig', [
  75.             'result'            => $result,
  76.             'frontendBaseUrl'   => $this->config->getFrontendBaseUrl(),
  77.         ]);
  78.     }
  79. }