Lines 90.00% 9 / 10
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
FakeGoogleIdTokenVerifier 90.00% 9 / 10 50.00% 1 / 2 7.05 0.00% 0 / 1
 token 100.00% 1 / 1 100.00% 1 / 1 3
 verify 88.88% 8 / 9 0.00% 0 / 1 4.02
1<?php
2
3declare(strict_types=1);
4
5namespace App\Security;
6
7/**
8 * Fake Google ID token verification for the test env (and opt-in in dev, see DevGoogleIdTokenVerifier).
9 *
10 * Accepts tokens built with {@see self::token()}; anything else is invalid.
11 */
12final class FakeGoogleIdTokenVerifier implements GoogleIdTokenVerifierInterface
13{
14    public static function token(string $googleId, string $email, bool $emailVerified = true, ?string $picture = null): string
15    {
16        return \implode('|', ['fake', $googleId, $email, $emailVerified ? 'verified' : 'unverified', ...(null === $picture ? [] : [$picture])]);
17    }
18
19    #[\Override]
20    public function verify(string $idToken): ?array
21    {
22        $parts = \explode('|', $idToken);
23
24        if (!\in_array(\count($parts), [4, 5], true) || 'fake' !== $parts[0]) {
25            return null;
26        }
27
28        return [
29            'sub' => $parts[1],
30            'email' => $parts[2],
31            'email_verified' => 'verified' === $parts[3],
32            ...(isset($parts[4]) ? ['picture' => $parts[4]] : []),
33        ];
34    }
35}