AppnexusStrategy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 80
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A authenticate() 37 37 5
A getSlug() 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\AppnexusClient\authentication;
4
5
use Audiens\AppnexusClient\exceptions\AuthException;
6
use Doctrine\Common\Cache\Cache;
7
use GuzzleHttp\ClientInterface;
8
9
/**
10
 * Class AppnexusStrategy
11
 */
12 View Code Duplication
class AppnexusStrategy 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.appnexus.com/auth';
18
19
    const CACHE_NAMESPACE  = 'appnexus_auth_token';
20
    const TOKEN_EXPIRATION = 110;
21
22
    /** @var Cache */
23
    protected $cache;
24
25
    /** @var ClientInterface  */
26
    protected $client;
27
28
    /**
29
     * @param ClientInterface $clientInterface
30
     * @param Cache      $cache
31
     */
32
    public function __construct(ClientInterface $clientInterface, Cache $cache)
33
    {
34
        $this->cache = $cache;
35
        $this->client = $clientInterface;
36
    }
37
38
    /**
39
     * @param string    $username
40
     * @param string    $password
41
     * @param bool|true $cache
42
     *
43
     * @return mixed
44
     * @throws AuthException
45
     */
46
    public function authenticate($username, $password, $cache = true)
47
    {
48
49
        $cacheKey = self::CACHE_NAMESPACE.sha1($username.$password.self::BASE_URL);
50
51
        if ($cache) {
52
            if ($this->cache->contains($cacheKey)) {
53
                return $this->cache->fetch($cacheKey);
54
            }
55
        }
56
57
        $payload = [
58
            'auth' => [
59
                'username' => $username,
60
                'password' => $password,
61
            ],
62
        ];
63
64
        $response = $this->client->request('POST', self::BASE_URL, ['body' => json_encode($payload)]);
65
66
        $content = $response->getBody()->getContents();
67
        $response->getBody()->rewind();
68
69
        $contentArray = json_decode($content, true);
70
71
        if (!isset($contentArray["response"]["token"])) {
72
            throw new AuthException($content);
73
        }
74
75
        $token = $contentArray["response"]["token"];
76
77
        if ($cache) {
78
            $this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION);
79
        }
80
81
        return $token;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getSlug()
88
    {
89
        return self::NAME;
90
    }
91
}
92