Completed
Pull Request — master (#5)
by Risan Bagja
01:34
created

Config::getCallbackUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Risan\OAuth1;
4
5
use InvalidArgumentException;
6
use Risan\OAuth1\Credentials\ClientCredentials;
7
8
class Config implements ConfigInterface
9
{
10
    /**
11
     * The ClientCredentials instance.
12
     *
13
     * @var \Risan\OAuth1\Credentials\ClientCredentials
14
     */
15
    protected $clientCredentials;
16
17
    /**
18
     * The callback URI.
19
     *
20
     * @var string|null
21
     */
22
    protected $callbackUri;
23
24
    /**
25
     * The URL for obtaining temporary credentials. Also known as request token
26
     * URL.
27
     *
28
     * @var string
29
     */
30
    protected $temporaryCredentialsUrl;
31
32
    /**
33
     * The URL for asking user to authorize the request.
34
     *
35
     * @var string
36
     */
37
    protected $authorizationUrl;
38
39
    /**
40
     * The URL for obtaining token credentials. Also known as access token URL.
41
     *
42
     * @var string
43
     */
44
    protected $tokenCredentialsUrl;
45
46
    /**
47
     * Create new instance of Config class.
48
     *
49
     * @param \Risan\OAuth1\Credentials\ClientCredentials $clientCredentials
50
     * @param string $temporaryCredentialsUrl
51
     * @param string $authorizationUrl
52
     * @param string $tokenCredentialsUrl
53
     * @param string|null $callbackUri
54
     */
55 18
    public function __construct(ClientCredentials $clientCredentials, $temporaryCredentialsUrl, $authorizationUrl, $tokenCredentialsUrl, $callbackUri = null)
56
    {
57 18
        $this->clientCredentials = $clientCredentials;
58 18
        $this->temporaryCredentialsUrl = $temporaryCredentialsUrl;
59 18
        $this->authorizationUrl = $authorizationUrl;
60 18
        $this->tokenCredentialsUrl = $tokenCredentialsUrl;
61 18
        $this->callbackUri = $callbackUri;
62 18
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 6
    public function getClientCredentials()
68
    {
69 6
        return $this->clientCredentials;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 2
    public function getClientCredentialsIdentifier()
76
    {
77 2
        return $this->getClientCredentials()->getIdentifier();
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 2
    public function getClientCredentialsSecret()
84
    {
85 2
        return $this->getClientCredentials()->getSecret();
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function hasCallbackUri()
92
    {
93 1
        return $this->getCallbackUri() !== null;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 3
    public function getCallbackUri()
100
    {
101 3
        return $this->callbackUri;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 2
    public function getTemporaryCredentialsUrl()
108
    {
109 2
        return $this->temporaryCredentialsUrl;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115 2
    public function getAuthorizationUrl()
116
    {
117 2
        return $this->authorizationUrl;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123 2
    public function getTokenCredentialsUrl()
124
    {
125 2
        return $this->tokenCredentialsUrl;
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 9
    public static function createFromArray(array $config)
132
    {
133
        $requiredParams = [
134 9
            'client_credentials_identifier',
135
            'client_credentials_secret',
136
            'temporary_credentials_url',
137
            'authorization_url',
138
            'token_credentials_url',
139
        ];
140
141 9
        foreach ($requiredParams as $param) {
142 9
            if (! isset($config[$param])) {
143 9
                throw new InvalidArgumentException("Missing OAuth1 client configuration: {$param}.");
144
            }
145
        }
146
147 4
        $callbackUri = isset($config['callback_uri']) ? $config['callback_uri'] : null;
148
149 4
        return new static(
150 4
            new ClientCredentials($config['client_credentials_identifier'], $config['client_credentials_secret']),
151 4
            $config['temporary_credentials_url'],
152 4
            $config['authorization_url'],
153 4
            $config['token_credentials_url'],
154 4
            $callbackUri
155
        );
156
    }
157
}
158