Completed
Push — master ( e7435a...ed4f51 )
by Francesco
03:00
created

Auth::autenticate()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 35
rs 8.439
cc 6
eloc 18
nc 7
nop 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Auth::requestAsync() 0 4 1
A Auth::getConfig() 0 4 1
A Auth::needToRevalidate() 0 10 2
1
<?php
2
3
namespace Audiens\AppnexusClient;
4
5
use Audiens\AppnexusClient\authentication\AuthStrategyInterface;
6
use Doctrine\Common\Cache\Cache;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Psr7\Response;
10
use Psr\Http\Message\RequestInterface;
11
12
/**
13
 * Class Auth
14
 *
15
 * see https://wiki.apnexus.com/display/adnexusdocumentation/Segment+Service
16
 *
17
 */
18
class Auth implements ClientInterface
19
{
20
21
    /** @var  Cache */
22
    protected $cache;
23
24
    /** @var  Client */
25
    protected $client;
26
27
    /** @var string */
28
    protected $token;
29
30
    /** @var string */
31
    protected $username;
32
33
    /** @var string */
34
    protected $password;
35
36
    protected $authStrategy;
37
38
    /**
39
     * @param                       $username
40
     * @param                       $password
41
     * @param ClientInterface       $clientInterface
42
     * @param AuthStrategyInterface $authStrategy
43
     */
44
    public function __construct(
45
        $username,
46
        $password,
47
        ClientInterface $clientInterface,
48
        AuthStrategyInterface $authStrategy
49
    ) {
50
        $this->username = $username;
51
        $this->password = $password;
52
53
        $this->client = $clientInterface;
0 ignored issues
show
Documentation Bug introduced by
$clientInterface is of type object<GuzzleHttp\ClientInterface>, but the property $client was declared to be of type object<GuzzleHttp\Client>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
54
        $this->authStrategy = $authStrategy;
55
56
    }
57
58
    /**
59
     * @param string $method
60
     * @param null   $uri
61
     * @param array  $options
62
     *
63
     * @return mixed|\Psr\Http\Message\ResponseInterface
64
     * @throws \Exception
65
     */
66
    public function request($method, $uri = null, array $options = [])
67
    {
68
69
        $optionForToken = [
70
            'headers' => [
71
                'Authorization' => $this->authStrategy->authenticate($this->username, $this->password),
72
            ],
73
        ];
74
75
        $options = array_merge($options, $optionForToken);
76
77
        $response = $this->client->request($method, $uri, $options);
78
79
        if (!$this->needToRevalidate($response)) {
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
80
            return $response;
81
        }
82
83
        $optionForToken = [
84
            'headers' => [
85
                'Authorization' => $this->authStrategy->authenticate($this->username, $this->password),
86
            ],
87
        ];
88
89
        $options = array_merge($options, $optionForToken);
90
91
        return $this->client->request($method, $uri, $options);
92
93
    }
94
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function send(RequestInterface $request, array $options = [])
100
    {
101
        return $this->client->send($request, $options);
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function sendAsync(RequestInterface $request, array $options = [])
108
    {
109
        return $this->client->sendAsync($request, $options);
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function requestAsync($method, $uri, array $options = [])
116
    {
117
        return $this->client->requestAsync($method, $uri, $options);
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function getConfig($option = null)
124
    {
125
        return $this->client->getConfig($option);
126
    }
127
128
129
    /**
130
     * @param Response $response
131
     *
132
     * @return bool
133
     */
134
    protected function needToRevalidate(Response $response)
135
    {
136
137
        $content = json_decode($response->getBody()->getContents(), true);
138
139
        $response->getBody()->rewind();
140
141
        return isset($content['response']['error_id']) && $content['response']['error_id'] == 'NOAUTH';
142
143
    }
144
145
}
146