Completed
Pull Request — master (#2)
by Artem
01:20
created

CredentialsGenerator::calculateExpirationTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the FreshCentrifugoBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\CentrifugoBundle\Service\Credentials;
14
15
use Fresh\CentrifugoBundle\Service\Jwt\JwtGenerator;
16
use Fresh\CentrifugoBundle\Token\JwtPayload;
17
use Fresh\CentrifugoBundle\Token\JwtPayloadForPrivateChannel;
18
use Fresh\CentrifugoBundle\User\CentrifugoUserInterface;
19
use Fresh\DateTime\DateTimeHelper;
20
21
/**
22
 * CredentialsGenerator.
23
 *
24
 * @author Artem Henvald <[email protected]>
25
 */
26
class CredentialsGenerator
27
{
28
    /** @var JwtGenerator */
29
    private $jwtGenerator;
30
31
    /** @var DateTimeHelper */
32
    private $dateTimeHelper;
33
34
    /** @var int|null */
35
    private $centrifugoJwtTtl;
36
37
    /**
38
     * @param JwtGenerator   $jwtGenerator
39
     * @param DateTimeHelper $dateTimeHelper
40
     * @param int|null       $centrifugoJwtTtl
41
     */
42
    public function __construct(JwtGenerator $jwtGenerator, DateTimeHelper $dateTimeHelper, ?int $centrifugoJwtTtl = null)
43
    {
44
        $this->jwtGenerator = $jwtGenerator;
45
        $this->dateTimeHelper = $dateTimeHelper;
46
        $this->centrifugoJwtTtl = $centrifugoJwtTtl;
47
    }
48
49
    /**
50
     * @param CentrifugoUserInterface $user
51
     * @param string|null             $base64info
52
     * @param array                   $channels
53
     *
54
     * @return string
55
     */
56
    public function generateJwtTokenForUser(CentrifugoUserInterface $user, ?string $base64info = null, array $channels = []): string
57
    {
58
        $jwtPayload = new JwtPayload(
59
            $user->getCentrifugoSubject(),
60
            $user->getCentrifugoUserInfo(),
61
            $this->calculateExpirationTime(),
62
            $base64info,
63
            $channels
64
        );
65
66
        return $this->jwtGenerator->generateToken($jwtPayload);
67
    }
68
69
    /**
70
     * @param string      $client
71
     * @param string      $channel
72
     * @param string|null $base64info
73
     * @param bool|null   $eto
74
     *
75
     * @return string
76
     */
77
    public function generateJwtTokenForPrivateChannel(string $client, string $channel, ?string $base64info = null, ?bool $eto = null): string
78
    {
79
        $jwtPayload = new JwtPayloadForPrivateChannel(
80
            $client,
81
            $channel,
82
            [], // @todo
83
            $this->calculateExpirationTime(),
84
            $base64info,
85
            $eto
86
        );
87
88
        return $this->jwtGenerator->generateToken($jwtPayload);
89
    }
90
91
    /**
92
     * @return int|null
93
     */
94
    private function calculateExpirationTime(): ?int
95
    {
96
        $expireAt = null;
97
98
        if (null !== $this->centrifugoJwtTtl) {
99
            $now = $this->dateTimeHelper->getCurrentDatetime();
100
            $now->add(new \DateInterval("PT{$this->centrifugoJwtTtl}S"));
101
            $expireAt = $now->getTimestamp();
102
        }
103
104
        return $expireAt;
105
    }
106
}
107