1<?php
2/**
3 * 500px service.
4 *
5 * @author  Pedro Amorim <contact@pamorim.fr>
6 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7 * @link    https://developers.500px.com/
8 */
9
10namespace OAuth\OAuth1\Service;
11
12use OAuth\OAuth1\Signature\SignatureInterface;
13use OAuth\OAuth1\Token\StdOAuth1Token;
14use OAuth\Common\Http\Exception\TokenResponseException;
15use OAuth\Common\Http\Uri\Uri;
16use OAuth\Common\Consumer\CredentialsInterface;
17use OAuth\Common\Http\Uri\UriInterface;
18use OAuth\Common\Storage\TokenStorageInterface;
19use OAuth\Common\Http\Client\ClientInterface;
20
21/**
22 * 500px service.
23 *
24 * @author  Pedro Amorim <contact@pamorim.fr>
25 * @license http://www.opensource.org/licenses/mit-license.html MIT License
26 * @link    https://developers.500px.com/
27 */
28class FiveHundredPx extends AbstractService
29{
30    public function __construct(
31        CredentialsInterface $credentials,
32        ClientInterface $httpClient,
33        TokenStorageInterface $storage,
34        SignatureInterface $signature,
35        UriInterface $baseApiUri = null
36    ) {
37        parent::__construct(
38            $credentials,
39            $httpClient,
40            $storage,
41            $signature,
42            $baseApiUri
43        );
44
45        if (null === $baseApiUri) {
46            $this->baseApiUri = new Uri('https://api.500px.com/v1/');
47        }
48    }
49
50    /**
51     * {@inheritDoc}
52     */
53    public function getRequestTokenEndpoint()
54    {
55        return new Uri('https://api.500px.com/v1/oauth/request_token');
56    }
57
58    /**
59     * {@inheritdoc}
60     */
61    public function getAuthorizationEndpoint()
62    {
63        return new Uri('https://api.500px.com/v1/oauth/authorize');
64    }
65
66    /**
67     * {@inheritdoc}
68     */
69    public function getAccessTokenEndpoint()
70    {
71        return new Uri('https://api.500px.com/v1/oauth/access_token');
72    }
73
74    /**
75     * {@inheritdoc}
76     */
77    protected function parseRequestTokenResponse($responseBody)
78    {
79        parse_str($responseBody, $data);
80
81        if (null === $data || !is_array($data)) {
82            throw new TokenResponseException('Unable to parse response.');
83        } elseif (!isset($data['oauth_callback_confirmed'])
84            || $data['oauth_callback_confirmed'] !== 'true'
85        ) {
86            throw new TokenResponseException('Error in retrieving token.');
87        }
88
89        return $this->parseAccessTokenResponse($responseBody);
90    }
91
92    /**
93     * {@inheritdoc}
94     */
95    protected function parseAccessTokenResponse($responseBody)
96    {
97        parse_str($responseBody, $data);
98
99        if (null === $data || !is_array($data)) {
100            throw new TokenResponseException('Unable to parse response.');
101        } elseif (isset($data['error'])) {
102            throw new TokenResponseException(
103                'Error in retrieving token: "' . $data['error'] . '"'
104            );
105        }
106
107        $token = new StdOAuth1Token();
108
109        $token->setRequestToken($data['oauth_token']);
110        $token->setRequestTokenSecret($data['oauth_token_secret']);
111        $token->setAccessToken($data['oauth_token']);
112        $token->setAccessTokenSecret($data['oauth_token_secret']);
113
114        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
115        unset($data['oauth_token'], $data['oauth_token_secret']);
116        $token->setExtraParams($data);
117
118        return $token;
119    }
120}
121