Completed
Pull Request — master (#15)
by Mischa
15:06 queued 08:38
created

OAuthPasswordAuthentication::setUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
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
    public function getAuthenticationTokens()
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::AUTHENTICATION_GRANT_TYPE,
103
                    'username' => $this->getUsername(),
104
                    'password' => $this->getPassword()
105
                )
106
            );
107
        } catch (ClientException $e) {
108
            throw new AuthenticationException('Could not request 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')) {
118
            $this->accessToken = $responseObj->access_token;
119
120
            return true;
121
        }
122
123
        throw new AuthenticationException('The server returned an unexpected response body.');
124
    }
125
}
126