Completed
Push — develop ( f1f6d4...edda33 )
by Risan Bagja
01:31
created

Config::hasCallbackUri()   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
     * Create new instance of Config class.
41
     *
42
     * @param \Risan\OAuth1\Credentials\ClientCredentials $clientCredentials
43
     * @param string $temporaryCredentialsUrl
44
     * @param string $authorizationUrl
45
     * @param string|null $callbackUri
46
     */
47 16
    public function __construct(ClientCredentials $clientCredentials, $temporaryCredentialsUrl, $authorizationUrl, $callbackUri = null)
48
    {
49 16
        $this->clientCredentials = $clientCredentials;
50 16
        $this->temporaryCredentialsUrl = $temporaryCredentialsUrl;
51 16
        $this->authorizationUrl = $authorizationUrl;
52 16
        $this->callbackUri = $callbackUri;
53 16
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 6
    public function getClientCredentials()
59
    {
60 6
        return $this->clientCredentials;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 2
    public function getClientCredentialsIdentifier()
67
    {
68 2
        return $this->getClientCredentials()->getIdentifier();
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 2
    public function getClientCredentialsSecret()
75
    {
76 2
        return $this->getClientCredentials()->getSecret();
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function hasCallbackUri()
83
    {
84 1
        return $this->getCallbackUri() !== null;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 3
    public function getCallbackUri()
91
    {
92 3
        return $this->callbackUri;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 2
    public function getTemporaryCredentialsUrl()
99
    {
100 2
        return $this->temporaryCredentialsUrl;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 2
    public function getAuthorizationUrl()
107
    {
108 2
        return $this->authorizationUrl;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 8
    public static function createFromArray(array $config)
115
    {
116
        $requiredParams = [
117 8
            'client_credentials_identifier',
118
            'client_credentials_secret',
119
            'temporary_credentials_url',
120
            'authorization_url',
121
        ];
122
123 8
        foreach ($requiredParams as $param) {
124 8
            if (! isset($config[$param])) {
125 8
                throw new InvalidArgumentException("Missing OAuth1 client configuration: {$param}.");
126
            }
127
        }
128
129 4
        $callbackUri = isset($config['callback_uri']) ? $config['callback_uri'] : null;
130
131 4
        return new static(
132 4
            new ClientCredentials($config['client_credentials_identifier'], $config['client_credentials_secret']),
133 4
            $config['temporary_credentials_url'],
134 4
            $config['authorization_url'],
135 4
            $callbackUri
136
        );
137
    }
138
}
139