Passed
Branch develop (eaaf06)
by Florian
04:36
created

GoogleOAuthService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 7
eloc 26
c 0
b 0
f 0
dl 0
loc 57
ccs 23
cts 26
cp 0.8846
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshAccessTokenIfExpired() 0 33 4
A revoke() 0 11 2
A __construct() 0 5 1
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\User;
6
use App\Exception\GoogleException;
7
use App\Exception\GoogleRefreshTokenException;
8
use DateTime;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Exception;
11
use Google_Client;
12
use Psr\Log\LoggerInterface;
13
14
class GoogleOAuthService
15
{
16 4
    public function __construct(
17
        private Google_Client $googleClient,
0 ignored issues
show
Unused Code introduced by
The parameter $googleClient is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
        /** @scrutinizer ignore-unused */ private Google_Client $googleClient,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
        private EntityManagerInterface $em,
0 ignored issues
show
Unused Code introduced by
The parameter $em is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
        /** @scrutinizer ignore-unused */ private EntityManagerInterface $em,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
        private LoggerInterface $logger
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

19
        /** @scrutinizer ignore-unused */ private LoggerInterface $logger

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20 4
    ) {}
21
22
    /**
23
     * @throws GoogleException|GoogleRefreshTokenException
24
     */
25 2
    public function refreshAccessTokenIfExpired(User $user): string
26
    {
27 2
        $this->googleClient->setAccessToken([
28 2
            'access_token' => $user->getAccessToken(),
29 2
            'expires_in' => $user->getAccessTokenExpiresAt()->getTimestamp() - time(),
30 2
            'created' => $user->getAccessTokenCreatedAt()->getTimestamp()
31
        ]);
32
33 2
        if (!$this->googleClient->isAccessTokenExpired()) {
34 1
            return $user->getAccessToken();
35
        }
36
37
        try {
38 1
            $creds = $this->googleClient->fetchAccessTokenWithRefreshToken($user->getRefreshToken());
39
        } catch (Exception $e) {
40
            throw new GoogleException('Could not refresh the token: ' . $e->getMessage());
41
        }
42
43 1
        if (isset($creds['error'])) {
44
            throw new GoogleRefreshTokenException($creds['error_description']);
45
        }
46
47
        $user
48 1
            ->setAccessToken($creds['access_token'])
49 1
            ->setAccessTokenCreatedAt(new DateTime('@' . $creds['created']))
50
            // ->setAccessTokenExpiresAt(new DateTime(sprintf('+%d seconds', $data['expires_in'])));
51
            // Driven by the unit tests: time() function is mockable, new DateTime objects are not
52 1
            ->setAccessTokenExpiresAt(DateTime::createFromFormat('U', time() + $creds['expires_in'])
53
            );
54
55 1
        $this->em->flush();
56
57 1
        return $creds['access_token'];
58
    }
59
60 2
    public function revoke(User $user): void
61
    {
62
        try {
63 2
            $this->googleClient->revokeToken($user->getAccessToken());
64 2
            $this->googleClient->revokeToken($user->getRefreshToken());
65 1
        } catch (Exception $e) {
66 1
            $this->logger->warning('Could not revoke at least one token: ' . $e->getMessage());
67
        }
68
69 2
        $user->setRevokedAt(new DateTime());
70 2
        $this->em->flush();
71 2
    }
72
}
73