Completed
Push — master ( bda8d5...d74c6d )
by Дмитрий
03:32 queued 01:52
created

AbstractBaseProvider::getRedirectUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\Provider;
9
10
use SocialConnect\Common\Http\Client\ClientInterface;
11
use SocialConnect\Provider\Session\SessionInterface;
12
13
abstract class AbstractBaseProvider
14
{
15
    /**
16
     * @var Consumer
17
     */
18
    protected $consumer;
19
20
    /**
21
     * @var array
22
     */
23
    protected $scope = [];
24
25
    /**
26
     * @var ClientInterface
27
     */
28
    protected $httpClient;
29
30
    /**
31
     * @var string
32
     */
33
    protected $redirectUri;
34
35
    /**
36
     * @var SessionInterface
37
     */
38
    protected $session;
39
40
    /**
41
     * @var array
42
     */
43
    protected $options = [];
44
45
    /**
46
     * @param ClientInterface $httpClient
47
     * @param SessionInterface $session
48
     * @param array $parameters
49
     */
50 332
    public function __construct(ClientInterface $httpClient, SessionInterface $session, Consumer $consumer, array $parameters)
51
    {
52 332
        $this->httpClient = $httpClient;
53 332
        $this->session = $session;
54 332
        $this->consumer = $consumer;
55
56 332
        if (isset($parameters['scope'])) {
57
            $this->setScope($parameters['scope']);
58
        }
59
60 332
        if (isset($parameters['redirectUri'])) {
61 328
            $this->redirectUri = $parameters['redirectUri'];
62
        }
63
64 332
        if (isset($parameters['options'])) {
65
            $this->options = $parameters['options'];
66
        }
67 332
    }
68
69
    /**
70
     * @param string $key
71
     * @param bool $default
72
     * @return bool
73
     */
74 23
    public function getBoolOption($key, $default): bool
75
    {
76 23
        if (array_key_exists($key, $this->options)) {
77
            return (bool) $this->options[$key];
78
        }
79
80 23
        return $default;
81
    }
82
83
    /**
84
     * @param string $key
85
     * @param array $default
86
     * @return array
87
     */
88 31
    public function getArrayOption($key, array $default = []): array
89
    {
90 31
        if (array_key_exists($key, $this->options)) {
91
            return (array) $this->options[$key];
92
        }
93
94 31
        return $default;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 48
    public function getRedirectUrl(): string
101
    {
102 48
        return str_replace('${provider}', $this->getName(), $this->redirectUri);
103
    }
104
105
    /**
106
     * Default parameters for auth url, can be redeclared inside implementation of the Provider
107
     *
108
     * @return array
109
     */
110 23
    public function getAuthUrlParameters(): array
111
    {
112 23
        return $this->getArrayOption('auth.parameters', []);
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    abstract public function getBaseUri();
119
120
    /**
121
     * Return Provider's name
122
     *
123
     * @return string
124
     */
125
    abstract public function getName();
126
127
    /**
128
     * @param array $requestParameters
129
     * @return \SocialConnect\Provider\AccessTokenInterface
130
     */
131
    abstract public function getAccessTokenByRequestParameters(array $requestParameters);
132
133
    /**
134
     * @return string
135
     */
136
    abstract public function makeAuthUrl(): string;
137
138
    /**
139
     * Get current user identity from social network by $accessToken
140
     *
141
     * @param AccessTokenInterface $accessToken
142
     * @return \SocialConnect\Common\Entity\User
143
     *
144
     * @throws \SocialConnect\Provider\Exception\InvalidResponse
145
     */
146
    abstract public function getIdentity(AccessTokenInterface $accessToken);
147
148
    /**
149
     * @return array
150
     */
151
    public function getScope()
152
    {
153
        return $this->scope;
154
    }
155
156
    /**
157
     * @param array $scope
158
     */
159
    public function setScope(array $scope)
160
    {
161
        $this->scope = $scope;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getScopeInline()
168
    {
169
        return implode(',', $this->scope);
170
    }
171
172
    /**
173
     * @return \SocialConnect\Provider\Consumer
174
     */
175 1
    public function getConsumer()
176
    {
177 1
        return $this->consumer;
178
    }
179
}
180