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.

AdnxStrategy::getSlug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Audiens\AdobeClient\Authentication;
4
5
use Audiens\AdobeClient\Exception\AuthException;
6
use Doctrine\Common\Cache\Cache;
7
use GuzzleHttp\ClientInterface;
8
9
/**
10
 * Class AdnxStrategy
11
 */
12 View Code Duplication
class AdnxStrategy 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...
13
{
14
15
    const NAME = 'adnx_auth_strategy';
16
17
    const BASE_URL = 'https://api.demdex.com/oauth/token';
18
19
    const CACHE_NAMESPACE = 'adnx_auth_token';
20
    const TOKEN_EXPIRATION = 110;
21
22
    /** @var Cache */
23
    protected $cache;
24
25
    /**
26
     * AdnxStrategy constructor.
27
     *
28
     * @param ClientInterface $clientInterface
29
     * @param Cache|null $cache
30
     */
31
    public function __construct(ClientInterface $clientInterface, Cache $cache)
32
    {
33
        $this->cache = $cache;
34
        $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...
35
    }
36
37
    /**
38
     * @param string $username
39
     * @param string $password
40
     * @param bool $cache
41
     * @param bool $refresh
42
     * @return mixed
43
     * @throws AuthException
44
     */
45
    public function authenticate($clientId, $secretKey, $username, $password, $cache = true, $refresh = false)
46
    {
47
        $cacheKey = self::CACHE_NAMESPACE . sha1($username . $password . self::BASE_URL);
48
49
        if ($cache) {
50
            if ($this->cache->contains($cacheKey)) {
51
                return $this->cache->fetch($cacheKey);
52
            }
53
        }
54
55
        $headerAuth = base64_encode(
56
            sprintf(
57
                '%s:%s',
58
                $clientId,
59
                $secretKey
60
            )
61
        );
62
63
        $response = $this->client->request(
64
            'POST',
65
            self::BASE_URL,
66
            [
67
                'headers' =>
68
                    [
69
                        'Authorization' => 'Basic ' . $headerAuth
70
                    ],
71
                    'form_params' =>
72
                    [
73
                        'grant_type' => $refresh ? 'refresh_token' : 'password',
74
                        'username' => $username,
75
                        'password' => $password,
76
                    ]
77
            ]
78
        );
79
80
81
        $content = $response->getBody()->getContents();
82
        $response->getBody()->rewind();
83
84
        $contentArray = json_decode($content, true);
85
86
        if (!isset($contentArray["access_token"])) {
87
            throw new \Exception(AuthException::authFailed('No field access token available in json response'));
88
        }
89
90
        $token = $contentArray["access_token"];
91
92
        if ($cache) {
93
            $this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION);
94
        }
95
96
        return $token;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getSlug()
103
    {
104
        return self::NAME;
105
    }
106
107
    public function authenticateJwtToken($clientId, $clientSecret, $jwtToken, $cache = true)
108
    {
109
        // TODO: Implement authenticateJwtToken() method.
110
    }
111
}
112