Completed
Push — master ( 01b1f4...cd2255 )
by Francesco
03:06
created

SandboxStrategy::getSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    /**
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|true $cache
41
     *
42
     * @return mixed
43
     * @throws AuthException
44
     */
45
    public function authenticate($username, $password, $cache = true)
46
    {
47
48
        $cacheKey = self::CACHE_NAMESPACE.sha1($username.$password.self::BASE_URL);
49
50
        if ($cache) {
51
            if ($this->cache->contains($cacheKey)) {
52
                return $this->cache->fetch($cacheKey);
53
            }
54
        }
55
56
        $payload = [
57
            'auth' => [
58
                'username' => $username,
59
                'password' => $password,
60
            ],
61
        ];
62
63
        $response = $this->client->request('POST', self::BASE_URL, ['body' => json_encode($payload)]);
64
65
        $content = $response->getBody()->getContents();
66
        $response->getBody()->rewind();
67
68
        $contentArray = json_decode($content, true);
69
70
        if (!isset($contentArray["response"]["token"])) {
71
            throw new AuthException($content);
72
        }
73
74
        $token = $contentArray["response"]["token"];
75
76
        if ($cache) {
77
            $this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION);
78
        }
79
80
        return $token;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getSlug()
87
    {
88
        return self::NAME;
89
    }
90
}
91