1
|
|
|
<?php namespace Pizdata\OAuth2\Client\Provider; |
2
|
|
|
|
3
|
|
|
use Psr\Http\Message\ResponseInterface; |
4
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use Pizdata\OAuth2\Client\Exception\ShopifyIdentityProviderException; |
7
|
|
|
|
8
|
|
|
class Shopify extends AbstractProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Production Shopify domain. |
12
|
|
|
* |
13
|
|
|
* @const string |
14
|
|
|
*/ |
15
|
|
|
const BASE_SHOPIFY_DOMAIN = 'myshopify.com'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The store name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $store = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $options |
26
|
|
|
* @param array $collaborators |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
18 |
|
public function __construct($options = [], array $collaborators = []) |
31
|
|
|
{ |
32
|
18 |
|
parent::__construct($options, $collaborators); |
33
|
|
|
|
34
|
18 |
|
if (empty($options['store'])) { |
35
|
|
|
$message = 'The "store" option not set. Please set a Store name.'; |
36
|
|
|
throw new \InvalidArgumentException($message); |
37
|
|
|
} |
38
|
|
|
|
39
|
18 |
|
$this->store = $options['store']; |
40
|
18 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get authorization url to begin OAuth flow |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
6 |
|
public function getBaseAuthorizationUrl() |
48
|
|
|
{ |
49
|
6 |
|
return sprintf('https://%s.%s/admin/oauth/authorize', $this->store, self::BASE_SHOPIFY_DOMAIN); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get access token url to retrieve token |
54
|
|
|
* |
55
|
|
|
* @param array $params |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
60
|
|
|
{ |
61
|
12 |
|
return sprintf('https://%s.%s/admin/oauth/access_token', $this->store, self::BASE_SHOPIFY_DOMAIN); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get provider url to fetch user details |
66
|
|
|
* |
67
|
|
|
* @param AccessToken $token |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
72
|
|
|
{ |
73
|
6 |
|
return sprintf( |
74
|
6 |
|
'https://%s.%s/admin/shop.json?access_token=%s', |
75
|
6 |
|
$this->store, |
76
|
6 |
|
self::BASE_SHOPIFY_DOMAIN, |
77
|
6 |
|
$token->getToken() |
78
|
4 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the default scopes used by this provider. |
83
|
|
|
* |
84
|
|
|
* @link https://help.shopify.com/api/getting-started/authentication/oauth#scopes |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
6 |
|
protected function getDefaultScopes() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
6 |
|
'read_orders', |
91
|
|
|
'read_products' |
92
|
4 |
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check a provider response for errors. |
97
|
|
|
* |
98
|
|
|
* @throws IdentityProviderException |
99
|
|
|
* @param ResponseInterface $response |
100
|
|
|
* @param array $data |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
104
|
|
|
{ |
105
|
9 |
|
if ($response->getStatusCode() == 400) { |
106
|
|
|
throw new ShopifyIdentityProviderException($data, 0, $response->getReasonPhrase()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
if (!empty($data['errors'])) { |
110
|
|
|
throw new ShopifyIdentityProviderException($data['errors'], 0, $data); |
111
|
2 |
|
} |
112
|
9 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Generate a user object from a successful user details request. |
116
|
|
|
* |
117
|
|
|
* @param array $response |
118
|
|
|
* @param AccessToken $token |
119
|
|
|
* @return League\OAuth2\Client\Provider\ResourceOwnerInterface |
120
|
|
|
*/ |
121
|
5 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
122
|
|
|
{ |
123
|
5 |
|
$store = new ShopifyResourceOwner($response); |
124
|
|
|
|
125
|
3 |
|
return $store; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: