Completed
Push — master ( 02cfb9...95b30c )
by Artem
06:06
created

generateJwtTokenForAnonymous()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 2
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->getExpirationTime(),
62
            $base64info,
63
            $channels
64
        );
65
66
        return $this->jwtGenerator->generateToken($jwtPayload);
67
    }
68
69
    /**
70
     * @param string|null $base64info
71
     * @param array       $channels
72
     *
73
     * @return string
74
     */
75
    public function generateJwtTokenForAnonymous(?string $base64info = null, array $channels = []): string
76
    {
77
        $jwtPayload = new JwtPayload(
78
            '',
79
            [],
80
            $this->getExpirationTime(),
81
            $base64info,
82
            $channels
83
        );
84
85
        return $this->jwtGenerator->generateToken($jwtPayload);
86
    }
87
88
    /**
89
     * @param string      $client
90
     * @param string      $channel
91
     * @param string|null $base64info
92
     * @param bool|null   $eto
93
     *
94
     * @return string
95
     */
96
    public function generateJwtTokenForPrivateChannel(string $client, string $channel, ?string $base64info = null, ?bool $eto = null): string
97
    {
98
        $jwtPayload = new JwtPayloadForPrivateChannel(
99
            $client,
100
            $channel,
101
            [],
102
            $this->getExpirationTime(),
103
            $base64info,
104
            $eto
105
        );
106
107
        return $this->jwtGenerator->generateToken($jwtPayload);
108
    }
109
110
    /**
111
     * @return int|null
112
     */
113
    private function getExpirationTime(): ?int
114
    {
115
        $expireAt = null;
116
117
        if (null !== $this->centrifugoJwtTtl) {
118
            $now = $this->dateTimeHelper->getCurrentDatetime();
119
            $now->add(new \DateInterval("PT{$this->centrifugoJwtTtl}S"));
120
            $expireAt = $now->getTimestamp();
121
        }
122
123
        return $expireAt;
124
    }
125
}
126