Completed
Pull Request — master (#15)
by Mischa
05:43
created

OAuthPasswordAuthentication::getPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the PHP SDK library for the Superdesk Content API.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace Superdesk\ContentApiSdk\API\Authentication;
16
17
use Superdesk\ContentApiSdk\API\Request\RequestInterface;
18
use Superdesk\ContentApiSdk\ContentApiSdk;
19
use Superdesk\ContentApiSdk\Exception\AuthenticationException;
20
use Superdesk\ContentApiSdk\Exception\ClientException;
21
use Superdesk\ContentApiSdk\Exception\InvalidDataException;
22
23
class OAuthPasswordAuthentication extends AbstractAuthentication
24
{
25
    const AUTHENTICATION_GRANT_TYPE = 'password';
26
27
    /**
28
     * Username for OAuth password authentication.
29
     *
30
     * @var string
31
     */
32
    protected $username;
33
34
    /**
35
     * Password for OAuth password authentication.
36
     *
37
     * @var string
38
     */
39
    protected $password;
40
41
    /**
42
     * Gets the value of username.
43
     *
44
     * @return string
45
     */
46
    public function getUsername()
47
    {
48
        return $this->username;
49
    }
50
51
    /**
52
     * Sets the value of username.
53
     *
54
     * @param string $username Value to set
55
     *
56
     * @return self
57
     */
58
    public function setUsername($username)
59
    {
60
        $this->username = $username;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Gets the value of password.
67
     *
68
     * @return string
69
     */
70
    public function getPassword()
71
    {
72
        return $this->password;
73
    }
74
75
    /**
76
     * Sets the value of password.
77
     *
78
     * @param string $password Value to set
79
     *
80
     * @return self
81
     */
82
    public function setPassword($password)
83
    {
84
        $this->password = $password;
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 View Code Duplication
    public function refreshAccessToken()
0 ignored issues
show
Duplication introduced by
This method 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...
93
    {
94
        try {
95
            $response = $this->client->makeCall(
96
                $this->getAuthenticationUrl(),
97
                array(),
98
                array(),
99
                'POST',
100
                array(
101
                    'client_id' => $this->getClientId(),
102
                    'grant_type' => self::REFRESH_GRANT_TYPE,
103
                    'username' => $this->getUsername(),
104
                    'refresh_token' => $this->refreshToken
105
                )
106
            );
107
        } catch (ClientException $e) {
108
            throw new AuthenticationException('Could not refresh access token.', $e->getCode(), $e);
109
        }
110
111
        try {
112
            $responseObj = ContentApiSdk::getValidJsonObj($response['body']);
113
        } catch (InvalidDataException $e) {
114
            throw new AuthenticationException('Authentication response body is not (valid) json.', $e->getCode(), $e);
115
        }
116
117
        if (property_exists($responseObj, 'access_token') && property_exists($responseObj, 'refresh_token')) {
118
            $this->accessToken = $responseObj->access_token;
119
            $this->refreshToken = $responseObj->refresh_token;
120
121
            return true;
122
        }
123
124
        throw new AuthenticationException('The server returned an unexpected response body.');
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 View Code Duplication
    public function getAuthenticationTokens()
0 ignored issues
show
Duplication introduced by
This method 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...
131
    {
132
        try {
133
            $response = $this->client->makeCall(
134
                $this->getAuthenticationUrl(),
135
                array(),
136
                array(),
137
                'POST',
138
                array(
139
                    'client_id' => $this->getClientId(),
140
                    'grant_type' => self::AUTHENTICATION_GRANT_TYPE,
141
                    'username' => $this->getUsername(),
142
                    'password' => $this->getPassword()
143
                )
144
            );
145
        } catch (ClientException $e) {
146
            throw new AuthenticationException('Could not request access token.', $e->getCode(), $e);
147
        }
148
149
        try {
150
            $responseObj = ContentApiSdk::getValidJsonObj($response['body']);
151
        } catch (InvalidDataException $e) {
152
            throw new AuthenticationException('Authentication response body is not (valid) json.', $e->getCode(), $e);
153
        }
154
155
        if (property_exists($responseObj, 'access_token') && property_exists($responseObj, 'refresh_token')) {
156
            $this->accessToken = $responseObj->access_token;
157
            $this->refreshToken = $responseObj->refresh_token;
158
159
            return true;
160
        }
161
162
        throw new AuthenticationException('The server returned an unexpected response body.');
163
    }
164
}
165