These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SocialiteProviders\Manager\OAuth1; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use GuzzleHttp\Exception\BadResponseException; |
||
7 | use InvalidArgumentException; |
||
8 | use League\OAuth1\Client\Credentials\TemporaryCredentials; |
||
9 | use League\OAuth1\Client\Server\Server as BaseServer; |
||
10 | use SocialiteProviders\Manager\ConfigTrait; |
||
11 | |||
12 | abstract class Server extends BaseServer |
||
13 | { |
||
14 | use ConfigTrait; |
||
15 | |||
16 | /** |
||
17 | * The custom parameters to be sent with the request. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $parameters = []; |
||
22 | |||
23 | /** |
||
24 | * The scopes being requested. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $scopes = []; |
||
29 | |||
30 | /** |
||
31 | * The separating character for the requested scopes. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $scopeSeparator = ','; |
||
36 | |||
37 | /** |
||
38 | * Retrieves token credentials by passing in the temporary credentials, |
||
39 | * the temporary credentials identifier as passed back by the server |
||
40 | * and finally the verifier code. |
||
41 | * |
||
42 | * @param \League\OAuth1\Client\Credentials\TemporaryCredentials $temporaryCredentials |
||
43 | * @param string $temporaryIdentifier |
||
44 | * @param string $verifier |
||
45 | * |
||
46 | * @return array |
||
47 | * @throws \InvalidArgumentException |
||
48 | */ |
||
49 | public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier) |
||
50 | { |
||
51 | if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) { |
||
52 | throw new InvalidArgumentException( |
||
53 | 'Temporary identifier passed back by server does not match that of stored temporary credentials. |
||
54 | Potential man-in-the-middle.' |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | $uri = $this->urlTokenCredentials(); |
||
59 | $bodyParameters = ['oauth_verifier' => $verifier]; |
||
60 | |||
61 | $client = $this->createHttpClient(); |
||
62 | |||
63 | $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); |
||
64 | |||
65 | try { |
||
66 | if ($client instanceof Client) { |
||
67 | $response = $client->post($uri, [ |
||
68 | 'headers' => $headers, |
||
69 | 'form_params' => $bodyParameters, |
||
70 | ]); |
||
71 | } else { |
||
72 | $response = $client->post($uri, $headers, $bodyParameters)->send(); |
||
73 | } |
||
74 | } catch (BadResponseException $e) { |
||
75 | $this->handleTokenCredentialsBadResponse($e); |
||
76 | } |
||
77 | |||
78 | return [ |
||
0 ignored issues
–
show
|
|||
79 | 'tokenCredentials' => $this->createTokenCredentials($response->getBody()), |
||
80 | 'credentialsResponseBody' => $response->getBody(), |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the scopes of the requested access. |
||
86 | * |
||
87 | * @param array $scopes |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function scopes(array $scopes) |
||
92 | { |
||
93 | $this->scopes = array_unique(array_merge($this->scopes, $scopes)); |
||
94 | |||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Set the custom parameters of the request. |
||
100 | * |
||
101 | * @param array $parameters |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function with(array $parameters) |
||
106 | { |
||
107 | $this->parameters = $parameters; |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Format the given scopes. |
||
114 | * |
||
115 | * @param array $scopes |
||
116 | * @param string $scopeSeparator |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
121 | { |
||
122 | return implode($scopeSeparator, $scopes); |
||
123 | } |
||
124 | } |
||
125 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.