Completed
Push — master ( bd326e...3b0d07 )
by Paweł
12:10 queued 06:38
created

OAuthPasswordAuthentication   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 107
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsername() 0 4 1
A setUsername() 0 6 1
A getPassword() 0 4 1
A setPassword() 0 6 1
B getAuthenticationTokens() 0 37 5
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
/**
24
 * OAuth password authentication class.
25
 */
26
class OAuthPasswordAuthentication extends AbstractAuthentication
27
{
28
    const AUTHENTICATION_GRANT_TYPE = 'password';
29
30
    /**
31
     * Username for OAuth password authentication.
32
     *
33
     * @var string
34
     */
35
    protected $username;
36
37
    /**
38
     * Password for OAuth password authentication.
39
     *
40
     * @var string
41
     */
42
    protected $password;
43
44
    /**
45
     * Gets the value of username.
46
     *
47
     * @return string
48
     */
49
    public function getUsername()
50
    {
51
        return $this->username;
52
    }
53
54
    /**
55
     * Sets the value of username.
56
     *
57
     * @param string $username Value to set
58
     *
59
     * @return self
60
     */
61
    public function setUsername($username)
62
    {
63
        $this->username = $username;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Gets the value of password.
70
     *
71
     * @return string
72
     */
73
    public function getPassword()
74
    {
75
        return $this->password;
76
    }
77
78
    /**
79
     * Sets the value of password.
80
     *
81
     * @param string $password Value to set
82
     *
83
     * @return self
84
     */
85
    public function setPassword($password)
86
    {
87
        $this->password = $password;
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getAuthenticationTokens()
96
    {
97
        try {
98
            $response = $this->client->makeCall(
99
                $this->getAuthenticationUrl(),
100
                array(),
101
                array(),
102
                'POST',
103
                array(
104
                    'client_id' => $this->getClientId(),
105
                    'grant_type' => self::AUTHENTICATION_GRANT_TYPE,
106
                    'username' => $this->getUsername(),
107
                    'password' => $this->getPassword()
108
                )
109
            );
110
        } catch (ClientException $e) {
111
            throw new AuthenticationException('Could not request access token.', $e->getCode(), $e);
112
        }
113
114
        if ($response['status'] === 200) {
115
            try {
116
                $responseObj = ContentApiSdk::getValidJsonObj($response['body']);
117
            } catch (InvalidDataException $e) {
118
                throw new AuthenticationException('Authentication response body is not (valid) json.', $e->getCode(), $e);
119
            }
120
121
            if (property_exists($responseObj, 'access_token')) {
122
                $this->accessToken = $responseObj->access_token;
123
124
                return true;
125
            }
126
127
            throw new AuthenticationException('The server returned an unexpected response body.');
128
        }
129
130
        throw new AuthenticationException(sprintf('The server returned an error with status %s.', $response['status']), $response['status']);
131
    }
132
}
133