OAuthDecorator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessToken() 0 4 1
A setAccessToken() 0 6 1
A addAuthentication() 0 12 2
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|null
28
     */
29
    protected $accessToken = null;
30
31
    /**
32
     * Get access token.
33
     *
34
     * @return string
35
     */
36
    public function getAccessToken()
37
    {
38
        return $this->accessToken;
39
    }
40
41
    /**
42
     * Set access token.
43
     *
44
     * @param string $accessToken
45
     *
46
     * @return self
47
     */
48
    public function setAccessToken($accessToken)
49
    {
50
        $this->accessToken = $accessToken;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Sets Authentication header on decorated request.
57
     *
58
     * @return self
59
     *
60
     * @throws RequestException When accessToken is not set
61
     */
62
    public function addAuthentication()
63
    {
64
        if ($this->accessToken === null) {
65
            throw new RequestException('Property accessToken should be set.');
66
        }
67
68
        $headers = $this->decoratedRequest->getHeaders();
69
        $headers['Authorization'] = sprintf('%s %s', 'OAuth2', $this->accessToken);
70
        $this->decoratedRequest->setHeaders($headers);
71
72
        return $this;
73
    }
74
}
75