Completed
Pull Request — master (#15)
by Mischa
03:26
created

OAuthDecorator::addAuthentication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
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\Request;
16
17
use Superdesk\ContentApiSdk\Exception\RequestException;
18
19
/**
20
 * OAuth decorator for API request.
21
 */
22
class OAuthDecorator extends RequestDecorator
23
{
24
    /**
25
     * OAuth access token.
26
     *
27
     * @var string
28
     */
29
    protected $access_token;
30
31
    /**
32
     * OAuth refresh token.
33
     *
34
     * @var string
35
     */
36
    protected $refresh_token;
37
38
    /**
39
     * Get access token.
40
     *
41
     * @return string
42
     */
43
    public function getAccessToken()
44
    {
45
        return $this->access_token;
46
    }
47
48
    /**
49
     * Set access token.
50
     *
51
     * @param string $access_token
52
     *
53
     * @return self
54
     */
55
    public function setAccessToken($access_token)
56
    {
57
        $this->access_token = $access_token;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Sets Authentication header on decorated request.
64
     *
65
     * @return self
66
     *
67
     * @throws RequestException When access_token is not set
68
     */
69
    public function addAuthentication()
70
    {
71
        if ($this->access_token == null) {
72
            throw new RequestException('Property access_token should be set.');
73
        }
74
75
        $headers = $this->decoratedRequest->getHeaders();
76
        $headers['Authorization'] = sprintf('%s %s', 'OAuth2', $this->access_token);
77
        $this->decoratedRequest->setHeaders($headers);
78
79
        return $this;
80
    }
81
}
82