GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SandboxStrategy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 102
loc 102
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B authenticate() 53 53 6
A getSlug() 4 4 1
A authenticateJwtToken() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Audiens\AdobeClient\Authentication;
4
5
use Audiens\AdobeClient\Auth;
6
use Audiens\AdobeClient\Exception\AuthException;
7
use Doctrine\Common\Cache\Cache;
8
use GuzzleHttp\ClientInterface;
9
10
/**
11
 * Class SandboxStrategy
12
 */
13 View Code Duplication
class SandboxStrategy implements AuthStrategyInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
16
    const NAME = 'adnx_auth_strategy';
17
18
    const BASE_URL = 'https://api-beta.demdex.com/oauth/token';
19
20
    const CACHE_NAMESPACE = 'adnx_auth_token';
21
    const TOKEN_EXPIRATION = 110;
22
23
    /** @var Cache */
24
    protected $cache;
25
26
    /**
27
     * AdnxStrategy constructor.
28
     *
29
     * @param ClientInterface $clientInterface
30
     * @param Cache|null $cache
31
     */
32
    public function __construct(ClientInterface $clientInterface, Cache $cache)
33
    {
34
        $this->cache = $cache;
35
        $this->client = $clientInterface;
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
    }
37
38
    /**
39
     * @param string $clientId
40
     * @param string $secretKey
41
     * @param string $username
42
     * @param $password
43
     * @param bool $cache
44
     * @param bool $refresh
45
     * @return mixed
46
     * @throws AuthException
47
     */
48
    public function authenticate($clientId, $secretKey, $username, $password, $cache = true, $refresh = false)
49
    {
50
51
        $cacheKey = self::CACHE_NAMESPACE . sha1($username . $password . self::BASE_URL);
52
53
        if ($cache) {
54
            if ($this->cache->contains($cacheKey)) {
55
                return $this->cache->fetch($cacheKey);
56
            }
57
        }
58
59
        $headerAuth = base64_encode(
60
            sprintf(
61
                '%s:%s',
62
                $clientId,
63
                $secretKey
64
            )
65
        );
66
67
        $response = $this->client->request(
68
            'POST',
69
            self::BASE_URL,
70
            [
71
                'headers' =>
72
                    [
73
                        'Authorization' => 'Basic ' . $headerAuth
74
                    ],
75
                    'form_params' =>
76
                    [
77
                        'grant_type' => $refresh ? 'refresh_token' : 'password',
78
                        'username' => $username,
79
                        'password' => $password,
80
                    ]
81
            ]
82
        );
83
84
        $content = $response->getBody()->getContents();
85
        $response->getBody()->rewind();
86
87
        $contentArray = json_decode($content, true);
88
89
        if (!isset($contentArray["access_token"])) {
90
            throw new \Exception(AuthException::authFailed('No field access token available in json response'));
91
        }
92
93
        $token = $contentArray["access_token"];
94
95
        if ($cache) {
96
            $this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION);
97
        }
98
99
        return $token;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getSlug()
106
    {
107
        return self::NAME;
108
    }
109
110
    public function authenticateJwtToken($clientId, $clientSecret, $jwtToken, $cache = true)
111
    {
112
        // TODO: Implement authenticateJwtToken() method.
113
    }
114
}
115