Passed
Pull Request — master (#81)
by
unknown
06:53
created

RequestRefreshToken   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 15
cts 17
cp 0.8824
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getRefreshToken() 0 17 6
A getRefreshTokenName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gesdinet\JWTRefreshTokenBundle\Request;
13
14
use Gesdinet\JWTRefreshTokenBundle\NameGenerator\NameGeneratorInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Service to extract refresh token from a request object.
19
 */
20
class RequestRefreshToken
21
{
22
    /**
23
     * @var NameGeneratorInterface
24
     */
25
    private $nameGenerator;
26
27
    /**
28
     * Injects dependencies.
29
     *
30
     * @param NameGeneratorInterface $nameGenerator
31
     */
32 10
    public function __construct(NameGeneratorInterface $nameGenerator)
33
    {
34 10
        $this->nameGenerator = $nameGenerator;
35 10
    }
36
37 10
    public function getRefreshToken(Request $request)
38
    {
39 10
        $refreshTokenString = null;
40 10
        $refreshTokenName = $this->getRefreshTokenName();
41
42 10
        if (false !== strpos($request->getContentType(), 'json')) {
43 6
            $content = $request->getContent();
44 6
            $params = !empty($content) ? json_decode($content, true) : array();
45 6
            $refreshTokenString = isset($params[$refreshTokenName]) ? trim($params[$refreshTokenName]) : null;
46 4
        } elseif (null !== $request->get($refreshTokenName)) {
47 4
            $refreshTokenString = $request->get($refreshTokenName);
48
        } elseif (null !== $request->request->get($refreshTokenName)) {
49
            $refreshTokenString = $request->request->get($refreshTokenName);
50
        }
51
52 10
        return $refreshTokenString;
53
    }
54
55
    /**
56
     * Returns the name of the access token based on the current naming convention.
57
     *
58
     * @return string
59
     */
60 10
    private function getRefreshTokenName()
61
    {
62 10
        return $this->nameGenerator->generateName('refresh_token');
63
    }
64
}
65