Completed
Push — master ( 06e523...365332 )
by Дмитрий
03:43
created

AbstractBaseProvider::useState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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