SandboxStrategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
dl 5
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 SandboxStrategy
11
 */
12 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...
13
{
14
15
    const NAME = 'sandbox_auth_strategy';
16
17
    const BASE_URL = 'http://api-test.adnxs.com/auth';
18
19
    const CACHE_NAMESPACE  = 'adnx_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