1010 */
1111class PrintfulApiClient
1212{
13+ const TYPE_LEGACY_STORE_KEY = 'legacy-store-key ' ;
14+ const TYPE_OAUTH_TOKEN = 'oauth-token ' ;
15+ const DEFAULT_KEY = self ::TYPE_LEGACY_STORE_KEY ;
16+
1317 /**
1418 * Printful API key
15- * @var string
19+ * @var string|null
20+ */
21+ private $ legacyStoreKey ;
22+
23+ /**
24+ * Printful OAuth token
25+ * @var string|null
1626 */
17- private $ key = '' ;
27+ private $ oauthToken ;
1828
1929 private $ lastResponseRaw ;
2030
@@ -37,16 +47,36 @@ class PrintfulApiClient
3747 public $ curlTimeout = 20 ;
3848
3949 /**
40- * @param string $key Printful Store API key
50+ * @param string $key
51+ * @param string $type // PrintfulApiClient::TYPE_LEGACY_STORE_KEY or PrintfulApiClient::TYPE_OAUTH_TOKEN
4152 * @throws \Printful\Exceptions\PrintfulException if the library failed to initialize
4253 */
43- public function __construct ($ key )
54+ public function __construct ($ key, $ type = self :: DEFAULT_KEY )
4455 {
45- if (strlen ($ key ) < 32 ) {
46- throw new PrintfulException ('Missing or invalid Printful store key! ' );
56+ if ($ type === self :: TYPE_LEGACY_STORE_KEY && strlen ($ key ) < 32 ) {
57+ throw new PrintfulException ('Invalid Printful store key! ' );
4758 }
4859
49- $ this ->key = $ key ;
60+ $ this ->legacyStoreKey = $ type === self ::TYPE_LEGACY_STORE_KEY ? $ key : null ;
61+ $ this ->oauthToken = $ type === self ::TYPE_OAUTH_TOKEN ? $ key : null ;
62+ }
63+
64+ /**
65+ * @param string $oAuthToken
66+ * @throws PrintfulException
67+ */
68+ public static function createOauthClient ($ oAuthToken )
69+ {
70+ return new self ($ oAuthToken , self ::TYPE_OAUTH_TOKEN );
71+ }
72+
73+ /**
74+ * @param string $legacyStoreKey
75+ * @throws PrintfulException
76+ */
77+ public static function createLegacyStoreKeyClient ($ legacyStoreKey )
78+ {
79+ return new self ($ legacyStoreKey , self ::TYPE_LEGACY_STORE_KEY );
5080 }
5181
5282 /**
@@ -154,7 +184,8 @@ private function request($method, $path, array $params = [], $data = null)
154184
155185 $ curl = curl_init ($ this ->url . $ url );
156186
157- curl_setopt ($ curl , CURLOPT_USERPWD , $ this ->key );
187+ $ this ->setCredentials ($ curl );
188+
158189 curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , $ method );
159190 curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
160191 curl_setopt ($ curl , CURLOPT_FOLLOWLOCATION , true );
@@ -194,4 +225,19 @@ private function request($method, $path, array $params = [], $data = null)
194225 }
195226 return $ response ['result ' ];
196227 }
228+
229+ /**
230+ * @param resource $curl
231+ * @throws PrintfulException
232+ */
233+ private function setCredentials ($ curl )
234+ {
235+ if ($ this ->oauthToken !== null ) {
236+ curl_setopt ($ curl , CURLOPT_HTTPHEADER , ["Authorization: Bearer $ this ->oauthToken " ]);
237+ } elseif ($ this ->legacyStoreKey !== null ) {
238+ curl_setopt ($ curl , CURLOPT_USERPWD , $ this ->legacyStoreKey );
239+ } else {
240+ throw new PrintfulException ('Either OAuth token or store key must be set to make this request. ' );
241+ }
242+ }
197243}
0 commit comments